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了。
这个时候我再判断是否满足题中图的条件。
/* ***********************************************
Author :111qqz
Created Time :2016年09月02日 星期五 15时56分14秒
File Name :code/cf/problem/623A.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=505;
7int n,m;
8bool conc[N][N];
9int degree[N];
10char ans[N];
11int main()
12{
13 #ifndef ONLINE_JUDGE
14 freopen("code/in.txt","r",stdin);
15 #endif
16 ms(degree,0);
17 ms(conc,false);
18 ms(ans,0);
19 cin>>n>>m;
20 for ( int i = 1 ; i <= m ; i++)
21 {
22 int u,v;
23 cin>>u>>v;
24 u--;
25 v--;
26 conc[u][v] = conc[v][u] = true;
27 degree[u]++;
28 degree[v]++;
29 }
1 for ( int i = 0 ; i < n ; i++) if (degree[i]==n-1) ans[i]='b'; // 字符串第一位为空的话。。。会输出空串。。。
2 for ( int i = 0 ; i < n ; i++)
3 {
4 if (!ans[i])
5 {
6 ans[i] = 'a';
7 for ( int j = 0 ; j < n ;j++)
8 if (conc[i][j]&&!ans[j]) ans[j]='a';
9 break;
10 }
1 }
2 for ( int i = 0 ; i < n ; i++) if (!ans[i]) ans[i]='c';
3 for ( int i = 0 ; i < n ; i++)
4 for ( int j = 0 ; j < n ; j++)
5 {
6 if (i==j) continue;
7 if (ans[i]=='b'||ans[j]=='b') continue;
8 if (conc[i][j]&&(ans[i]!=ans[j]))
9 {
10 puts("No");
11 return 0;
12 }
13 if (!conc[i][j]&&(ans[i]==ans[j]))
14 {
15 puts("No");
16 return 0;
17 }
18 }
19 cout<<"Yes"<<endl;
20 cout<<ans<<endl;
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}