Problem Description
  1. A thief is running away!
  2. 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–.
  3. 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.
  4. The cops want to know if theres some moment at which its possible for the thief to appear at any cross in the city.
 
Input
  1. The input contains multiple test cases:
  2. In the first line of the input theres an integer T which is the number of test cases. Then the description of T test cases will be given.
  3. 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.
  4. For the next M lines, there will be integers u and v in each line ( u, v < N). It means theres an undirected street between cross u and cross v.
Output
  1. For each test case, output one line to tell if theres a moment that its possible for the thief to appear at any cross. Look at the sample output for output format.
 
Sample Input
  1.  
 
Sample Output
  1. Case : YES
  2. Case : NO
Hint
  1. For the first case, just look at the table below. (YES means the thief may appear at the cross at that moment)

  1. For the second input, at any moment, theres at least one cross that the thief cant reach.
Source

转自别人的解释:

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

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

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

无向图G为二部图的充分必要条件是:
G至少有两个顶点,且其所有回路的长度均为偶数。

如果我们把图中奇数时刻能够到达的点归到X集合,偶数能到点归到Y集合,那么如果图中出现相同集合的点有 
边相连,那么就不满足二分图的性质,即可输出YES,如果原图可二分图话,答案就是NO了。

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

///

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

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

 

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

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

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

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

///

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

bfs实现染色:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<math.h>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<set>
  10. #include<bitset>
  11. #include<map>
  12. #include<vector>
  13. #include<stdlib.h>
  14. #include <stack>
  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=;i<N;i++){
  29. fa[i]=i;
  30. }
  31. }
  32. int find(int x){
  33. return fa[x]==x?x:fa[x]=find(fa[x]);
  34. }
  35. bool merge(int x,int y){
  36. int root1=find(x);
  37. int root2=find(y);
  38. if(root1==root2) return false;
  39. fa[root1]=root2;
  40. return true;
  41. }
  42. ///////////////////////////////////////////////////
  43. int n,m,s;
  44. vector<int> v[N];
  45. int color[N];
  46. bool bfs(){
  47. queue<int>q;
  48. q.push(s);
  49. color[s]=;
  50. while(!q.empty()){
  51. int t1=q.front();
  52. q.pop();
  53.  
  54. for(int i=;i<v[t1].size();i++){
  55. int t2=v[t1][i];
  56. if(color[t2]==-){
  57. if(color[t1]==){
  58. color[t2]=;
  59. }
  60. else{
  61. color[t2]=;
  62. }
  63. q.push(t2);
  64. }
  65. else{
  66. if(color[t1]==color[t2]){
  67. return true;
  68. }
  69. }
  70.  
  71. }
  72. }
  73. return false;
  74. }
  75. int main()
  76. {
  77. int t;
  78. int ac=;
  79. scanf("%d",&t);
  80. while(t--){
  81. scanf("%d%d%d",&n,&m,&s);
  82. for(int i=;i<=n;i++){
  83. v[i].clear();
  84. }
  85. init();
  86. int num=n-;
  87. for(int i=;i<m;i++){
  88. int x,y;
  89. scanf("%d%d",&x,&y);
  90. if(merge(x,y)){
  91. num--;
  92. }
  93. v[x].push_back(y);
  94. v[y].push_back(x);
  95. }
  96. memset(color,-,sizeof(color));
  97. printf("Case %d: ",++ac);
  98. if(num!=){
  99. printf("NO\n");
  100. continue;
  101. }
  102.  
  103. if(bfs()){
  104. printf("YES\n");
  105. }
  106. else{
  107. printf("NO\n");
  108. }
  109.  
  110. }
  111. return ;
  112. }

dfs实现染色:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<math.h>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<set>
  10. #include<bitset>
  11. #include<map>
  12. #include<vector>
  13. #include<stdlib.h>
  14. #include <stack>
  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=;i<N;i++){
  29. fa[i]=i;
  30. }
  31. }
  32. int find(int x){
  33. return fa[x]==x?x:fa[x]=find(fa[x]);
  34. }
  35. bool merge(int x,int y){
  36. int root1=find(x);
  37. int root2=find(y);
  38. if(root1==root2) return false;
  39. fa[root1]=root2;
  40. return true;
  41. }
  42. ///////////////////////////////////////////////////
  43.  
  44. int n,m,s;
  45. vector<int> v[N];
  46. int color[N];
  47. bool dfs(int x,int c){
  48. color[x]=c;
  49. for(int i=;i<v[x].size();i++){
  50. int y=v[x][i];
  51. if(color[y]==-){
  52. color[y]=!c;
  53. dfs(y,!c);
  54. }
  55. else{
  56. if(color[y]==color[x]){
  57. return true;
  58. }
  59. }
  60. }
  61. return false;
  62. }
  63. int main()
  64. {
  65. int ac=;
  66. int t;
  67. scanf("%d",&t);
  68. while(t--){
  69. for(int i=;i<N;i++){
  70. v[i].clear();
  71. }
  72. init();
  73. scanf("%d%d%d",&n,&m,&s);
  74. int num=n-;
  75. for(int i=;i<m;i++){
  76. int x,y;
  77. scanf("%d%d",&x,&y);
  78. if(merge(x,y)){
  79. num--;
  80. }
  81. v[x].push_back(y);
  82. v[y].push_back(x);
  83. }
  84.  
  85. printf("Case %d: ",++ac);
  86. if(num!=){
  87. printf("NO\n");
  88. continue;
  89. }
  90.  
  91. memset(color,-,sizeof(color));
  92. if(dfs(s,)){
  93. printf("YES\n");
  94. }
  95. else{
  96. printf("NO\n");
  97. }
  98.  
  99. }
  100. return ;
  101. }

