您的位置:首页 > 大数据 > 人工智能

AIM Tech Round (Div. 2)-C. Graph and String

2016-10-16 11:29 411 查看
原题链接

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.

Examples

input
2 1
1 2


output
Yes
aa


input
4 3
1 2
1 3
1 4


output
No


若有一点连上了其他所有的点,则这个点对应的字符为'b',其他染成'a', 'c', 若有冲突则为NO

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#define maxn 5005
#define MOD 1000000007
using namespace std;
typedef long long ll;

int vis[505][505], cnt[505];
char str[505];
int main(){

//freopen("in.txt", "r", stdin);
int n, m, a, b;
int d = 'a' + 'c';

scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++){
scanf("%d%d", &a, &b);
vis[a][b] = 1;
vis[b][a] = 1;
cnt[a]++;
cnt[b]++;
}
for(int i = 1; i <= n; i++){
if(cnt[i] == n - 1){
str[i] = 'b';
}
else{
if(str[i] == 0)
str[i] = 'a';
for(int j = 1; j <= n; j++){
if(i == j || cnt[j] == n - 1)
continue;
if(vis[i][j] == 1){
if(str[j] == 0)
str[j] = str[i];
else if(str[j] != str[i]){
puts("No");
return 0;
}
}
else{
if(str[j] == 0)
str[j] = d - str[i];
else if(str[j] == str[i]){
puts("No");
return 0;
}
}
}
}
}
puts("Yes");
puts(str+1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: