题目大意:

为了锻炼自己的儿子 Jiajia 和Wind 把自己的儿子带入到一个洞穴内,洞穴有n个房间,洞穴的道路是单向的。
每一次Wind 选择两个房间  x 和 y,   让他的儿子从一个房间走到另一个房间,(要么从 x->y  或者 y->x), Wind承诺这个是一定可以走到的。但是他不知道如何判断这个 xy一定是互通的,现在给你一个洞穴,问随机给你两个洞穴的编号,是否是相通的。
题目分析:题目意思有点偏移,其实意思是(只要能从x->y  或者 y->x 这个都算是通的), 求一下TopSort,判断能否直接连成一串。
(集训前恶补了一下数据结构,再写TopSort的时候拿以前笔记,看一下就懂了了。)
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <algorithm>
  7. #include <vector>
  8. usingnamespace std;
  9. #define INF 0x7ffffff
  10. #define maxn 1005
  11. typedef longlong LL;
  12. #define Min(a,b) (a<b?a:b)
  13. #define MOD 1000000007
  14. int m, n, Time, top, ans;
  15. int Stack[maxn], dfn[maxn], low[maxn], P[maxn][maxn], blocks[maxn], In[maxn];
  16. bool InStack[maxn];
  17. vector<vector<int> > G;
  18. void init()
  19. {
  20. memset(In, 0, sizeof(In));
  21. memset(dfn, 0, sizeof(dfn));
  22. memset(low, 0, sizeof(low));
  23. memset(P, 0, sizeof(P));
  24. ans = Time = top = 0;
  25. G.clear();
  26. G.resize(n+2);
  27. }
  28. bool TopSort()
  29. {
  30. int cnt = 0;
  31. top = 0;
  32. for(int i=0; i<ans; i++)
  33. {
  34. if(In[i] == 0)
  35. {
  36. Stack[top ++] = i;
  37. cnt ++;
  38. break;
  39. }
  40. }
  41.  
  42. while(top)
  43. {
  44. int v = Stack[--top];
  45. for(int i=0; i<ans; i++)
  46. {
  47. if(P[v][i] && ! --In[i])
  48. {
  49. Stack[top++] = i;
  50. cnt ++;
  51. }
  52. }
  53. }
  54. return cnt == ans;
  55. }
  56.  
  57. void Tarjan(int u)
  58. {
  59. dfn[u] = low[u] = ++Time;
  60. Stack[top++] = u;
  61. InStack[u] = true;
  62. int len = G[u].size(), v;
  63.  
  64. for(int i=0; i<len; i++)
  65. {
  66. v = G[u][i];
  67. if( !low[v] )
  68. {
  69. Tarjan(v);
  70. low[u] = min(low[u], low[v]);
  71. }
  72. elseif( InStack[v] )
  73. low[u] = min(low[u], dfn[v]);
  74. }
  75. if(low[u] == dfn[u])
  76. {
  77. do
  78. {
  79. v = Stack[--top];
  80. InStack[v] = false;
  81. blocks[v] = ans;
  82. }
  83. while(u != v);
  84. ans ++;
  85. }
  86. }
  87.  
  88. void solve()
  89. {
  90. for(int i=1; i<=n; i++)
  91. {
  92. if(!low[i])
  93. Tarjan(i);
  94. }
  95.  
  96. for(int i=1; i<=n; i++)
  97. {
  98. int len = G[i].size(), v;
  99. for(int j=0; j<len; j++)
  100. {
  101. v = G[i][j];
  102. if(blocks[i] != blocks[v])
  103. {
  104. int a = blocks[i], b = blocks[v];
  105. P[a][b] ++;
  106. if(P[a][b] == 1)
  107. {
  108. In[b] ++;
  109. break;
  110. }
  111.  
  112. }
  113. }
  114. }
  115. if( !TopSort() )
  116. printf("No\n");
  117. else
  118. puts("Yes");
  119. }
  120.  
  121. int main()
  122. {
  123. int T;
  124. scanf("%d", &T);
  125. while(T--)
  126. {
  127. scanf("%d %d",&n, &m);
  128. init();
  129. while(m --)
  130. {
  131. int a, b;
  132. scanf("%d %d",&a, &b);
  133. G[a].push_back(b);
  134. }
  135. solve();
  136. }
  137. return0;
  138. }

POJ 2762 Going from u to v or from v to u?(强联通 + TopSort)的更多相关文章

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

  2. poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)

    http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit:  ...

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

    职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...

  4. POJ 2762 Going from u to v or from v to u? (判断单连通)

    http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...

  5. [ tarjan + dfs ] poj 2762 Going from u to v or from v to u?

    题目链接: http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory L ...

  6. POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)

    id=2762">http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS ...

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

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

  9. POJ 2762 Going from u to v or from v to u? Tarjan算法 学习例题

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17104   Accepted: 4594 Description In o ...

随机推荐

  1. Android开发报错系列(一),java.lang.NullPointerException,at android.widget.ListView.setupChild

    问题描述:运行代码是报空指针错误,java.lang.NullPointerException,at Android.widget.ListView.setupChild 问题定位:listview控 ...

  2. EXCEL插件

    http://www.cnblogs.com/brooks-dotnet/category/233027.html http://www.360doc.com/content/15/0713/00/1 ...

  3. wpf提示背景,资源样式

    查找资源时多用element.TryFindResource() <TextBox FontSize="17" Height="26" Margin=&q ...

  4. win 10 安装 mysql解压版 步骤

    参考资料:win 10 安装 mysql 5.7 网址:http://blog.sina.com.cn/s/blog_5f39af320102wbk0.html 本文参考上面的网址的教程,感谢作者分享 ...

  5. Quartz Quick Start Guide

    Welcome to the QuickStart guide for Quartz. As you read this guide, expect to see details of: Downlo ...

  6. Java中View游戏开发框架

    java中游戏开发引擎View比较适合被动触发的游戏,不能使用于那种对战的游戏 Game01Activity.java  这里是调用的activity package cn.sun.syspro; i ...

  7. WPF FindName()查找命名注册的元素

    一.查找xaml中命名注册的元素 <Button x:Name="btn1" Content="显示内容" HorizontalAlignment=&qu ...

  8. Creating a web application.

    About creating web GIS applications As you learn and use ArcGIS for Server, you'll probably reach th ...

  9. .NET 4.6

    http://referencesource.microsoft.com/ DownLoad 下载原代码

  10. 在Iframe框架下如何跳转到登录界面

    在Iframe框架下跳转到登录界面总会跳到子界面中,类似于下图 试用Respon.Redirect()不行, 用Js函数,但我跳转代码都是写在cs文件中的,用Respose.write(),js函数根 ...