cf623Graph and String

网友投稿 763 2022-11-09

cf623Graph and String

cf623Graph and String

​​ 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 Note In the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings “aa”, “ab”, “ba”, “bb”, “bc”, “cb”, “cc” meets the graph’s conditions.

In the second sample the first vertex is connected to all three other vertices, but these three vertices are not connected with each other. That means that they must correspond to distinct letters that are not adjacent, but that is impossible as there are only two such letters: a and c.

一个仅包含abc这3个字母的字符串,a与b和自身相连,b与ac和自身相连,c与b与自身相连,给出字符串每一位的相连情况,要求构造出这样一个字符串

建立补图,如果一个点没有出度,那么它一定是b

否则的话他就是要么a要么c

可是 存在两点之间有边,但是我却给染色成a和c 显然是不合法的

所以要on^2检查一下染色是否合法 如 1234 14有边而其他都没边

那么按照染色来看只能是0 1 0 1但是和要求相悖 所以输出No

#include #include#define N 550inline char gc(){ static char now[1<<16], *S, *T; if(S==T){T=(S=now)+fread(now, 1, 1<<16, stdin); if(S==T)return EOF;} return *S++;}inline long long read(){ long long x=0;char ch=gc(); while (ch<'0'||ch>'9') ch=gc(); while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();} return x;}struct node{ int y,next;}data[N*N*2];int color[N],h[N],n,m,num;bool map[N][N];bool dfs(int x,int colour){ color[x]=colour; for (int i=h[x];i;i=data[i].next){ int y=data[i].y; if (color[y]==color[x]) return false; if (color[y]!=-1) continue; if (!dfs(y,colour^1)) return false; }return true;}int main(){ freopen("cf.in","r",stdin); n=read();m=read(); for (int i=1;i<=m;++i){ int x=read(),y=read();map[x][y]=map[y][x]=true; } for (int i=1;i<=n;++i) for (int j=i+1;j<=n;++j) if (!map[i][j]){ data[++num].y=j;data[num].next=h[i];h[i]=num; data[++num].y=i;data[num].next=h[j];h[j]=num; } memset(color,-1,sizeof(color)); for (int i=1;i<=n;++i){ if (!h[i]) continue; if (color[i]==-1) if (!dfs(i,0)) {printf("No");return 0;} } for (int i=1;i<=n;++i) for (int j=i+1;j<=n;++j){ if (map[i][j]&&color[i]!=color[j]&&color[i]!=-1&&color[j]!=-1) {printf("No");return 0;} } printf("Yes\n"); for (int i=1;i<=n;++i){ if (color[i]==-1) {printf("b");continue;} if (!color[i]) printf("a");else printf("c"); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Mybatis联合查询的实现方法
下一篇:codeforces 204e Little Elephant and Strings
相关文章

 发表评论

暂时没有评论,来抢沙发吧~