博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CSU - 1356 Catch(dfs染色两种写法,和hdu4751比较)
阅读量:4611 次
发布时间:2019-06-09

本文共 3174 字,大约阅读时间需要 10 分钟。

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 0 to N–1. 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 + 1 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 (≤ 100 000), M (≤ 500 000), 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 2 integers u and v in each line (0 ≤ 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

23 3 00 10 21 22 1 00 1

 

Sample Output

Case 1: YESCase 2: 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比较

1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 using namespace std;16 #define PI acos(-1.0)17 #define max(a,b) (a) > (b) ? (a) : (b)18 #define min(a,b) (a) < (b) ? (a) : (b)19 #define ll long long20 #define eps 1e-1021 #define MOD 100000000722 #define N 10000623 #define inf 1e1224 int n,m,s;25 vector
g[N];26 int color[N];27 bool dfs(int u,int c){28 color[u]=c;29 for(int i=0;i
View Code

 

第二种写法:

1 #include
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 #define N 100006 8 int n,m,s; 9 vector
g[N];10 int flag;11 int color[N];12 void dfs(int u,int c,int fa){13 if(color[u]!=-1){14 if(color[u]!=c){15 flag=1;16 }17 return;18 }19 if(flag) return;20 color[u]=c;21 for(int i=0;i
View Code

 

 

转载于:https://www.cnblogs.com/UniqueColor/p/5020333.html

你可能感兴趣的文章
传值方式:ajax技术和普通传值方式
查看>>
Linux-网络连接-(VMware与CentOS)
查看>>
寻找链表相交节点
查看>>
linq 学习笔记之 Linq基本子句
查看>>
[Js]布局转换
查看>>
Java annotation 自定义注释@interface的用法
查看>>
Apache Spark 章节1
查看>>
Linux crontab定时执行任务
查看>>
mysql root密码重置
查看>>
33蛇形填数
查看>>
选择排序
查看>>
SQL Server 数据库的数据和日志空间信息
查看>>
前端基础之JavaScript
查看>>
自己动手做个智能小车(6)
查看>>
自己遇到的,曾未知道的知识点
查看>>
P1382 楼房 set用法小结
查看>>
分类器性能度量
查看>>
docker 基础
查看>>
写一个bat文件,删除文件名符合特定规则,且更改日期在某
查看>>
我的友情链接
查看>>