codeforces 623 A. Graph and String (构造)

题目链接:题目链接

题意:给出一个无向图,该图是通过仅包含‘a’ ‘b’ ‘c’三个字母,以规则“i,j之间有边,当且仅当s[i]和s[j]相同,或者s[i]和s[j]在字母表中相邻”(也就是只有’a’和’c’是没有边相连的)得到的,现在问能否还原这个字符串,如果能,输出任意一个解。

思路:其实就是简单构造。。。

构造的一个技巧是。。把能确定的地方先确定了。。。。

我们发现’b’比较特殊。。因为b和任意点都相连。。。

于是可以统计一下度。。。然后确定字符串中的b

然后对于某个没有确定的位置,我放置一个a,并且把所有和这个位置相连的都放成a

字符串中剩下的没有确定的位置就一定是c了。

这个时候我再判断是否满足题中图的条件。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年09月02日 星期五 15时56分14秒
 4File Name :code/cf/problem/623A.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=505;
34int n,m;
35bool conc[N][N];
36int degree[N];
37char ans[N];
38int main()
39{
40	#ifndef  ONLINE_JUDGE 
41	freopen("code/in.txt","r",stdin);
42  #endif
43	ms(degree,0);
44	ms(conc,false);
45	ms(ans,0);
46	cin>>n>>m;
47	for ( int i = 1 ; i <= m ; i++)
48	{
49	    int u,v;
50	    cin>>u>>v;
51	    u--;
52	    v--;
53	    conc[u][v] = conc[v][u] = true;
54	    degree[u]++;
55	    degree[v]++;
56	}
57
58	for ( int i = 0 ; i < n ; i++) if (degree[i]==n-1) ans[i]='b'; // 字符串第一位为空的话。。。会输出空串。。。
59	for ( int i = 0 ; i < n ; i++)
60	{
61	    if (!ans[i])
62	    {
63		ans[i] = 'a';
64		for ( int j = 0 ; j < n ;j++)
65		    if (conc[i][j]&&!ans[j]) ans[j]='a';
66		break;
67	    }
68
69	}
70	for ( int i = 0 ; i < n ; i++) if (!ans[i]) ans[i]='c';
71	for ( int i = 0 ; i < n ; i++)
72	    for ( int j = 0 ; j < n ; j++)
73	    {
74		if (i==j) continue;
75		if (ans[i]=='b'||ans[j]=='b') continue;
76		if (conc[i][j]&&(ans[i]!=ans[j]))
77		{
78		    puts("No");
79		    return 0;
80		}
81		if (!conc[i][j]&&(ans[i]==ans[j]))
82		{
83		    puts("No");
84		    return 0;
85		}
86	    }
87	cout<<"Yes"<<endl;
88	cout<<ans<<endl;
89
90  #ifndef ONLINE_JUDGE  
91  fclose(stdin);
92  #endif
93    return 0;
94}