题目传送门

  1. /*
  2. 题意:无向图和有向图的混合图判环;
  3. 官方题解:首先对于所有的无向边,我们使用并查集将两边的点并起来,若一条边未合并之前,
  4. 两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为这两个点处于同一个并查集集合中,那么它们之间至少存在一条路径)
  5. 如果上一步没有判断出环,那么仅靠无向边是找不到环的
  6. 考虑到,处于同一个并查集集合中的点之间必定存在一条路径互达,因此将一个集合的点合并之后,
  7. 原问题等价于在新生成的有向图中是否有环
  8. 我们知道,有向无环图必定存在拓扑序,因此只需使用拓扑排序判定即可
  9. 时间复杂度O(N+M1+M2)
  10. 并查集+拓扑排序:并查集来判断无向图,拓扑排序判断有向图
  11. 另外:用读入外挂时间比scanf ()多,不清楚...
  12. Accepted 5222 5007MS 45124K 2158 B G++ BH //scanf ()
  13. Accepted 5222 7581MS 45116K 2158 B G++ BH //read ()
  14. */
  15. #include <cstdio>
  16. #include <cmath>
  17. #include <cstring>
  18. #include <string>
  19. #include <iostream>
  20. #include <algorithm>
  21. #include <queue>
  22. #include <vector>
  23. #pragma comment(linker, "/STACK:102400000,102400000")
  24. using namespace std;
  25. const int MAXN = 1e6 + ;
  26. const int INF = 0x3f3f3f3f;
  27. int ans[MAXN], in[MAXN];
  28. int rt[MAXN];
  29. vector<int> G[MAXN];
  30. int n, m1, m2;
  31. inline int read(void)
  32. {
  33. int x = , f = ; char ch = getchar ();
  34. while (ch < '' || ch > '') {if (ch == '-') f = -; ch = getchar ();}
  35. while (ch >= '' && ch <= '') {x = x * + ch - ''; ch = getchar ();}
  36. return x * f;
  37. }
  38. bool TopoSort(void)
  39. {
  40. memset (in, , sizeof (in));
  41. for (int i=; i<=n; ++i)
  42. for (int j=; j<G[i].size (); ++j) in[G[i][j]]++;
  43. queue<int> Q; int cnt = ;
  44. for (int i=; i<=n; ++i) {if (!in[i]) Q.push (i);}
  45. while (!Q.empty ())
  46. {
  47. int u = Q.front (); Q.pop ();
  48. ans[++cnt] = u;
  49. for (int j=; j<G[u].size (); ++j)
  50. {
  51. int v = G[u][j];
  52. in[v]--;
  53. if (!in[v]) Q.push (v);
  54. }
  55. }
  56. if (cnt == n) return false;
  57. else return true;
  58. }
  59. int Find(int x)
  60. {
  61. return (rt[x] == x) ? rt[x] : rt[x] = Find (rt[x]);
  62. }
  63. void Union(int x, int y)
  64. {
  65. x = Find (x); y = Find (y);
  66. if (x < y) rt[x] = y;
  67. else rt[y] = x;
  68. }
  69. bool same(int x, int y)
  70. {
  71. return (Find (x) == Find (y)) ? true : false;
  72. }
  73. int main(void) //赛码 1009 Exploration
  74. {
  75. //freopen ("I.in", "r", stdin);
  76. int t;
  77. scanf ("%d", &t);
  78. while (t--)
  79. {
  80. scanf ("%d%d%d", &n, &m1, &m2);
  81. for (int i=; i<=n; ++i) rt[i] = i;
  82. for (int i=; i<=n; ++i) G[i].clear ();
  83. bool ok = false; int u, v;
  84. for (int i=; i<=m1; ++i)
  85. {
  86. scanf ("%d%d", &u, &v);
  87. //u = read (); v = read ();
  88. //G[u].push_back (v);
  89. if (same (u, v) == true) ok = true;
  90. else Union (u, v);
  91. }
  92. for (int i=; i<=m2; ++i)
  93. {
  94. int u, v;
  95. scanf ("%d%d", &u, &v);
  96. //u = read (); v = read ();
  97. if (same (u, v) == true) ok = true;
  98. if (ok) continue;
  99. G[u].push_back (v);
  100. }
  101. if (ok) {puts ("YES"); continue;}
  102. if (TopoSort () == true) puts ("YES");
  103. else puts ("NO");
  104. }
  105. return ;
  106. }

并查集+拓扑排序 赛码 1009 Exploration的更多相关文章

  1. HDU 1811:Rank of Tetris(并查集+拓扑排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=1811 Rank of Tetris Problem Description   自从Lele开发了Rating系 ...

  2. hdu 1811Rank of Tetris (并查集 + 拓扑排序)

    /* 题意:这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B,等于B ...

  3. 【并查集+拓扑排序】【HDU1811】【Rank of Tetris】

    题意:给你3种关系 A=B,A>B,A<B 问是否排名方式唯一,或者存在矛盾 解 1.读入数据先处理 =号 用并查集的祖先作为代表元素,其他儿子节点都等于跟这个点重叠. 再读入 '< ...

  4. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  5. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

  6. HDU 1811 Rank of Tetris(并查集+拓扑排序 非常经典)

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. HDU 1811(并查集+拓扑排序)题解

    Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球.为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他 ...

  8. HDU 5222 ——Exploration——————【并查集+拓扑排序判有向环】

    Exploration Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. HDU——1272小希的迷宫(并查集+拓扑排序)

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

随机推荐

  1. dedecms删除没有文章的标签

    要批量的删除织梦TAG标签,那我们就只能在数据库里做修改了. 登录数据库,在数据库里执行以下SQL语句: delete FROM dede_tagindex where typeid not in ( ...

  2. Linux简单的常用命令——纯手打(慢慢积累)

    ==============linux下快捷键==================ctrl+insert 复制shift +insert 粘贴 输入文件名的前三个字母,按tab键自动补全文件名 在vi ...

  3. SVN安装与配置 SVN整合MyEclipse

    SVN安装: 1.安装服务器 ######### 安装文件:SVN服务器############### # http://www.collab.net/downloads/subversion # C ...

  4. Metasploit是一款开源的安全漏洞检测工具,

    Metasploit是一款开源的安全漏洞检测工具,可以帮助安全和IT专业人士识别安全性问题,验证漏洞的缓解措施,并管理专家驱动的安全性进行评估,适合于需要核实漏洞的安全专家,同时也适合于强大进攻能力的 ...

  5. #define 的一些用法 以及 迭代器的 [] 与 find()函数的区别

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

  6. C++ virtual descructor

    [代码1]  C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546 ...

  7. object-c学习笔记

    原文地址 最近开始学习object-c,分享一下学习oc的经验以及对oc的理解,其中难免会有错误,请大家理解. 对初学者来说,objective-c存在了很多令人费解的写法,当然也包括我! 我刚开始看 ...

  8. css3学习总结1--CSS3选择器

    CSS3的属性选择器主要包括以下几种: 1. E[attr^="value"]:指定了属性名,并且有属性值,属性值是以value开头的: 2. E[attr$="valu ...

  9. Google map测量工具

    启用Google map的测量工具,测量图上的距离. 打开Google map,左下角看到Google 地图实验室,点开,然后enable相应功能即可. 通过Google map lab还能使我们能够 ...

  10. 核电站问题(codevs 2618)

    题目描述 Description 一个核电站有N个放核物质的坑,坑排列在一条直线上.如果连续M个坑中放入核物质,则会发生爆炸,于是,在某些坑中可能不放核物质. 任务:对于给定的N和M,求不发生爆炸的放 ...