判断图是否单连通,先用强连通分图处理,再拓扑排序,需注意:

符合要求的不一定是链
拓扑排序列结果唯一,即在队列中的元素始终只有一个

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<string>
  6. #include<algorithm>
  7. #include<map>
  8. #include<queue>
  9. #include<vector>
  10. #include<cmath>
  11. #include<utility>
  12. #include<stack>
  13. using namespace std;
  14. typedef long long LL;
  15. const int N = 1008, INF = 0x3F3F3F3F;
  16. int dfn[N],id[N];
  17. int lab,cnt;
  18. stack <int> st;
  19. int n, m;
  20. int head[N], tot;
  21. int indeg[N];
  22. vector<int> g[N];
  23. void init(){
  24. memset(head, - 1,sizeof(head));
  25. tot= 0;
  26. }
  27.  
  28. struct Edge{
  29. int to, next;
  30. }edge[20008];
  31.  
  32. void add(int u, int v){
  33. edge[tot].to = v;
  34. edge[tot].next = head[u];
  35. head[u] = tot++;
  36. }
  37.  
  38. int dfs(int u){
  39. int lowu=dfn[u]=++lab;
  40. st.push(u);
  41. for(int i = head[u];i!=-1;i=edge[i].next){
  42. int v = edge[i].to;
  43. if(!dfn[v]){
  44. int lowv = dfs(v);
  45. lowu = min(lowu, lowv);
  46. }else if(!id[v]) {
  47. lowu = min(lowu, dfn[v]);
  48. }
  49. }
  50. if(lowu == dfn[u]){
  51. cnt++;
  52. while(1){
  53. int x = st.top();
  54. st.pop();
  55. id[x] = cnt;
  56. if(x == u) break;
  57. }
  58. }
  59. return lowu;
  60. }
  61. int tarjan(){
  62. for(int i=1;i<=n;i++) {
  63. dfn[i] = id[i] = 0;
  64. }
  65. lab=cnt=0;
  66. for(int i=1;i<=n;i++) {
  67. if(!dfn[i]){
  68. dfs(i);
  69. }
  70. }
  71. return cnt;
  72. }
  73.  
  74. //符合要求的不一定是链
  75. //拓扑排序列结果唯一,即在队列中的元素始终只有一个
  76. bool topsort(int n){
  77. queue<int > q;
  78. int sum = 0;
  79. for(int i = 1; i <= n; i++){
  80. if(indeg[i] == 0){
  81. q.push(i);
  82. if(q.size() > 1){
  83. return false;
  84. }
  85. }
  86. }
  87. while(!q.empty()){
  88. int u = q.front();
  89. q.pop();
  90. sum++;
  91. for(int i= 0; i < g[u].size(); i++){
  92. int v = g[u][i];
  93. indeg[v]--;
  94. if(indeg[v] == 0){
  95. q.push(v);
  96. }
  97. }
  98. if(q.size() > 1){
  99. return false;
  100. }
  101. }
  102. if(sum != n){
  103. return false;
  104. }
  105. return true;
  106. }
  107. int main(){
  108. int t;
  109. cin>>t;
  110. while(t--){
  111. init();
  112. memset(indeg, 0, sizeof(indeg));
  113. scanf("%d %d", &n, &m);
  114. while(m--){
  115. int u, v;
  116. scanf("%d %d", &u, &v);
  117. add(u, v);
  118. }
  119. tarjan();
  120. for(int i =1; i<= cnt; i++){
  121. g[i].clear();
  122. }
  123. for(int u = 1; u <= n; u++){
  124. for(int i = head[u] ; ~i ; i = edge[i].next){
  125. int v = edge[i].to;
  126. if(id[u] != id[v]){
  127. indeg[id[v]]++;
  128. g[id[u]].push_back(id[v]);
  129. }
  130. }
  131. }
  132. if(topsort(cnt)){
  133. printf("Yes\n");
  134. }else{
  135. printf("No\n");
  136. }
  137. }
  138. return 0;
  139. }

  

POJ2762 Going from u to v or from v to u(单连通 缩点)的更多相关文章

  1. POJ2762 Going from u to v or from v to u? 强连通+缩点

    题目链接: poj2762 题意: 给出一幅单向图.问这张图是否满足   随意两点ab 都能 从a到达b 或  从b到达a 题解思路: 推断一幅图是否满足弱连通 首先想到的是将图中的 强连通分量(能互 ...

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

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

  3. [poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud     Going from u to v or from v to u? Tim ...

  4. poj2762 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: 13040 ...

  5. 【缩点+拓扑判链】POJ2762 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. Oracle基本数据字典:v$database、v$instance、v$version、dba_objects

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

  7. Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序

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

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

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

  9. [强连通分量] 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: 17089 ...

随机推荐

  1. ejs模板

    nodejs的模板引擎有很多, ejs是比较简单和容易上手的.常用的一些语法: 用<%...%>包含js代码 用<%=...%>输出变量 变量若包含 '<' '>' ...

  2. python成长之路 :线程、进程和协程

    python线程 进程与线程的历史 我们都知道计算机是由硬件和软件组成的.硬件中的CPU是计算机的核心,它承担计算机的所有任务. 操作系统是运行在硬件之上的软件,是计算机的管理者,它负责资源的管理和分 ...

  3. iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束

    http://blog.csdn.net/pucker/article/details/41843511 上一篇文章<iOS 8界面自动布局系列-1>简要介绍了iOS界面布局方式的前世今生 ...

  4. crontab命令

    前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个 ...

  5. html常用标签的使用方法

    1  html标题 <h1> to <h6> <!DOCTYPE html> <html lang="en"> <head&g ...

  6. Unity关于用LoadLevelAdditiveAsync导致新场景的Navmesh数据不正确Loading条的实践

    为了解决用Application.LoadLevelAdditiveAsync 导致新场景的Navmesh数据不正确(我们用的是4.63),我们现在loading条做法是先切到Loading的场景,然 ...

  7. C# 中的virtural和abstract

    一. Virtual 方法(虚方法) virtual 关键字用于在基类中修饰方法.virtual 的使用有两种情况: 1.在基类中定义了virtual方法,但是派生类中没有重写该虚方法,那么在对派生类 ...

  8. ios bitcode 机制对 dsym 调试文件的影响

    今天想试试用dsym和crash文件跟踪crash信息,可是一直返回如下信息: Thread name: Dispatch queue: com.apple.main-thread Thread Cr ...

  9. ACM/ICPC 之 四道MST-Prim解法(POJ1258-POJ1751-POJ2349-POJ3026)

    四道MST,适合Prim解法,也可以作为MST练习题. 题意包括在代码中. POJ1258-Agri Net 水题 //Prim-没什么好说的 //接受一个邻接矩阵,求MST //Time:0Ms M ...

  10. ACM/ICPC 之 双向链表_构造列表-模拟祖玛 (TSH OJ-Zuma(祖玛))

    这一题是TsingHua OJ上的一道题目,学堂在线的一位数据结构老师的题目(原创),所以我直接把题目先贴下来了,这道题对复习双向链表很有帮助,而且也对数据结构中List,也就是对列表的回顾也是很有帮 ...