hdu5222(拓扑排序+并查集)
来源:互联网 发布:淘宝上下架工具 编辑:程序博客网 时间:2024/06/06 07:16
Exploration
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1416 Accepted Submission(s): 388
Problem Description
Miceren likes exploration and he found a huge labyrinth underground!
This labyrinth hasN 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
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 integersN, M1, M2 , indicating the number of caves, the number of undirectional tunnels, the number of directional tunnels.
The nextM1 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 nextM2 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 withN > 1000 is less than 5%.
Each test case begins with three integers
The next
The next
There may be some tunnels connect the same pair of caves.
The ratio of test cases with
Output
For each test queries, print the answer. If Miceren can do that, output "YES", otherwise "NO".
Sample Input
25 2 11 21 24 54 2 21 22 34 34 1
Sample Output
YESNOHintIf you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.
此题题意:有双向桥和单向桥,走过每座桥之后该桥都会塌,问该图里面是否存在回路(就是所谓的环)
分析:
由于有双向桥,用并查集来处理双向桥的话,如果一条没有连接的边的两个点的父亲节点相同说明肯定有通路= =这是第一种情况。
然后就是剩下的单向桥,因为将双向桥用并查集连接起来了,所以接下来就相当于查找单相桥是否存在有向回路,判断有向回路的话就直接拓扑排序来查看是否能进行拓扑排序就ok!
#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <stdio.h>#include <stdlib.h>#include<string.h>#include<algorithm>#include<math.h>#include<queue>using namespace std;typedef long long ll;const int maxn=1000010,M=1000010;int head[maxn],ip,rd[maxn],f[maxn];int x[M],y[M];char c[M];struct data{ int v,next;}tu[M];void init(int n){ memset(head,-1,sizeof(head)); ip=0; for(int i=0;i<=n;i++)///注意这里的等号!!! f[i]=i,rd[i]=0,head[i]=-1;}void add(int u,int v){ tu[ip].v=v,tu[ip].next=head[u],head[u]=ip++;}int fa(int x){ if(f[x]!=x) f[x]=fa(f[x]); return f[x];}int s;void topu(int n){ queue<int>q; for(int i=1;i<=n;i++)///注意容易错!从0开始还是从1! { if(!rd[i]) {q.push(i);} } s=0; while(!q.empty()) { int tem=q.front(); q.pop(); s++; for(int i=head[tem];i!=-1;i=tu[i].next) { int hh=tu[i].v; rd[hh]--; if(!rd[hh]) q.push(hh); } } //printf("%d\n",s); if(s<n) printf("YES\n"); else printf("NO\n");}int main(){ int t; cin>>t; while(t--) { int n,m1,m2; cin>>n>>m1>>m2; init(n); int flag=0; for(int i=0;i<m1;i++) { int x,y; scanf("%d%d",&x,&y); if(fa(x)!=fa(y)) f[fa(x)]=fa(y); else flag=1; } for(int i=0;i<m2;i++) { int x,y; scanf("%d%d",&x,&y); if(!flag) { x=fa(x),y=fa(y); add(x,y); rd[y]++; } } if(flag) printf("YES\n"); else topu(n); } return 0;}
0 0
- hdu5222(拓扑排序+并查集)
- hdu5222 Exploration【并查集+拓扑排序】
- HDU5222 Exploration(拓扑排序+并查集)
- hdu5222 拓扑+并查集
- hdu1811(并查集+拓扑排序)
- 并查集&拓扑排序 (hdu1811)
- 拓扑排序+并查集(MB hud 1811)
- hdu 1811拓扑排序+并查集(容器实现)
- ACMclub - 1122 关系推断 (拓扑排序, 并查集)
- hdu 1181(并查集+拓扑排序)
- POJ 3660Cow Contest(并查集+拓扑排序)
- 【HDU】5222 Exploration(并查集+拓扑排序)
- hdu 5222 Exploration(并查集+拓扑排序)
- HDU1811 Rank of Tetris(并查集+拓扑排序)
- hdu5438 Ponds(并查集、拓扑排序)
- hdu 5438 Ponds(拓扑排序+并查集)
- HDU 5438 Ponds(拓扑排序+并查集)
- HDU 5438(拓扑排序+并查集)
- linux添加系统调用总结(内核版本4.4.4)
- Python爬虫学习笔记(2)-单线程爬虫
- 【spark】采用LogisticRegression(ML API篇)对MNIST的0-1数字进行识别
- JavaScript 原型概念深入理解
- HDU5131(模拟)
- hdu5222(拓扑排序+并查集)
- Debian下iceweasel(FireFox)缓存目录下的视频文件
- Python爬虫学习笔记(3)-XPath与多线程爬虫
- bzoj 3625 小朋友和二叉树 多项式开根
- UI学习第09天
- POJ 2362 Square (搜索 + 剪枝)
- uboot中常见烧写命令总结
- Java中的stringbuffer和String类:
- GPRS-SIM900模块应用程序——双线程支持发短信接打电话