Codeforces 624C Graph and String

来源:互联网 发布:js 字符串转数组 编辑:程序博客网 时间:2024/06/10 10:34
 
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 graphG with the following properties:

  • G has exactly n vertices, numbered from1 to n.
  • For all pairs of vertices i and j, where i ≠ j, there is an edge connecting themif and only if characters si andsj 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 strings, 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 andm  — the number of vertices and edges in the graph found by Petya, respectively.

Each of the next m lines contains two integersui andvi(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 strings 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 ofs 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 11 2
Output
Yesaa
Input
4 31 21 31 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.



中文题意:给出一个仅有'a' 'b' 'c'构成的字符串到无向图的转换方法:对于一个长度为n的字符串以及其相对应的带有n个节点的无向图,节点I和节点j(i!=j)没有边的充要条件是字符串的第I个字符和第j个字符分别是a和c(或者c和a),否则就有边。给出一张图,问是否存在一个字符串可以转换到这张图。

具体做法:对于给定的无向图,对应的字符串需要满足:

1. 对于两个相连接的节点,他们不能是a和c的组合
2.对于两个不相互连接的节点,他们是a和c的组合。

那么。根据第二个条件,我们想到了二分图染色。

具体的,我们首先构造出原图的补图;然后从任一一个点开始染色,在染色过程中如果出现矛盾则答案为No

接着,如果这一步我们顺利完成了染色,我们还要用第一个条件去验证。

最后如果可以,我们还需要把B预处理出来(很好预处理,就是原图里度为n-1的点),然后其他点根据颜色选择A 或者C

代码:
#include<bits/stdc++.h>using namespace std;int n,m;int color[5010];//染色的颜色,-1为初始值,0 1为染色的颜色int a[5010][5010];//邻接矩阵存图vector <int> G[5010];//vector模拟邻接表,存储补图bool is_ac[5010];//判断这个点是否为Bbool colored[5010];//代表这个点是否拓展过了(是否从这个点出发前往它的相邻点了)bool ans=true;//初始化可以成功void paint(int i,int x)//将节点i染成x颜色{    if((color[i]!=-1)&&(color[i]!=x))//如果i已经有了颜色,而且不是x    {        ans=false;//肯定失败    }    color[i]=x;    if(colored[i]==true)        return ;//如果已经拓展过不用重复拓展    colored[i]=true;//设置已经拓展的tag    for(auto j:G[i])        paint(j,1-x);//dfs}int main(void){    int u,v;    cin>>n>>m;    memset(color,-1,sizeof(color));//颜色全部初始化    memset(a,-1,sizeof(a));    memset(is_ac,0,sizeof(is_ac));//都初始化为是B    memset(colored,0,sizeof(colored));    for(int i=0;i<m;i++)    {        scanf("%d%d",&u,&v);        a[u][v]=a[v][u]=1;    }    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            if(i==j)                continue;            if(a[i][j]==-1)//求补图            {                G[i].push_back(j);                is_ac[i]=is_ac[j]=true;//这些点不会是B,是a或者C            }        }    }    for(int i=1;i<=n;i++)//对所有连通域进行染色    {        if(is_ac[i]==false)//是B的话直接不用考虑            continue;        int temp_color=color[i];        if(temp_color==-1)//要是这个点的颜色还是初始的-1的话            temp_color=0;        if(colored[i]==false)//如果这个点还没有拓展过/处于不同的连通域            paint(i,temp_color);    }    if(ans)//如果染色完毕,答案为true    {        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)        {            if((a[i][j]==1)&&(color[i]+color[j]==1))//对第一个条件检查                ans=false;        }    }    if(ans)        cout<<"Yes"<<endl;    else        cout<<"No"<<endl;    if(ans)    {        for(int i=1;i<=n;i++)        {            if(is_ac[i]==false)                cout<<"b";            else if(color[i])                cout<<"a";            else                cout<<"c";        }    }    return 0;}
因为没有对第一个条件检查,我wa了很久,现在反思应该是逻辑推理问题,下次思考问题要更加注重逻辑。
0 0