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

题目:
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;
}



你可能感兴趣的:(算法,DFS,codeforces,染色,连通图)