博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3478 Catch(染色 dfs 或 bfs )
阅读量:4512 次
发布时间:2019-06-08

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

Problem 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: YES Case 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.

 

 

 

Source

 

转自别人的解释:

如果出现遍历图中的某个点都是在奇数时刻或者偶数时刻,那么小偷的藏点就是根据时间判定在某些的奇数点和偶数点了。

如果图出现奇数的环,即:有一个环由奇数个点组成,那么环中的某个点在奇数和偶数时刻都能到达(可以画图试试)。其实奇数环导致小偷藏点无规律的最大原因是:

在遍历最后奇数环的两个(必定是两个)未遍历点的时候他们是同奇(偶)的,然而还有一条边直接相连。导致在下一时刻,那两个点又可以同时变成偶(奇)。如果在回溯遍历的话,就会出现整张图在奇数时刻或者偶数时刻都能到达。 

无向图G为二部图的充分必要条件是:

G至少有两个顶点,且其所有回路的长度均为偶数。

如果我们把图中奇数时刻能够到达的点归到X集合,偶数能到点归到Y集合,那么如果图中出现相同集合的点有 

边相连,那么就不满足二分图的性质,即可输出YES,如果原图可二分图话,答案就是NO了。

然后就是经典的二分图判定。

 

 

 ///

题意:一个小偷从初始点逃到他相邻的点,从某个点到另外若干个相邻的点的时间是相同的,

也就是同个时间点,问你在同个时间点小偷能否遍历全部点,能的话输出YES,否则输出NO

 

解法:DFS判断连通性+DFS染色判断是否为二分图。

如果是个二分图那么它的奇数步和偶数步是属于各自独立的集合,

如果奇数步能够遍历到偶数步,这个意思就是此小偷可以在某个时间点遍历整个图的点。

画个奇数点的环,从某个点出发一点存在一条边,同时改变改点的奇偶性。

///

 

 

总之,只要判断连通性和二分图即可

 

bfs实现染色:

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 long 20 #define eps 1e-10 21 #define MOD 1000000007 22 #define N 100006 23 #define inf 1e12 24 25 //// 26 int fa[N]; 27 void init(){ 28 for(int i=0;i
v[N]; 45 int color[N]; 46 bool bfs(){ 47 queue
q; 48 q.push(s); 49 color[s]=0; 50 while(!q.empty()){ 51 int t1=q.front(); 52 q.pop(); 53 54 for(int i=0;i
View Code

 

dfs实现染色:

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 long 20 #define eps 1e-10 21 #define MOD 1000000007 22 #define N 100006 23 #define inf 1e12 24 25 //// 26 int fa[N]; 27 void init(){ 28 for(int i=0;i
v[N]; 46 int color[N]; 47 bool dfs(int x,int c){ 48 color[x]=c; 49 for(int i=0;i
View Code

 

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

你可能感兴趣的文章
open live writer安装以及代码高亮、折叠插件安装
查看>>
消息队列
查看>>
POJ 1321 棋盘问题 dfs回溯
查看>>
org.apache.catalina.LifecycleException异常的处理
查看>>
C++转向C#的疑惑:难道C#中没有拷贝构造函数 ?[转]
查看>>
计算一个整数二进制中1的个数
查看>>
netdom join 错误:指定的域不存在,或无法联系。
查看>>
Android中Dialog的使用
查看>>
Android Activity接收Service发送的广播
查看>>
[Leetcode] Spiral Matrix | 把一个2D matrix用螺旋方式打印
查看>>
加速和监控国际网络
查看>>
【Flex】读取本地XML,然后XML数据转成JSON数据
查看>>
RecyclerView使用StaggeredGridLayoutManager布局的粘性头部实现
查看>>
Android 优雅的让Fragment监听返回键
查看>>
Android 数据库框架总结,总有一个适合你!
查看>>
Android 设置 横屏 竖屏
查看>>
Spring MVC兑现QQ第三方登录
查看>>
R类型5 R语言 数据帧
查看>>
百度云推送教程
查看>>
简单几步轻松实现在微信中直接下载APK
查看>>