写下第三题的解题过程:
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 from1 to n.
- For all pairs of vertices i and j, where i ≠ j, there is an edge connecting themif 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 strings, such that if Vasya used this s he would produce the given graph G.
The first line of the input contains two integers n andm — the number of vertices and edges in the graph found by Petya, respectively.
Each of the next m lines contains two integersui andvi(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.
In the first line print "Yes" (without the quotes), if the strings 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 ofs 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.
2 1 1 2
Yes aa
4 3 1 2 1 3 1 4
No
思路:
看到题目的时候发现只有A和C是不相连的,因此把这个作为一个突破口,开始构造;
首先判断了一种情况是显然不可能构造成功的, A和B不相连,A和C不相连,B和C是一定相连的,参见第一条;
然后依次构造,A显然是必须和C才会不相连的;
构造后发现有反例,看来是构造思路不全面,因此在构造后判断了一下,看构造是不是成功;
发现有别人是直接用二分图的染色来匹配的,滚去膜拜下= =
代码:
//代码冗长= = 不过好歹能AC =, =
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int data[505][505];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i,j;
memset(data,0,sizeof(data));
for(i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
data[a][b]=1;
data[b][a]=1;
}
int flag=0;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(flag)
break;
if(data[i][j]==0)
{
for(int k=j+1;k<=n;k++)
{
if(data[i][k]==0&&data[j][k]==0)
{
flag=1;
break;
}
}
}
}
}
if(flag)
{
printf("No\n");
continue;
}
char ans[505];
char wtf[505];
memset(wtf,0,sizeof(wtf));
int WA=0;
for(i=1;i<=n;i++)
{
if(!wtf[i])
{
int fh=0;
for(j=i+1;j<=n;j++)
{
if(data[i][j]==0)
{
ans[i]='a';
wtf[i]=1;
for(int k=j;k<=n;k++)
{
if(data[i][k]==0)
{
ans[k]='c';
wtf[k]=1;
}
}
break;
}
}
if(wtf[i]==0)
{
ans[i]='b';
wtf[i]=1;
}
}
else
{
if(ans[i]=='b')
continue;
else if(ans[i]=='a')
{
for(j=i+1;j<=n;j++)
{
if(data[i][j]==0&&wtf[j]==0)
{
ans[j]='c';
wtf[j]=1;
}
else if(data[i][j]==0&&wtf[j])
{
if(ans[j]=='a')
WA=1;
}
}
}
else if(ans[i]=='c')
{
for(j=i+1;j<=n;j++)
{
if(data[i][j]==0&&wtf[j]==0)
{
ans[j]='a';
wtf[j]=1;
}
else if(data[i][j]==0&&wtf[j])
{
if(ans[j]=='c')
WA=1;
}
}
}
}
}
char view[505][505];
memset(view,0,sizeof(view));
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(ans[i]=='a')
{
if(ans[j]=='a'||ans[j]=='b')
{
view[i][j]=1;
view[j][i]=1;
}
else
{
view[i][j]=0;
view[j][i]=0;
}
}
else if(ans[i]=='b')
{
view[i][j]=1;
view[j][i]=1;
}
else if(ans[i]=='c')
{
if(ans[j]=='c'||ans[j]=='b')
{
view[i][j]=1;
view[j][i]=1;
}
else
{
view[i][j]=0;
view[j][i]=0;
}
}
}
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(data[i][j]!=view[i][j])
{
WA=1;
break;
}
}
}
if(WA)
{
printf("No\n");
continue;
}
printf("Yes\n");
for(i=1;i<=n;i++)
{
printf("%c",ans[i]);
}
printf("\n");
}
return 0;
}