Description

A thief is running away!
We can consider the city where he locates as an undirected graph in which nodes stand for crosses and edges stand for streets. The crosses are labeled from to N–.
The tricky thief starts his escaping from cross S. Each moment he moves to an adjacent cross. More exactly, assume he is at cross u at the moment t. He may appear at cross v at moment t + if and only if there is a street between cross u and cross v. Notice that he may not stay at the same cross in two consecutive moment.
The cops want to know if there’s some moment at which it’s possible for the thief to appear at any cross in the city.

Input

The input contains multiple test cases:
In the first line of the input there’s an integer T which is the number of test cases. Then the description of T test cases will be given.
For any test case, the first line contains three integers N (≤ ), M (≤ ), and S. N is the number of crosses. M is the number of streets and S is the index of the cross where the thief starts his escaping.
For the next M lines, there will be integers u and v in each line ( ≤ u, v < N). It means there’s an undirected street between cross u and cross v.

Output

For each test case, output one line to tell if there’s a moment that it’s possible for the thief to appear at any cross. Look at the sample output for output format.

Sample Input


Sample Output

Case : YES
Case : NO

Hint

For the first case, just look at the table below. (YES means the thief may appear at the cross at that moment)

For the second input, at any moment, there’s at least one cross that the thief can’t reach.

题意:给出一个起始点,一些边,有人从这个起始点开始随意走,问在某一个时候,它是否可以处于任意位置。

思路:思考下,就可以明白,只要是一个联通图,并且存在奇数点形成的环,那么在某一个时候就可以处于任意位置。

如何判断存在一个奇数点形成的环?

染色法:就是给每个点进行标号,标为0,1如果存在一条边连接的两个点标号相同,那么就是存在一个奇数环......

第一种写法,和hdu4751比较

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
int n,m,s;
vector<int> g[N];
int color[N];
bool dfs(int u,int c){
color[u]=c;
for(int i=;i<g[u].size();i++){
int next=g[u][i];
if(color[next]!=-){
if(color[next]==c){
return true;
}
continue;
}
if(dfs(next,!c)) return true;
}
return false;
}
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--){
for(int i=;i<N;i++){
g[i].clear();
}
scanf("%d%d%d",&n,&m,&s);
for(int i=;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
memset(color,-,sizeof(color));
printf("Case %d: ",++ac);
if(dfs(s,)){
printf("YES\n");
}else{
printf("NO\n");
}
}
return ;
}

第二种写法:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define N 100006
int n,m,s;
vector<int>g[N];
int flag;
int color[N];
void dfs(int u,int c,int fa){
if(color[u]!=-){
if(color[u]!=c){
flag=;
}
return;
}
if(flag) return;
color[u]=c;
for(int i=;i<g[u].size();i++){
int next=g[u][i];
if(next!=fa){
dfs(next,!c,u);
}
}
}
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--){ scanf("%d%d%d",&n,&m,&s);
for(int i=;i<=n;i++){
g[i].clear();
}
for(int i=;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
memset(color,-,sizeof(color));
flag=;
dfs(s,,-);
printf("Case %d: ",++ac);
if(flag){
printf("YES\n");
}else{
printf("NO\n");
}
}
return ;
}

