图论:(Code Forces) Graph and String
2 seconds
256 megabytes
standard input
standard output
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.
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.
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.
2 1
1 2
Yes
aa
4 3
1 2
1 3
1 4
No
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.
这题特别容易错!!!!!!!!!
颠覆了我的人生观!!!!!!!!
我的OI之路啊!!!!!!!!!!
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int G[][];
int n,m;
int ans[];
bool Solve(int node)
{
bool ok=true;
for(int i=;i<=n;i++)
if(G[node][i]){
if(ans[i]){
if(ans[node]==ans[i])return false;
}
else
ans[i]=-ans[node],ok&=Solve(i);
}
return ok;
}
void print()
{
printf("Yes\n");
for(int i=;i<=n;i++)
printf("%c",'b'+ans[i]);
printf("\n");
}
int main()
{
scanf("%d%d",&n,&m);
memset(ans,,sizeof(ans));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(i!=j)
G[i][j]=;
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
G[x][y]=;
G[y][x]=;
} int flag=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(G[i][j]&&!ans[i]){
ans[i]=;
if(!Solve(i))
flag=;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(!G[i][j]&&(ans[i]-ans[j]==||ans[i]-ans[j]==-)){
flag=;
}
if(flag)
print();
else
puts("No"); return ;
}
图论:(Code Forces) Graph and String的更多相关文章
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- AIM Tech Round (Div. 2) C. Graph and String 二分图染色
C. Graph and String 题目连接: http://codeforces.com/contest/624/problem/C Description One day student Va ...
- Error Code: 1630. FUNCTION rand.string does not exist
1.错误描述 13:50:13 call new_procedure Error Code: 1630. FUNCTION rand.string does not exist. Check the ...
- AIM Tech Round (Div. 2) C. Graph and String
C. Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- codeforces 624C Graph and String
C. Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Code Forces Bear and Forgotten Tree 3 639B
B. Bear and Forgotten Tree 3 time limit per test2 seconds memory limit per test256 megabytes inputst ...
- Code Forces 26C Dijkstra?
C. Dijkstra? time limit per test 1 second memory limit per test 64 megabytes input standard input ou ...
随机推荐
- PHP利用超级全局变量$_GET来接收表单数据。
我们在linux上用 touch php_get.html php_get.php创建两个文件. php_get.html文件的代码如下: <!doctype html> <html ...
- oracle:变长数组varray,嵌套表,集合
创建变长数组类型 ) ); 这个变长数组最多可以容纳两个数据,数据的类型为 varchar2(50) 更改元素类型的大小或精度 可以更改变长数组类型和嵌套表类型 元素的大小. ALTER TYPE ...
- css 选择符
css参考手册:css.doyoe.com 在css3中,不同的浏览器可能需要不同的前缀,它表示该css属性或规则尚未成为W3C标准的一部分,是浏览器的私有属性,虽然目前较新版本的浏览器都是不需要前缀 ...
- angularjs kindEditor 中自定义按钮 弹出dialog
1.angular-kindeditor.js 第38行左右加 editorConfig.items = ["placehoder"]; 2.en.js 第234行 placeho ...
- iOS定位问题解决方案
在需要用到定位服务时,需在info文件中加入: 1.NSLocationWhenInUseUsageDescription(类型为:string,值为:”我们需要通过您的地理位置信息获取您周边的相关数 ...
- ios-pch文件的手动添加
Xcode6添加pch文件 前言:Xcode6中不在为开发者自动创建pch文件,在pch文件中我们可以添加一些琐碎的宏定义,在项目中任何地方都可以引用,加快了编译的速度 Xcode6之后的版本都是需要 ...
- 10集合:List<T>,Dictionary<K,V>
List<T>泛型集合 List<T>是C#中一种快捷.易于使用的泛型集合类型,使用泛型编程为编写面向对象程序增加了极大的效率和灵活性. 1.List<T>用法 ...
- java_设计模式_观察者模式_Observer Pattern(2016-07-27)
看了好几篇文章,最终还是觉得<Head First 设计模式>举得例子比较符合观察者模式. 观察者模式概述: 观察者模式有时被称作发布/订阅模式,它定义了一种一对多的依赖关系,让多个观察者 ...
- SGU 124.Broken line
时间限制:0.25s 空间限制:4M 题意: 给出n条线段和一个点,保证所有线段平行X轴或Y,并且闭合成一个多边形.判断这个点的位置是在多边形上,还是多边形内,还是多边形外. solution: 由于 ...
- 利用C#的反射机制动态调用DLL类库
最近由于业务要求,需要动态调用DLL类库,所以研究了一下,感觉还好也不太难,今天就把自己理解的写了一个小例子(已经通过VS2005跑通),供大家一起研究和探讨,有理解不当的地方还请高手们多多指正,谢谢 ...