Going from u to v or from v to u?
Time Limit: 2000MS   Memory Limit: 65536K
     

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

POJ Monthly--2006.02.26,zgl & twb





题意:jiajia有一个洞穴,洞穴中有n个房间,房间的连接是有方向的,jiajia想知道u与v之间是不是有路径u->v或v->u





思路:单连通问题,首先将图中的强连通的部分进行缩点,构成一颗树,接下来拓扑排序,判断拓扑排序的两点之间是不是有边,如果没有边则图不符合要求。



  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <stack>
  6. #include <queue>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. const int Max = 1100;
  12.  
  13. const int INF = 0x3f3f3f3f;
  14.  
  15. typedef struct node
  16. {
  17. int v;
  18.  
  19. int next;
  20.  
  21. }Line ;
  22.  
  23. Line Li[Max*6];
  24.  
  25. int Head[Max],top;
  26.  
  27. int Map[Max][Max];
  28.  
  29. int Du[Max],pre[Max];
  30.  
  31. int vis1[Max],dfn[Max],low[Max];
  32.  
  33. bool vis2[Max];
  34.  
  35. int dep;
  36.  
  37. int Point[Max*5][2],Num;
  38.  
  39. int topo[Max],num;
  40.  
  41. int a[Max],ToNum;
  42.  
  43. stack < int >S;
  44.  
  45. void AddEdge(int u,int v)
  46. {
  47. Li[top].v = v; Li[top].next = Head[u];
  48.  
  49. Head[u] = top++;
  50. }
  51.  
  52. int Find(int x)
  53. {
  54. return pre[x]==-1?x:pre[x] = Find(pre[x]);
  55. }
  56.  
  57. void Tarjan(int u) //强连通缩点
  58. {
  59.  
  60. dfn[u] = low[u] = dep++;
  61.  
  62. vis1[u] = 1;
  63.  
  64. S.push(u);
  65.  
  66. for(int i=Head[u];i!=-1;i=Li[i].next)
  67. {
  68. if(vis1[Li[i].v]==1)
  69. {
  70. low[u]=min(low[u],dfn[Li[i].v]);
  71. }
  72. if(vis1[Li[i].v]==0)
  73. {
  74. Tarjan(Li[i].v);
  75.  
  76. low[u]=min(low[u],low[Li[i].v]);
  77. }
  78.  
  79. if(vis2[Li[i].v])
  80. {
  81. Point[Num][0]=u;
  82.  
  83. Point[Num++][1]=Li[i].v;
  84. }
  85. }
  86.  
  87. if(low[u]==dfn[u]) //如果low[u]==dfn[u],则说明是强连通的根节点。
  88. {
  89. vis2[u]=true;
  90.  
  91. topo[num++] = u;
  92.  
  93. while(1)
  94. {
  95. if(S.empty())
  96. {
  97. break;
  98. }
  99. int v = S.top();
  100.  
  101. S.pop();
  102.  
  103. vis1[v]=2;
  104.  
  105. if(v==u)
  106. {
  107. break;
  108. }
  109. pre[v]=u;
  110.  
  111. }
  112. }
  113. }
  114.  
  115. void Toposort()//BFS拓扑排序
  116. {
  117.  
  118. queue<int>Q;
  119. for(int i=0;i<num;i++)
  120. {
  121. if(Du[topo[i]]==0)
  122. {
  123. Q.push(topo[i]);
  124.  
  125. }
  126. }
  127. while(!Q.empty())
  128. {
  129. int u=Q.front();
  130.  
  131. a[ToNum++]=u;
  132.  
  133. Q.pop();
  134.  
  135. for(int i=0;i<num;i++)
  136. {
  137. if(Map[u][topo[i]])
  138. {
  139. Du[topo[i]]--;
  140.  
  141. if(Du[topo[i]]==0)
  142. {
  143. Q.push(topo[i]);
  144. }
  145. }
  146. }
  147. }
  148. }
  149.  
  150. int main()
  151. {
  152.  
  153. int T;
  154.  
  155. int n,m;
  156.  
  157. scanf("%d",&T);
  158.  
  159. while(T--)
  160. {
  161. scanf("%d %d",&n,&m);
  162.  
  163. top = 0;
  164.  
  165. memset(Head,-1,sizeof(Head));
  166.  
  167. int u,v;
  168.  
  169. for(int i=0;i<m;i++)
  170. {
  171. scanf("%d %d",&u,&v);
  172.  
  173. AddEdge(u,v);
  174. }
  175.  
  176. memset(vis1,0,sizeof(vis1));
  177.  
  178. memset(Map,0,sizeof(Map));
  179.  
  180. memset(vis2,false,sizeof(vis2));
  181.  
  182. memset(Du,0,sizeof(Du));
  183.  
  184. memset(pre,-1,sizeof(pre));
  185.  
  186. dep = 0; Num =0 ;num = 0;
  187.  
  188. while(!S.empty())
  189. {
  190. S.pop();
  191. }
  192.  
  193. for(int i=1;i<=n;i++)
  194. {
  195. if(vis1[i]==0)
  196. {
  197. Tarjan(i);
  198. }
  199. }
  200. for(int i=0;i<Num;i++)
  201. {
  202.  
  203. int x = Find(Point[i][0]);
  204.  
  205. int y = Find(Point[i][1]);
  206.  
  207. Map[x][y]=1;
  208.  
  209. Du[y]++;
  210. }
  211.  
  212. ToNum = 0;
  213.  
  214. Toposort();
  215.  
  216. bool flag=false;
  217.  
  218. for(int i=0;i<ToNum-1;i++)
  219. {
  220. if(!Map[a[i]][a[i+1]])//判断相邻的是不是存在边
  221. {
  222. flag=true;
  223.  
  224. break;
  225. }
  226. }
  227.  
  228. if(flag)
  229. {
  230. printf("No\n");
  231. }
  232. else
  233. {
  234. printf("Yes\n");
  235. }
  236.  
  237. }
  238.  
  239. return 0;
  240. }

Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序的更多相关文章

  1. POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)

    这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...

  2. 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. ...

  3. 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 ...

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

    [题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...

  5. Java实现判断单联通(强连通缩点+拓扑排序)Going from u to v or from v to u

    Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has ...

  6. 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 ...

  7. Oracle基本数据字典:v$database、v$instance、v$version、dba_objects

    v$database: 视图结构: SQL> desc v$database; Name                                      Null?    Type - ...

  8. POJ2762 Going from u to v or from v to u(单连通 缩点)

    判断图是否单连通,先用强连通分图处理,再拓扑排序,需注意: 符合要求的不一定是链拓扑排序列结果唯一,即在队列中的元素始终只有一个 #include<cstdio> #include< ...

  9. 临时文件相关的v$tempfile v$sort_usage与V$tempseg_usage

    SQL> select username,user,segtype,segfile#,segblk#,extents,segrfno# from v$sort_usage; SEGFILE#代表 ...

随机推荐

  1. bootstrap 不兼容ie8 的问题

    官方推荐的脚手架中,其实已经包含着解决方案:html5shiv.min.js .Respond.min.js 但由于respond.js  使用 file:// 协议,IE8 是无法调起本地文件的   ...

  2. free一个无效指针

    1. 错误描述:   刚才写了一个删除单链表的结点函数, 参数是 指向链表的指针和链表中指定删除的结点的指针.  当我free这个待删除的结点, 结果报错. 2. 为什么会报错? 我查了查MSDN, ...

  3. PG, Pool之间的一些数量关系

    先说一下我的环境: Ceph cluster中包含6台OSD节点 (osd.0 - 5), 一共有10个Pool (0 - 9), 这些Pool共享了144个PG (这个数字是所有Pool的PG_SI ...

  4. Solved Unable to copy the source file ./installer/services.sh to the destination file /etc/vmware-t

    Sometimes when you intall vmwaretools there will be some problems such as "Unable to copy the s ...

  5. c# 文件遍历

    DirectoryInfo TheFolder=new DirectoryInfo(folderFullName); //遍历文件夹 foreach(DirectoryInfo NextFolder ...

  6. Android MP3录音实现

    给APP做语音功能,必须考虑到IOS和Android平台的通用性.wav录音质量高,文件太大,AAC和AMR格式在IOS平台却不支持,所以采用libmp3lame把AudioRecord音频流直接转换 ...

  7. 一个相比jdk的io包更方便处理数据读写的包

    apche的commons-io.jar包,里面有个类IOUtils,提供的下列方法: readLines方法能够从字节输入流或字符输入流里读取数据,按行读,返回字符串组成的list write方法能 ...

  8. 【C++】int、const char*、char*、char、string之间的转换

    #include "stdafx.h" #include<string> #include<vector> #include<iostream> ...

  9. 用Appium进行android自动化测试

    appium是开源的移动端自动化测试框架,可以测试ios,android应用.appium让移动端自动化测试不必限定在某种语言和某个具体的框架:也就是说任何人都可以使用自己最熟悉最顺手的语言以及框架来 ...

  10. iOS:命令行方式使用OSChina托管私有代码

    一.介绍 在项目开发中,使用版本控制工具是必不可少的开发工具,它可以帮助我们程序员写完代码后及时提交备份,防止因个人操作导致代码被误删除了或者丢失了,安全可靠.同时,使用版本控制器工具也可以很方便的进 ...