题目传送门

 /*
题意:无向图和有向图的混合图判环; 官方题解:首先对于所有的无向边,我们使用并查集将两边的点并起来,若一条边未合并之前,
两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为这两个点处于同一个并查集集合中,那么它们之间至少存在一条路径)
如果上一步没有判断出环,那么仅靠无向边是找不到环的
考虑到,处于同一个并查集集合中的点之间必定存在一条路径互达,因此将一个集合的点合并之后,
原问题等价于在新生成的有向图中是否有环
我们知道,有向无环图必定存在拓扑序,因此只需使用拓扑排序判定即可
时间复杂度O(N+M1+M2) 并查集+拓扑排序:并查集来判断无向图,拓扑排序判断有向图 另外:用读入外挂时间比scanf ()多,不清楚...
Accepted 5222 5007MS 45124K 2158 B G++ BH //scanf ()
Accepted 5222 7581MS 45116K 2158 B G++ BH //read ()
*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std; const int MAXN = 1e6 + ;
const int INF = 0x3f3f3f3f;
int ans[MAXN], in[MAXN];
int rt[MAXN];
vector<int> G[MAXN];
int n, m1, m2; inline int read(void)
{
int x = , f = ; char ch = getchar ();
while (ch < '' || ch > '') {if (ch == '-') f = -; ch = getchar ();}
while (ch >= '' && ch <= '') {x = x * + ch - ''; ch = getchar ();}
return x * f;
} bool TopoSort(void)
{
memset (in, , sizeof (in));
for (int i=; i<=n; ++i)
for (int j=; j<G[i].size (); ++j) in[G[i][j]]++; queue<int> Q; int cnt = ;
for (int i=; i<=n; ++i) {if (!in[i]) Q.push (i);} while (!Q.empty ())
{
int u = Q.front (); Q.pop ();
ans[++cnt] = u;
for (int j=; j<G[u].size (); ++j)
{
int v = G[u][j];
in[v]--;
if (!in[v]) Q.push (v);
}
} if (cnt == n) return false;
else return true;
} int Find(int x)
{
return (rt[x] == x) ? rt[x] : rt[x] = Find (rt[x]);
} void Union(int x, int y)
{
x = Find (x); y = Find (y);
if (x < y) rt[x] = y;
else rt[y] = x;
} bool same(int x, int y)
{
return (Find (x) == Find (y)) ? true : false;
} int main(void) //赛码 1009 Exploration
{
//freopen ("I.in", "r", stdin); int t;
scanf ("%d", &t);
while (t--)
{
scanf ("%d%d%d", &n, &m1, &m2); for (int i=; i<=n; ++i) rt[i] = i;
for (int i=; i<=n; ++i) G[i].clear (); bool ok = false; int u, v;
for (int i=; i<=m1; ++i)
{
scanf ("%d%d", &u, &v);
//u = read (); v = read ();
//G[u].push_back (v);
if (same (u, v) == true) ok = true;
else Union (u, v);
}
for (int i=; i<=m2; ++i)
{
int u, v;
scanf ("%d%d", &u, &v);
//u = read (); v = read ();
if (same (u, v) == true) ok = true;
if (ok) continue;
G[u].push_back (v);
} if (ok) {puts ("YES"); continue;}
if (TopoSort () == true) puts ("YES");
else puts ("NO");
} return ;
}

并查集+拓扑排序 赛码 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. 第10章 使用Apache服务部署静态网站

    章节简述: 本章节中通过对比目前热门的网站服务程序来说明Apache服务程序的优势,并新增主机空间选购技巧小节. 了解SELinux服务的3种工作模式,小心谨慎的使用semanage命令和setseb ...

  2. windows 程序的本体与操作系统之间的关系

    WinMain(hInst,hPrev,……) { MSG msg; RegisterClass(……); CreateWindow(……); ShowWindow(……); UpdateWindow ...

  3. Android使用OkHttp实现带进度的上传下载

    先贴上MainActivity.java package cn.edu.zafu.sample; import android.os.Bundle; import android.support.v7 ...

  4. HDU2191多重背包例题

    悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d ...

  5. PHPNG (next generation)

    PHPNG (next generation) This page gives short information about development state of a new PHP branc ...

  6. python模拟浏览器保存Cookie进行会话

    #! /usr/bin/env python # -*-coding:utf- -*- import urllib import urllib2 import cookielib class NetR ...

  7. static-const 类成员变量

    [本文链接] http://www.cnblogs.com/hellogiser/p/static-const.html [分析] const数据成员必须在构造函数初始化列表中初始化; static数 ...

  8. 71 Query Rank Min Max Successor of BST

    [本文链接] http://www.cnblogs.com/hellogiser/p/query-min-max-successor-of-bst.html [代码]  C++ Code  12345 ...

  9. 36.在字符串中删除特定的字符[Delete source from dest]

    [题目] 输入两个字符串,从第一字符串中删除第二个字符串中所有的字符.例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”. ...

  10. Linux/Ubuntu下解压命令

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压 ...