HDOJ 5222 Exploration 并查集+拓扑排序 找环
来源:互联网 发布:临床研究中心网络构架 编辑:程序博客网 时间:2024/05/22 04:35
Exploration
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 715 Accepted Submission(s): 197
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++.
Source
赛码"BestCoder"杯中国大学生程序设计冠军赛
/* ***********************************************Author :CKbossCreated Time :2015年05月07日 星期四 22时48分45秒File Name :HDOJ5222.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>#pragma comment(linker, "/STACK:102400000,102400000") using namespace std;const int maxn=1001000;int fa[maxn];int find(int x){if(fa[x]==x) return x;return fa[x]=find(fa[x]);}bool Union(int a,int b){a=find(a); b=find(b);if(a!=b) {fa[a]=b; return true;}return false;}int n,m1,m2;struct Edge{int to,next;}edge[2*maxn];int Adj[maxn],Size;int degree[maxn];bool vis[maxn];void init(){for(int i=0;i<=n+10;i++) fa[i]=i;memset(Adj,-1,sizeof(Adj)); Size=0;memset(degree,0,sizeof(degree));memset(vis,false,sizeof(vis));}void Add_Edge(int u,int v){edge[Size].to=v;edge[Size].next=Adj[u];Adj[u]=Size++;}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T_T;scanf("%d",&T_T);while(T_T--){scanf("%d%d%d",&n,&m1,&m2);init();bool flag=false;for(int i=0;i<m1;i++){int u,v;scanf("%d%d",&u,&v);if(Union(u,v)==false) flag=true;}for(int i=0;i<m2;i++){int u,v;scanf("%d%d",&u,&v);if(flag) continue;u=find(u); v=find(v);if(u!=v) {/// u--->vAdd_Edge(v,u);degree[u]++;}else flag=true;}if(flag){puts("YES"); continue;}queue<int> q;for(int i=1;i<=n;i++){int u=find(i);if(vis[u]) continue;if(degree[u]==0){q.push(u);vis[u]=true;}}while(!q.empty()){int u=q.front(); q.pop();u=find(u);for(int i=Adj[u];~i;i=edge[i].next){int v=edge[i].to;v=find(v);degree[v]--;if(degree[v]==0) {q.push(v);vis[v]=true;}}}bool ck=true;for(int i=1;i<=n&&ck;i++){if(vis[find(i)]==false) ck=false;}if(ck) puts("NO");else puts("YES");} return 0;}
0 0
- HDOJ 5222 Exploration 并查集+拓扑排序 找环
- hdu5222 Exploration【并查集+拓扑排序】
- 【HDU】5222 Exploration(并查集+拓扑排序)
- hdu 5222 Exploration(并查集+拓扑排序)
- HDU5222 Exploration(拓扑排序+并查集)
- hdoj-1811-拓扑排序+并查集
- hdoj 1811 拓扑排序+并查集
- hdoj-1811-Rank of Tetris【并查集+拓扑排序】
- hdoj 1811 Rank of Tetris【拓扑排序 + 并查集】
- hdoj 1811 Rank of Tetris ( 拓扑排序&并查集 )
- hdu Exploration (并查集 拓扑 强连通)
- Mr. Kitayuta's Technology CodeForces - 505D(并查集+拓扑排序或dfs找环) 题解
- HDOJ 1811 Rank of Tetris (拓扑排序+并查集)
- HDOJ 1811 Rank of Tetris(拓扑排序+并查集)
- hdu 1811 并查集 + 拓扑排序
- HDU1811 并查集+拓扑排序
- hdu 1811 拓扑排序+并查集
- hdu 1811 拓扑排序,查并集
- HDU3065 病毒侵袭持续中 AC自动机
- Action如何接收jsp页面form表单提交的数据?
- 把一个数组里的数组合全部列出
- Android Studio最新版1.2百度云下载镜像
- java 反射
- HDOJ 5222 Exploration 并查集+拓扑排序 找环
- xxx
- UDP网络通讯编程
- 解决.gitignore无法忽略的文件以及自身被忽略的方法
- 一次完整的HTTP事务是怎样一个过程?
- 兼容安卓和iOS:Windows丧失平台优势
- C++动态二维数组内存的分配和释放 opencv
- sd老师讲课的佛为服务了健康
- 使用vs2005 C语言调用lua脚本实现hello,world