CF623A Graph and String 【题解】

题目描述

One day student Vasya was sitting on a lecture and mentioned a string s 1 s 2 . . .   s n s_{1}s_{2}...\ s_{n} , 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 s i s_{i} and s j s_{j} 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 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 u i u_{i} and v i v_{i} ( 1 < = u i , v i < = n , u i v i ) (1<=u_{i},v_{i}<=n,u_{i}≠v_{i}) — 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.

输入输出样例
输入样例#1:

2 1
1 2

输出样例#1: 复制

Yes
aa

输入样例#2:

4 3
1 2
1 3
1 4

输出样例#2:

No

说明

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.


先描述一下题意吧:

给出一个由字符串 s s 建图的过程,

  • 字符仅有 a , b , c a,b,c 三种,建出的图中第 i i 个顶点表示原来的第 i i 个字符。
  • i i j j 有连边,当且仅当 s [ i ] s[i] s [ j ] s[j] 相同或 s [ i ] s[i] s [ j ] s[j] 是相邻的字符( a a b b 相邻, b b c c 相邻)

现给出由某个字符串 s s 建出的图,构造一个字符串使其符合上面的要求要求,或者回答不存在这样的字符串。

输入:

第一行两个整数 n , m n,m ,表示节点数(即字符串长度)和边数
接下来 m m 行,每行两个数字 u i u_{i} and v i v_{i} ,表示这两个节点之间有一条边

输出:

如果字符串存在,第一行输出一个$“Yes”,第二行输出符合条件的字符串。

如果不存在,仅在第一行输出一个“No”;


讲解时间:

由题意可知, b b 与所有的点都有连边,我们很容易就可以找出 b b

反过来想,只有 a , c a,c 之间有没有连的边,那么我们把图反过来,准确的说是建原图的补图,

如果有符合条件的字符串,那这个补图一定是个二分图,那么对补图进行二分图染色就完成了。

注意一下,在补图中一条边都没有的即为点 b b ,我们并不需要对它进行处理,只在最后输出就可以啦。

代码:
#include<iostream>
#include<cstdio>
#include<ctype.h>
#include<cstring>
#include<cstdlib>
#define _ 0
using namespace std;
inline int read(){
    int x=0,f=0;char ch=getchar();
    while(!isdigit(ch))f|=ch=='-',ch=getchar();
    while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
    return f?-x:x;
}
int head[507],cnt;
struct Edge{
    int next,to;
}edge[500007];
bool ma[507][507];//用邻接矩阵存图,方便建补图
int col[507];
inline void Byebye(){printf("No\n");exit( ~~(0^_^0) );}//有点闲..
inline void add_edge(int from,int to){
    edge[++cnt].next=head[from];
    edge[cnt].to=to;head[from]=cnt;
}
bool dfs(int x){//二分图染色
    for(int i=head[x];i;i=edge[i].next){
        int to=edge[i].to;
        if(col[to]==col[x])return 0;
        if(~col[to])continue;col[to]=col[x]^1;
        if(!dfs(to))return 0;
    }
    return 1;
}
int main(){
    int n=read(),m=read();
    for(int i=1;i<=m;++i){
        int u=read(),v=read();
        ma[u][v]=ma[v][u]=1;
    }
    for(int i=1;i<=n;++i)for(int j=1;j<i;++j)//建补图
        if(!ma[i][j])add_edge(i,j),add_edge(j,i);
    memset(col,-1,sizeof col);//染色数组置为-1,最后若依旧为-1,即为b
    for(int i=1;i<=n;++i){
        if(~col[i] || !head[i])continue;
        col[i]=0;
        if(!dfs(i))Byebye();//染色基本套路,若不符合条件输出No,退出程序
    }
    for(int i=1;i<=n;++i)for(int j=1;j<i;++j)
        if(ma[i][j] && col[i]+col[j]==1)Byebye();//检查a,c之间是否有连边
    printf("Yes\n");
    for(int i=1;i<=n;++i)putchar(~col[i]?!col[i]?'a':'c':'b');//输出一种即可
    return 0;//好习惯
}

猜你喜欢

转载自blog.csdn.net/weixin_44023181/article/details/85088041