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

本文介绍了一个有趣的问题:根据一个特定的图结构还原出一个由'a'、'b'、'c'组成的字符串,该字符串定义了图中节点间的连接规则。文章详细解释了解题思路,包括如何通过无边情况构建图并进行二分图染色来找到可能的字符串,并验证其正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

【题意】给定一个N≤500的无向图,现要根据图还原一个字符串s1s2...sn,字符集只有a,b,c。若(u,v)有边则abs(su−sv)≤1,无边则abs(su−sv)=2。求这个字符串,无解输出No

【解题方法】考虑无边的情况,显然(u,v)只能是(a,c)或者(c,a)。那么可以考虑把无边的单独拿出来建图,跑二分图染色,搞出一个可行解。假设a=−1,b=0,c=1,搞出可行解之后还要判断,原图与此是否矛盾,如果有边,col[u]∗col[v]<0,即是(a,c),则是矛盾的,如果无边,col[u]∗col[v]≥0,即是(b,b),(a,b),(b,c),则是矛盾的。如果没有矛盾,则此解可行,按照染色输出即可。时间复杂度O(n2+n+m)

【AC代码】

#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
int n,m,g[maxn][maxn],col[maxn];
char ss[3]={'a','b','c'};
vector<int>G[maxn];
bool dfs(int u,int c)
{
    col[u]=c;
    for(int v : G[u]){
        if(col[v]==col[u]) return false;
        if(!col[v]&&!dfs(v,-c)) return false;
    }
    return true;
}
int main()
{
    cin>>n>>m;
    memset(g,0,sizeof(g));
    memset(col,0,sizeof(col));
    for(int i=1; i<=n; i++) G[i].clear();
    for(int i=1; i<=m; i++){
        int u,v;
        cin>>u>>v;
        g[u][v]=g[v][u]=1;
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            if(i!=j&&!g[i][j]) G[i].push_back(j);
        }
    }
    bool flag=1;
    for(int i=1; i<=n; i++){
        if(!col[i]&&G[i].size()&&!dfs(i,1)){
            flag=0;
            break;
        }
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            if(i==j) continue;
            if(g[i][j]&&col[i]*col[j]<0) flag=0;
            if(!g[i][j]&&col[i]*col[j]>=0) flag=0;
        }
    }
    if(flag==0){
        puts("No");
    }
    else{
        puts("Yes");
        for(int i=1; i<=n; i++) putchar(ss[col[i]+1]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值