hdu 3478 Catch(染色 dfs 或 bfs )的更多相关文章

  1. HDU 1241 Oil Deposits (DFS or BFS)

    链接 : Here! 思路 : 搜索判断连通块个数, 所以 $DFS$ 或则 $BFS$ 都行喽...., 首先记录一下整个地图中所有$Oil$的个数, 然后遍历整个地图, 从油田开始搜索它所能连通多 ...

  2. hdu 3478 Catch 二分图染色

    题目链接 题意 小偷逃跑,从某个点出发,每下一个时刻能够跑到与当前点相邻的点. 问是否存在某一个时刻,小偷可能在图中的任意一个点出现. 思路 结论 如果该图为连通图且不为二分图,则可能,否则不可能. ...

  3. HDU 2717 Catch That Cow (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...

  4. HDU - 3478 Catch(判奇环/二分图)

    http://acm.hdu.edu.cn/showproblem.php?pid=3478 题意 给一个无向图和小偷的起点,小偷每秒可以向相邻的点出发,问有没有一个时间点小偷可能出现在任何点. 分析 ...

  5. HDU 2717 Catch That Cow(常规bfs)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...

  6. HDU 2717 Catch That Cow(BFS)

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  7. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. 题解报告:hdu 2717 Catch That Cow(bfs)

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

  9. HDU 2102 A计划 DFS与BFS两种写法 [搜索]

    1.题意:一位公主被困在迷宫里,一位勇士前去营救,迷宫为两层,规模为N*M,迷宫入口为(0,0,0),公主的位置用'P'标记:迷宫内,'.'表示空地,'*'表示墙,特殊的,'#'表示时空传输机,走到这 ...

随机推荐

  1. hdu 1159 Common Subsequence(LCS最长公共子序列)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. 对每个用户说hello

    #!/bin/bash #对每个用户说hello #用户数 Lines=`wc -l /etc/passwd | cut -d' ' -f1` $Lines`; do echo "Hello ...

  3. JavaScript面向对象之类的创建

    JavaScript对象的定义: 在js中函数极为对象,对象分为二种:对象字变量产生的对象连接到Object.prototype:函数对象连接到Function.prototype 方法:当一个函数被 ...

  4. 【转载】视频编码(H264概述)

    一视频编码介绍 1.1 视频压缩编码的目标 1)保证压缩比例 2)保证恢复的质量 3)易实现,低成本,可靠性 1.2 压缩的出发点(可行性) 1)时间相关性 在一组视频序列中,相邻相邻两帧只有极少的不 ...

  5. 在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码

    [实例简介] 涵盖了几种常用的 webBrowser执行javascript的方法,详见示例截图以及代码 [实例截图] [核心代码] execScript方式: 1 2 3 4 5 6 7 8 9 1 ...

  6. SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.

    SpringMVC的@ResponseBody,返回json,如果有中文显示乱码的解决办法. 在SpringMVC的配置文件中 <bean class="org.springframe ...

  7. [转]Laravel 4之控制器

    Laravel 4之控制器 http://dingjiannan.com/2013/laravel-controller/ 控制器 通常Laravel控制器文件放在app/controllers/目录 ...

  8. 2013年全球ERP市场格局(Gartner)

    Gartner于5月5日公布了全球ERP市场的分析报告,报告称全球ERP软件销售额2013年整体增长了3.8%(从2012年$244亿美元到2013年$258亿美元),全球前五位ERP厂商座次例如以下 ...

  9. Apache与tomcat

    联系 1)Apache和tomcat都是web网络服务器 2)Apache是普通的服务器,本身支持html即普通网页,可以通过插件支持php也可以与Tomcat连通  (Apache单向连接tomca ...

  10. JQuery 1.3.2联动获取部门

    Sql       $(document).ready(function(){ $(".dept").bind("click", function () { v ...