CSU - 1356 Catch(dfs染色两种写法,和hdu4751比较)的更多相关文章

  1. Sql语句模糊查询字符串的两种写法

    Sql语句模糊查询有两种写法,一种是在jdbcTemplate的查询方法参数里拼接字符串%,一种是在Sql语句里拼接%字符串. public class IsNameDaoImpl implement ...

  2. ORACLE 查询一个数据表后通过遍历再插入另一个表中的两种写法

    ORACLE 查询一个数据表后通过遍历再插入另一个表中的两种写法 语法 第一种: 通过使用Oracle语句块  --指定文档所有部门都能查看 declare cursor TABLE_DEPT and ...

  3. EF架构~linq模拟left join的两种写法,性能差之千里!

    回到目录 对于SQL左外连接我想没什么可说的,left join将左表数据都获出来,右表数据如果在左表中不存在,结果为NULL,而对于LINQ来说,要实现left join的效果,也是可以的,在进行j ...

  4. 运算符关键字。数据区别大小写。日期范围。判空的两种写法。NOT IN的两种写法。IN范围可含NULL,但NOT IN值范围不能含NULL。

    比较:>,<,=,>=,<=,<>(!=) 逻辑:AND,OR,NOT 范围:BETWEEN...AND... 范围:IN,NOT IN 判空:IS NULL, I ...

  5. 快速排序partition过程常见的两种写法+快速排序非递归实现

    这里不详细说明快速排序的原理,具体可参考here 快速排序主要是partition的过程,partition最常用有以下两种写法 第一种: int mypartition(vector<int& ...

  6. java 路径分隔符File.separator 以及 路径两种写法"/"和"\\"

    一.File.separator File file=new File(); 这句是新建一个文件.file.separator这个代表系统目录中的间隔符,说白了就是斜线,不过有时候需要双线,有时候是单 ...

  7. iOS中表视图单元格事件用nib和storyboard的两种写法总结

    从ios6开始,苹果公司推出了storyborad技术取代了nib的写法,这样代码量确实少写了很多,也比较简洁.但是,从学习的角度来说,阿堂认为 用nib的写法,虽然多了些代码,但是对于掌握知识和原理 ...

  8. linq和ef关于group by取最大值的两种写法

    LINQ: var temp = from p in db.jj_Credentials group p by p.ProfessionID into g select new { g.Key, Ma ...

  9. ThinkPHP中Widget的两种写法及调用

    Widget扩展一般用于页面组件的扩展,在页面根据需要输出不同的内容,下面介绍一下ThinkPHP中Widget的两种写法及调用 写法一: ArticlWidget.class.php文件: clas ...

随机推荐

  1. poj 1603 Risk_spfa向前星

    poj终于到100题,贴个代码纪念一下,hdu 到400题再贴 题意:有20个城市,接下来有19行告诉你,i城市与n个城市相连,图是双向的,然后叫你求x到y的最小经过几个城市 #include < ...

  2. Multiply Strings 大数相乘 java

    先贴上代码 public String multiply(String num1, String num2) { String str = ""; StringBuffer sb ...

  3. Pet(dfs+vector)

    Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. Codeforces 148D Bag of mice 概率dp(水

    题目链接:http://codeforces.com/problemset/problem/148/D 题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢 ...

  5. Cocos2d-x Render-NewCulling

    .cpp layout->setBackGroundImageScale9Enabled(true); layout->setBackGroundImage("green_edi ...

  6. DevExpress VCL 2014.1.2 for C++BUILDER XE6

    DevExpress VCL 2014.1.2 for C++BUILDER XE6 1)下载     DevExpress VCL 2014.1.2下载链接:http://pan.baidu.com ...

  7. js仿百度文库文档上传页面的分类选择器_第二版

    仿百度文库文档上传页面的多级联动分类选择器第二版,支持在一个页面同一时候使用多个分类选择器. 此版本号把HTML,CSS,以及图片都封装到"category.js"中.解决因文件路 ...

  8. C# 后台调用script使用类

    在网站的开发的时候,总是会用到一些前台的提示的script的代码,从项目中整理了一份常用的方法. public class Jscript { public Jscript() { // // TOD ...

  9. 第001篇——C#学习计划开启

    大年三十了,选在今天开启Blog,就是为了克服拖延症! Windows桌面程序,多年的执念,到现在一直不会写,再拖拉谁知道又要拖几年? 特此立下目标: 基本掌握C# winform 半年内可以做出一些 ...

  10. javascript中关于数组的迭代方法

    //都接受3个参数,分别为:值.在数组中的位置.数组对象本身 var num = [2, 1, 5, 4, 2, 1, 6, 8, 19]; //every:若每一项都返回true,则返回true v ...