#AIM Tech Round [div2] C. Graph and String 【连通图、染色】

图论与字符串匹配
本文探讨了一个有趣的图论问题,即根据给定的图结构还原可能的字符串顺序,字符串仅包含'a'、'b'、'c'三个字符。通过分析图的特性,如全连接点和边的关系,提出了一种基于深度优先搜索(DFS)的解决方案。
题目:
C. Graph and String
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One day student Vasya was sitting on a lecture and mentioned a string s1s2... sn, consisting of letters "a", "b" and "c" that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph G with the following properties:

  • G has exactly n vertices, numbered from 1 to n.
  • For all pairs of vertices i and j, where i ≠ j, there is an edge connecting them if and only if characters si and sj are either equal or neighbouring in the alphabet. That is, letters in pairs "a"-"b" and "b"-"c" are neighbouring, while letters "a"-"c" are not.

Vasya painted the resulting graph near the string and then erased the string. Next day Vasya's friend Petya came to a lecture and found some graph at his desk. He had heard of Vasya's adventure and now he wants to find out whether it could be the original graph G, painted by Vasya. In order to verify this, Petya needs to know whether there exists a string s, such that if Vasya used this s he would produce the given graph G.

Input

The first line of the input contains two integers n and m  — the number of vertices and edges in the graph found by Petya, respectively.

Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the edges of the graph G. It is guaranteed, that there are no multiple edges, that is any pair of vertexes appear in this list no more than once.

Output

In the first line print "Yes" (without the quotes), if the string s Petya is interested in really exists and "No" (without the quotes) otherwise.

If the string s exists, then print it on the second line of the output. The length of s must be exactly n, it must consist of only letters "a", "b" and "c" only, and the graph built using this string must coincide with G. If there are multiple possible answers, you may print any of them.

Sample test(s)
input
2 1
1 2
output
Yes
aa
input
4 3
1 2
1 3
1 4
output
No


思路:题意为将n个字母(只含a,b,c)摆成正n边形,并在任意非(a,c)的两点之间连一条线。样例中将给出点数和每一条边连接的点,要求还原出原图可能的字母顺序。

因为任意两点都有两种情况,即“有边”,或“无边”,那么,如果某两点间没有边连接,则这两点一定是“a”或“c”。同理,若某点和所有其它点都有边相连,那只存在两种情况:
1,所有点的字母都一样。
2,这个点是“b”。
所以,为了方便,我们可以把和其它点都有连接(全联通)的点标记为b。

然后,任意非b两点之间一定有以下关系:
若它们有边相连,那么它们取值相同。
若他们没有边,那么它们取值不同。

任取一个点作为起点,赋值“a”或“c”,然后以上述条件展开dfs染色即可。

若在染色中发现某点应当赋值为“a”,但该点已经赋值为“c”(或相反),说明该图无解,输出no。

以下代码用邻接矩阵存储连接信息。

</pre><pre name="code" class="cpp">#include<iostream>
#include<algorithm>

using namespace std;

int data[505][505];
char ans[505];

int n, m, endi;

void dfs(int i)
{
	for (size_t s = 0; s < n; s++)
	{
		if (i != s&&data[s][i] == 0 && ans[s] == ans[i])
		{
			endi = 1;
			return;
		}
		if (i != s&&data[s][i] == 1 &&( (ans[s] == 'a'&&ans[i] == 'c') || (ans[s] == 'c'&&ans[i] == 'a')))
		{
			endi = 1;
			return;
		}
		if (i != s&&data[s][i] == 0 && ans[s] == 0)
		{
			if (ans[i] == 'a')
			{
				ans[s] = 'c';
			}
			else
			{
				ans[s] = 'a';
			}
			dfs(s);
		}
		if (i != s&&data[s][i] == 1 && ans[s] == 0)
		{
			if (ans[i] == 'a')
			{
				ans[s] = 'a';
			}
			else
			{
				ans[s] = 'c';
			}
			dfs(s);
		}
	}
}

int main()
{
	cin >> n >> m;
for (size_t i = 0; i < m; i++)
	{
		int a, b;
		cin >> a >> b;
		data[a - 1][b - 1] = 1;
		data[b - 1][a - 1] = 1;
	}
	for (size_t s = 0; s < n; s++)
	{
		int temp = 0;
		for (size_t i = 0; i < n; i++)
		{
			if (data[s][i] == 0 && i != s)
			{
				temp = 1;
			}
		}
		if (temp == 0)
		{
			ans[s] = 'b';
		}
	}
	for (size_t i = 0; i < n; i++)
	{
		if (ans[i] == 0)
		{
			ans[i] = 'a';
			dfs(i);
		}
	}
	if (endi == 1)
	{
		cout << "no\n";
		return 0;
	}
	cout << "yes\n";
	cout << ans;
}



关于 Codeforces Round 1014 Div. 2 的具体题解和比赛详情并未在当前提供的引用中提及。然而,可以基于类似的竞赛结构以及常见的算法问题类型提供一些推测性的分析。 通常情况下,Codeforces 比赛中的题目会涉及多种经典算法领域,例如动态规划、贪心策略、图论、字符串处理等。以下是可能适用于该轮次的一些常见问题类型的解答框架: ### 动态规划 (Dynamic Programming) 如果某道题目涉及到最优子结构性质,则可以通过构建状态转移方程来解决问题。例如,在某些背包类问题中,定义 `dp[i][j]` 表示前 i 件物品容量为 j 时的最大价值[^3]。 ```python def knapsack(values, weights, capacity): n = len(values) dp = [[0]*(capacity+1) for _ in range(n+1)] for i in range(1,n+1): for w in range(capacity+1): if weights[i-1] <= w: dp[i][w] = max(dp[i-1][w], values[i-1]+dp[i-1][w-weights[i-1]]) else: dp[i][w] = dp[i-1][w] return dp[n][capacity] values = [60, 100, 120] weights = [10, 20, 30] capacity = 50 print(knapsack(values, weights, capacity)) ``` ### 贪心算法 (Greedy Algorithm) 当面对资源分配等问题时,采用局部最优策略往往能够达到全局最佳效果。比如安排会议时间表以最大化会议室利用率的情形下,按照结束时间排序并依次选取不冲突的区间即可实现目标[^4]。 ### 图论 (Graph Theory) 对于连通性和最短路徑计算方面的需求,Dijkstra 和 Floyd-Warshall 是两种非常实用的方法。前者适合单源最短路径查询;后者则能解决多源场景下的需求[^5]。 ```python import heapq def dijkstra(graph, start_node): distances = {node: float('inf') for node in graph} distances[start_node] = 0 priority_queue = [(0,start_node)] while priority_queue: current_distance, current_vertex = heapq.heappop(priority_queue) if current_distance > distances[current_vertex]: continue for neighbor, weight in graph[current_vertex].items(): distance = current_distance + weight if distance < distances[neighbor]: distances[neighbor] = distance heapq.heappush(priority_queue,(distance,neighbor)) graph = { 'A': {'B': 1,'C': 4}, 'B': {'A': 1,'C': 2,'D': 6}, 'C': {'A': 4,'B': 2,'D': 3}, 'D': {'B': 6,'C': 3} } dijkstra(graph, 'A') ``` 尽管上述内容并非直接针对 Codeforces Round 1014 Div. 2 提供的具体解决方案,但它涵盖了比赛中可能出现的核心知识点和技术手段。 ####
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值