hdu5222 拓撲+並查集
http://acm.hdu.edu.cn/showproblem.php?pid=5222
Problem Description
Miceren likes exploration and he found a huge labyrinth underground!
This labyrinth has N caves and some tunnels connecting some pairs of caves.
There are two types of tunnel, one type of them can be passed in only one direction and the other can be passed in two directions. Tunnels will collapse immediately after Miceren passing them.
Now, Miceren wants to choose a cave as his start point and visit at least one other cave, finally get back to start point.
As his friend, you must help him to determine whether a start point satisfing his request exists.
This labyrinth has N caves and some tunnels connecting some pairs of caves.
There are two types of tunnel, one type of them can be passed in only one direction and the other can be passed in two directions. Tunnels will collapse immediately after Miceren passing them.
Now, Miceren wants to choose a cave as his start point and visit at least one other cave, finally get back to start point.
As his friend, you must help him to determine whether a start point satisfing his request exists.
Input
The first line contains a single integer T,
indicating the number of test cases.
Each test case begins with three integers N, M1, M2, indicating the number of caves, the number of undirectional tunnels, the number of directional tunnels.
The next M1 lines contain the details of the undirectional tunnels. Each line contains two integers u, v meaning that there is a undirectional tunnel between u, v. (u ≠ v)
The next M2 lines contain the details of the directional tunnels. Each line contains integers u, v meaning that there is a directional tunnel from u to v. (u ≠ v)
T is about 100.
1 ≤ N,M1,M2 ≤ 1000000.
There may be some tunnels connect the same pair of caves.
The ratio of test cases with N > 1000 is less than 5%.
Each test case begins with three integers N, M1, M2, indicating the number of caves, the number of undirectional tunnels, the number of directional tunnels.
The next M1 lines contain the details of the undirectional tunnels. Each line contains two integers u, v meaning that there is a undirectional tunnel between u, v. (u ≠ v)
The next M2 lines contain the details of the directional tunnels. Each line contains integers u, v meaning that there is a directional tunnel from u to v. (u ≠ v)
T is about 100.
1 ≤ N,M1,M2 ≤ 1000000.
There may be some tunnels connect the same pair of caves.
The ratio of test cases with N > 1000 is less than 5%.
Output
For each test queries, print the answer. If Miceren can do that, output "YES", otherwise "NO".
Sample Input
2
5 2 1
1 2
1 2
4 5
4 2 2
1 2
2 3
4 3
4 1
Sample Output
YES
NO
Hint
If you need a larger stack size,
please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++./**
hdu5222 拓撲+並查集
題目大意:給定一些有向邊和無向邊每個邊只能走一次,問是否有環
解題思路:
首先對於所有的無向邊,我們使用並查集將兩邊的點並起來
若一條邊未合併之前,兩端的點已經處於同一個集合了,那麼說明必定存在可行的環(因為這兩個點處於同一個並查集集合中,那麼它們之間至少存在一條路徑)
如果上一步沒有判斷出環,那麼僅靠無向邊是找不到環的
考慮到,處於同一個並查集集合中的點之間必定存在一條路徑互達,因此將一個集合的點合併之後,原問題等價於在新生成的有向圖中是否有環
我們知道,有向無環圖必定存在拓撲序,因此只需使用拓撲排序判定即可
時間複雜度O(N+M1+M2)
*/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
const int maxn=1000006;
int n,m1,m2,flag;
int par[maxn],indegree[maxn],seq[maxn];
int indeg[maxn];
void init()
{
for(int i=0; i<=n; i++)
{
par[i]=i;
}
}
int find(int x)
{
if(par[x]==x)
return x;
return par[x]=find(par[x]);
}
void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x==y)
{
flag=1;
return;
}
par[x]=y;
}
int head[maxn],ip;
void init1()
{
memset(head,-1,sizeof(head));
ip=0;
}
struct note
{
int v,next;
} edge[maxn];
void addedge(int u,int v)
{
edge[ip].v=v,edge[ip].next=head[u];
head[u]=ip++;
}
int topo()///拓撲
{
queue<int>q;
for(int i=1;i<=n;i++)
{
indeg[i]=indegree[i];
if(indeg[i]==0)
q.push(i);
}
int k=0;
while(!q.empty())
{
int u=q.front();
q.pop();
seq[k++]=u;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
indeg[v]--;
if(indeg[v]==0)
q.push(v);
}
}
// printf("(%d)\n",k);
if(k<n)return 0;
return 1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m1,&m2);
flag=0;
init();
for(int i=0; i<m1; i++)
{
int u,v;
scanf("%d%d",&u,&v);
if(flag)continue;
unite(u,v);
}
memset(indegree,0,sizeof(indegree));
init1();
for(int i=0; i<m2; i++)
{
int u,v;
scanf("%d%d",&u,&v);
u=find(u),v=find(v);
if(v==u)flag=1;
if(flag) continue;
addedge(u,v);
indegree[v]++;
}
if(!flag&&!topo())flag=1;
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
相關文章
- hdu 1811 並查集+拓撲排序並查集排序
- Codeforces-1131D:Gourmet choice(拓撲+並查集)Go並查集
- Mr. Kitayuta‘s Technology CodeForces - 505D(並查集+拓撲排序或dfs找環) 題解並查集排序
- tidb拓撲查詢工具qtidbTiDBQT
- 拓撲排序排序
- 並查集到帶權並查集並查集
- 拓撲排序,YYDS排序
- 拓撲排序模板排序
- 拓撲排序小結排序
- 圖論——拓撲排序圖論排序
- 筆記:拓撲排序筆記排序
- 【並查集】【帶偏移的並查集】食物鏈並查集
- 並查集(一)並查集的幾種實現並查集
- 並查集(小白)並查集
- 3.1並查集並查集
- 網路拓撲圖:網路拓撲圖介紹及線上製作
- Reward (圖論+拓撲排序)圖論排序
- 拓撲排序 - Topological Sort排序
- 拓撲排序核心程式碼排序
- HDU 4857 逃生(拓撲排序)排序
- AOV網與拓撲排序排序
- DFS實現拓撲排序排序
- 網路拓撲結構
- 【筆記/模板】拓撲排序筆記排序
- 並查集(Union Find)並查集
- 並查集應用並查集
- The Door Problem 並查集並查集
- 並查集練習並查集
- 並查集的使用並查集
- 並查集—應用並查集
- 寫模板, 並查集。並查集
- 並查集跳躍並查集
- 各種並查集並查集
- 拓撲排序就這麼回事排序
- HDU4857逃生(拓撲排序)排序
- 四、事務拓撲(Transactional Topolgoy)Go
- 紙上談兵: 拓撲排序排序
- poj 1094 拓撲排序排序