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.
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.
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.
2 1 1 2
Yes aa
4 3 1 2 1 3 1 4
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;
}
图论与字符串匹配
本文探讨了一个有趣的图论问题,即根据给定的图结构还原可能的字符串顺序,字符串仅包含'a'、'b'、'c'三个字符。通过分析图的特性,如全连接点和边的关系,提出了一种基于深度优先搜索(DFS)的解决方案。
405

被折叠的 条评论
为什么被折叠?



