http://poj.org/problem?id=2762

Going from u to v or from v to u?
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12733   Accepted: 3286

Description

In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the other. The son can either go from x to y, or from y to x. Wind promised that her tasks are all possible, but she actually doesn't know how to decide if a task is possible. To make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. Given a cave, can you tell Jiajia whether Wind can randomly choose two rooms without worrying about anything?

Input

The first line contains a single integer T, the number of test cases. And followed T cases. 
The first line for each case contains two integers n, m(0 < n < 1001,m < 6000), the number of rooms and corridors in the cave. The next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly. 

Output

The output should contain T lines. Write 'Yes' if the cave has the property stated above, or 'No' otherwise.

Sample Input

  1. 1
  2. 3 3
  3. 1 2
  4. 2 3
  5. 3 1

Sample Output

  1. Yes

Source

 
【题解】:
  
  这题题目大概意思就是说 判断两个点之间是不是能够走通,u到v 或者 v到u 都行,这题开始题目意思都理解错了,一开始以为就是一个判断强连通的题;
  先求强连通分量;
  然后将强连通分量缩点,重构图;
  强连通分量内部的点肯定是可以两两到的,所以可以不用管了;
  缩点后的图,然后进行拓扑排序,如果有两个及以上的点的入度为0,那么他们之间肯定是不可达的,所以不符合
  如图:
  
【code】:
  1. /**
  2. Judge Status:Accepted Memory:4896K
  3. Time:485MS Language:G++
  4. Code Lenght:2940B Author:cj
  5. */
  6.  
  7. #include<iostream>
  8. #include<algorithm>
  9. #include<stdio.h>
  10. #include<string.h>
  11. #include<vector>
  12. #include<stack>
  13.  
  14. using namespace std;
  15. #define N 1010
  16. #define M 6060
  17.  
  18. struct Edge
  19. {
  20. int v;
  21. int next;
  22. int u;
  23. }edge[M]; //M 开始传的N RE
  24.  
  25. int head[N],edge_cnt;//这一行变量是构造边的
  26.  
  27. int pre[N],lowlink[N],sccno[N],dfs_cnt,scc_cnt; //这两行是Tarjan算法求强连通分量用的
  28. stack<int> stk;
  29.  
  30. vector<int> G[N]; //缩点重构图用的邻接链表
  31. int in[N],mark[N][N]; //入度统计,以及重复边的标记
  32.  
  33. void init()
  34. {
  35. memset(head,-,sizeof(head));
  36. edge_cnt = ;
  37. }
  38.  
  39. void addEdge(int a,int b)
  40. {
  41. edge[edge_cnt].v = b;
  42. edge[edge_cnt].next = head[a];
  43. edge[edge_cnt].u = a; //这里记录a,重构图用,方便遍历所有边
  44. head[a] = edge_cnt++;
  45. }
  46.  
  47. void Tarjan(int u) //强连通分量
  48. {
  49. stk.push(u);
  50. pre[u] = lowlink[u] = ++dfs_cnt;
  51. int i;
  52. for(i=head[u];i!=-;i=edge[i].next)
  53. {
  54. int v = edge[i].v;
  55. if(!pre[v])
  56. {
  57. Tarjan(v);
  58. lowlink[u] = min(lowlink[u],lowlink[v]);
  59. }
  60. else if(!sccno[v])
  61. {
  62. lowlink[u] = min(lowlink[u],pre[v]);
  63. }
  64. }
  65. if(pre[u]==lowlink[u])
  66. {
  67. scc_cnt++;
  68. int x;
  69. do
  70. {
  71. x = stk.top();
  72. stk.pop();
  73. sccno[x] = scc_cnt; //sccno[x]表示下标为x的节点所在的第几个强连通分量
  74. }while(x!=u);
  75. }
  76. }
  77.  
  78. void findscc(int n)
  79. {
  80. memset(pre,,sizeof(pre));
  81. memset(lowlink,,sizeof(lowlink));
  82. memset(sccno,,sizeof(sccno));
  83. dfs_cnt = scc_cnt = ;
  84. while(!stk.empty()) //初始化 以防万一
  85. {
  86. stk.pop();
  87. }
  88. int i;
  89. for(i=;i<=n;i++) if(!pre[i]) Tarjan(i);
  90. }
  91.  
  92. void getNewMap() //重构缩点后的图,存入邻接链表G中
  93. {
  94. int i,j;
  95. for(i=;i<=scc_cnt;i++)
  96. {
  97. G[i].clear();
  98. in[i] = ;
  99. }
  100. memset(mark,,sizeof(mark));
  101. for(i=;i<edge_cnt;i++)
  102. {
  103. int v = edge[i].v;
  104. int u = edge[i].u;
  105. if(sccno[u]!=sccno[v])
  106. {
  107. if(!mark[u][v]) //重复边标记
  108. {
  109. G[sccno[u]].push_back(sccno[v]);
  110. in[sccno[v]]++; //入度统计
  111. }
  112. mark[u][v] = ;
  113. }
  114. }
  115. }
  116.  
  117. int cntInid() //计算入度为0的位置,以及是不是一个
  118. {
  119. int i,cnt=,id=;
  120. for(i=;i<=scc_cnt;i++)
  121. {
  122. if(!in[i])
  123. {
  124. cnt++;
  125. id = i;
  126. }
  127. }
  128. if(cnt==)
  129. return id;
  130. return ;
  131. }
  132.  
  133. int isOK() //用拓扑排序判断是否可行
  134. {
  135. int id = cntInid();
  136. if(!id) return ;
  137. int i;
  138. in[id] = -;
  139. for(i=;i<G[id].size();i++)
  140. {
  141. in[G[id][i]]--;
  142. }
  143. if(G[id].size()>) return isOK();
  144. return ;
  145. }
  146.  
  147. int main()
  148. {
  149. int t;
  150. scanf("%d",&t);
  151. while(t--)
  152. {
  153. int n,m;
  154. scanf("%d%d",&n,&m);
  155. int i;
  156. init();
  157. for(i=;i<m;i++)
  158. {
  159. int a,b;
  160. scanf("%d%d",&a,&b);
  161. addEdge(a,b);
  162. }
  163. findscc(n); //求强连通分量
  164. if(scc_cnt==) //如果只有一个那么肯定可行
  165. {
  166. puts("Yes");
  167. continue;
  168. }
  169. getNewMap(); //用强连通分量缩点,重构图
  170. if(isOK()) puts("Yes"); //拓扑排序判断
  171. else puts("No");
  172. }
  173. return ;
  174. }

poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)的更多相关文章

  1. poj 2553 The Bottom of a Graph(强连通分量+缩点)

    题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K ...

  2. 【强连通分量缩点】poj 1236 Network of Schools

    poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...

  3. POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)

    Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...

  4. POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...

  5. poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15812 ...

  6. POJ 2186 Popular Cows(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...

  7. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  8. poj 2186 Popular Cows (强连通分量+缩点)

    http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  9. POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序

    题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...

随机推荐

  1. Dynamic\Static\IsKinematic

    1.Dynamic: 有Collider和RigidBody的GameObject, Unity视之为Dynamic. 适用于经常变换移动的对象. 2.Static: 只含有Collider的Game ...

  2. 我是如何自学Android,资料分享

       我是如何自学Android,资料分享(2015 版) 已经完成,我的建议是先把这一篇看完,再看2015 版的.关于我在学习中开发的项目代码,已经发布在: 爱开发-源码搜索,集成了上万个App源码 ...

  3. 消息推送SignalR

    一.什么是 SignalR ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of add ...

  4. Android重力感应开发

    http://blog.csdn.net/mad1989/article/details/20848181 一.手机中常用的传感器 在Android2.3 gingerbread系统中,google提 ...

  5. 使用 EF Power Tool Code Frist 生成 Mysql 实体

    原文:使用 EF Power Tool Code Frist 生成 Mysql 实体 1,在要生成的项目上右键   2,   3,   4,   5,  生成后的效果     已知问题: 1,在Mys ...

  6. Ant 修改项目pom.xml文件应用

    <?xml version="1.0" encoding="UTF-8"?> <project name="project" ...

  7. 第二篇、倾力总结40条常见的移动端Web页面问题解决方案

    1.安卓浏览器看背景图片,有些设备会模糊.   用同等比例的图片在PC机上很清楚,但是手机上很模糊,原因是什么呢? 经过研究,是devicePixelRatio作怪,因为手机分辨率太小,如果按照分辨率 ...

  8. iOS开发——model类模板(过滤null和ID)

            说明:model类模板已默认过滤null值,附加特殊情况的关键字ID名的冲突(需手动去掉注释代码).MyMessageModel为示例的名字.可以自己随便起. 1.自己创建一个继承与N ...

  9. Linux学习三部曲(之一)

    作为.NET程序员,一直以来都是windows环境下工作,很少接触到linux系统.但是随着微软跨出跨平台这一步之后,相信.NET程序员在linux平台进行开发也会变得越来越寻常. 所以,今天这篇文章 ...

  10. (转)Web2.0 大型互联网站点的架构

    这种资料.向来可遇不可求啊 WikiPedia 技术架构学习分享 http://www.dbanotes.net/opensource/wikipedia_arch.html YouTube 的架构扩 ...