codeforces 624C Graph and String
2016-02-05 18:52
405 查看
C. Graph and Stringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputOne 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.
InputThe first line of the input contains two integers n and m
![](http://codeforces.com/predownloaded/4a/8f/4a8fa7f2b69671ae2035b64774c0b305c1d26ad3.png)
— 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.
OutputIn 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
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个点,和m条边,并且每个点只有三种选择,abc,如果两个字母是相等或相邻则表示他们 之间是有一条边,要你从他给你的边中,找一个符合要求的字母组合。
思路:贪心,假如给你一个点他与所有的顶点都有一条边,你会从,abc中你会取哪个字母,如果存在这样的点,你选a的话,就表示取余的点中不可能有c,同理选c也一样。
所以我们会选b,那么剩下那些未确定的点只有两种可能,a c,那么只要找一个未确定的点,给他a,那么与他有边的点肯定是a,无边的点是c,最后从的得到的字串判断下符合
要求即可。
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<vector>
7 #include<queue>
8 using namespace std;
9 void bi(int x,int y);
vector<int>my[600];
queue<int>que;
char aa[600];
bool flag[600][600];
int check(int p);
int main(void)
{
int n,i,j,k,p,q;
while(scanf("%d %d",&p,&q)!=EOF)
{
for(i=0; i<600; i++)
{
my[i].clear();
}
memset(aa,0,sizeof(aa));
memset(flag,0,sizeof(flag));
int xx,yy;
for(i=0; i<q; i++)
{
scanf("%d %d",&xx,&yy);
flag[xx][yy]=true;
flag[yy][xx]=true;
my[xx].push_back(yy);
my[yy].push_back(xx);
}
for(i=1;i<=p;i++)
{
if(my[i].size()==p-1)
{
aa[i]='b';
}
}
for(i=1;i<=p;i++)
{
if(aa[i]=='\0')
{
break;
}
}
aa[i]='a';int cc=i;
if(cc!=p+1)
{for(i=1;i<=p;i++)
{
if(flag[cc][i]&&aa[i]=='\0')
{
aa[i]='a';
}
else if(aa[i]=='\0')
{
aa[i]='c';
}
}}if(check(p)==1)
{printf("Yes\n");
for(i=1;i<=p;i++)
printf("%c",aa[i]);
printf("\n");
}
else printf("No\n");
}return 0;
}
int check(int p)
{
int i,j,k,q;
for(i=1;i<=p;i++)
{
for(j=i+1;j<=p;j++)
{
if((abs(aa[i]-aa[j])<=1&&!flag[i][j])||(abs(aa[i]-aa[j])==2&&flag[i][j]))
{
return 0;
}
}
}
return 1;85 }
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.
InputThe first line of the input contains two integers n and m
![](http://codeforces.com/predownloaded/4a/8f/4a8fa7f2b69671ae2035b64774c0b305c1d26ad3.png)
— 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.
OutputIn 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 2output
Yes aainput
4 3 1 2 1 3 1 4output
NoNoteIn 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个点,和m条边,并且每个点只有三种选择,abc,如果两个字母是相等或相邻则表示他们 之间是有一条边,要你从他给你的边中,找一个符合要求的字母组合。
思路:贪心,假如给你一个点他与所有的顶点都有一条边,你会从,abc中你会取哪个字母,如果存在这样的点,你选a的话,就表示取余的点中不可能有c,同理选c也一样。
所以我们会选b,那么剩下那些未确定的点只有两种可能,a c,那么只要找一个未确定的点,给他a,那么与他有边的点肯定是a,无边的点是c,最后从的得到的字串判断下符合
要求即可。
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<vector>
7 #include<queue>
8 using namespace std;
9 void bi(int x,int y);
vector<int>my[600];
queue<int>que;
char aa[600];
bool flag[600][600];
int check(int p);
int main(void)
{
int n,i,j,k,p,q;
while(scanf("%d %d",&p,&q)!=EOF)
{
for(i=0; i<600; i++)
{
my[i].clear();
}
memset(aa,0,sizeof(aa));
memset(flag,0,sizeof(flag));
int xx,yy;
for(i=0; i<q; i++)
{
scanf("%d %d",&xx,&yy);
flag[xx][yy]=true;
flag[yy][xx]=true;
my[xx].push_back(yy);
my[yy].push_back(xx);
}
for(i=1;i<=p;i++)
{
if(my[i].size()==p-1)
{
aa[i]='b';
}
}
for(i=1;i<=p;i++)
{
if(aa[i]=='\0')
{
break;
}
}
aa[i]='a';int cc=i;
if(cc!=p+1)
{for(i=1;i<=p;i++)
{
if(flag[cc][i]&&aa[i]=='\0')
{
aa[i]='a';
}
else if(aa[i]=='\0')
{
aa[i]='c';
}
}}if(check(p)==1)
{printf("Yes\n");
for(i=1;i<=p;i++)
printf("%c",aa[i]);
printf("\n");
}
else printf("No\n");
}return 0;
}
int check(int p)
{
int i,j,k,q;
for(i=1;i<=p;i++)
{
for(j=i+1;j<=p;j++)
{
if((abs(aa[i]-aa[j])<=1&&!flag[i][j])||(abs(aa[i]-aa[j])==2&&flag[i][j]))
{
return 0;
}
}
}
return 1;85 }
相关文章推荐
- 字符串的长度 -- js
- 错误记录--“==”与“=”错误
- hdu2037 今年暑假不AC(贪心||暴力)
- Java 自定义注解详解
- UBIFS 白皮书 -- UBIFS设计简介
- 加载大图到应用内存时所需的缩放步骤
- 使用二分法猜1到100之间的一个数最多需要几次猜对
- linux yum源讲解
- SELECT、POLL/EPOLL、IOCP,以及ACE、libevent、BOOST-ASIO等
- 面向对象程序设计自学规划
- Linux CentOS安装Azure Cli工具
- 四川成都电信天邑TEWA-300AI EPON光猫各种折腾
- 为什么要用数据库
- R语言学习笔记5:数据来源与解析
- reactjs学习笔记1
- Ubuntu 14.04 LTS 下升级 gcc 到 gcc-4.9、gcc-5 版本
- 安卓开发——图片矩阵变换时将坐标原点改为图形中心点
- Android 第九天(晚上)
- 正确认识SQL Server中的模糊查询LIKE
- ssh密钥认证原理