Codeforces 624C 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.

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.



中文題意:給出一個僅有'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];//判斷這個點是否爲B
bool 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了很久,現在反思應該是邏輯推理問題,下次思考問題要更加註重邏輯。
發佈了104 篇原創文章 · 獲贊 12 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章