AIM Tech Round [div2] C. Graph and String
来源:互联网 发布:沙特王子知乎 编辑:程序博客网 时间:2024/06/03 14:23
C. Graph 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.
Sample test(s)
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.
思路
考察点与其他点的连接情况,若和其他所有点相连则为b,否则为a,并且未与其相连的为c。并且检查已经确定的点与当前的点的连接情况是否正确。遍历一遍所有点的连接情况即可
代码
#include <stdio.h>#include <algorithm>#include <math.h>using namespace std;int a[505][505];char ans[505];int main(){ int n,m,x,y,ok=1; scanf("%d %d",&n,&m); while (m--) { scanf("%d %d",&x,&y); a[x][y]=1; a[y][x]=1; } for (int i=1;i<=n;i++) { int flag=0; for (int j=1;j<=n;j++) { if (i!=j && a[i][j]==0) flag=1; } if (flag==0) ans[i]='b'; else { if (ans[i]==0) ans[i]='a'; } for (int j=1;j<=n;j++) { if (i!=j) { if (flag) { if (ans[j]==0) { if (!a[i][j]) ans[j]='a'+'c'-ans[i]; } else { if (a[i][j] && ans[j]!='b' && ans[j]!=ans[i]) ok=0; if (!a[i][j] && (ans[j]=='b' || ans[j]==ans[i])) ok=0; } } } } } if (ok) printf("Yes\n%s",ans+1); else printf("No");}
- AIM Tech Round [div2] C. Graph and String
- #AIM Tech Round [div2] C. Graph and String 【连通图、染色】
- AIM Tech Round (Div. 2) C. Graph and String
- AIM Tech Round (Div. 2) C. Graph and String
- AIM Tech Round (Div. 2)-C. Graph and String
- AIM Tech Round (Div. 2) C. Graph and String(二分图染色 | 贪心)
- AIM Tech Round (Div. 2)C - Graph and String(二分图染色)
- cf#AIM Tech Round -C. Graph and String-贪心/ 二分图染色
- Codeforces - AIM Tech Round (Div. 2)C - Graph and String(练习)
- AIM Tech Round (Div. 2) C. Graph and String 二分图染色
- Codeforces AIM Tech Round (Div. 1) A Graph and String 想法
- Codeforces AIM Tech Round (Div. 1)623A Graph and String
- Codeforces AIM-TECH round div2 624AB 623ABC
- AIM Tech Round 4 (Div. 2) C
- codeforces AIM Tech Round
- AIM Tech Round (Div. 2)--B. Making a String
- Codeforces AIM Tech Round (Div. 2) B. Making a String
- cf#AIM Tech Round -B. Making a String-贪心/set
- python学习笔记
- BZOJ 1001 & SPFA
- 回文树/回文自动机 Palindromic Tree 学习小记
- Android的minSdkVersion,targetSdkVersion,maxSdkVersion
- When the layout is set to FIXED LAYOUT, the VERTICAL ALIGNMENT is not working. As soon as changed to
- AIM Tech Round [div2] C. Graph and String
- 关系型数据库是如何运作的(下)
- 【POJ3261】Milk Patterns【后缀数组】【二分】
- getDay
- hdu acm 1853 Cyclic Tour
- Linux制作通用运行jar运行文件
- BZOJ 1067 & Interval_Tree
- O(N2)的三种排序-- 冒泡、选择、插入(java代码)
- sprintf函数的用法