HDU 5222 Exploration 混合图判断是否成环
来源:互联网 发布:铣床铣平面编程 编辑:程序博客网 时间:2024/06/06 19:01
Description
Miceren likes exploration and he found a huge labyrinth underground!
This labyrinth has 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 , indicating the number of test cases.
Each test case begins with three integers , indicating the number of caves, the number of undirectional tunnels, the number of directional tunnels.
The next lines contain the details of the undirectional tunnels. Each line contains two integers meaning that there is a undirectional tunnel between . ( )
The next lines contain the details of the directional tunnels. Each line contains integers meaning that there is a directional tunnel from to . ( )
is about 100.
There may be some tunnels connect the same pair of caves.
The ratio of test cases with 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".
题意:
N个点,M1个双向边,M2个单向边,问是否有环。
解题思路:
拓扑排序,并查集。
代码:
#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#pragma comment(linker, "/STACK:102400000,102400000")#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 1000100#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int rd[MAXN],fa[MAXN];vector<int> vec[MAXN];void init(int n){ for(int i=1;i<=n;i++) { fa[i]=i; rd[i]=0; vec[i].clear(); }}int find_fa(int x){ int i, j=x; while(j != fa[j]) j = fa[j]; while(x != j){ i=fa[x]; fa[x]=j; x=i; } return j;}int topsort(int n){ queue<int> que; for(int i=1;i<=n;i++) { if(rd[i]==0) que.push(i); } while(!que.empty()) { int u=que.front(); que.pop(); if(rd[u]>0) return 1; for(int i=0;i<vec[u].size();i++) { rd[vec[u][i]]--; if(rd[vec[u][i]]==0) que.push(vec[u][i]); } } for(int i=1;i<=n;i++) { if(rd[i]>0) return 1; } return 0;}int main(){ int tc; scanf("%d",&tc); while(tc--) { int n,m1,m2; scanf("%d%d%d",&n,&m1,&m2); init(n); int u,v,x,y; int flag=0; for(int i=0;i<m1;i++) { scanf("%d%d",&u,&v); x=find_fa(u); y=find_fa(v); if(x==y) flag=1; else { fa[x]=y; //vec[u].push_back(v); 这题怪怪的,应该加上这几句吧,虽然加了就WA。 //rd[v]++; //rd[u]++; } } for(int i=0;i<m2;i++) { scanf("%d%d",&u,&v); x=find_fa(u); y=find_fa(v); if(x==y) flag=1; else { vec[u].push_back(v); rd[v]++; } } if(flag) printf("YES\n"); else { if(topsort(n)) printf("YES\n"); else printf("NO\n"); } } return 0;}
0 0
- HDU 5222 Exploration 混合图判断是否成环
- hdu 5222 Exploration (dfs)
- HDU 5222 Exploration
- hdu 5222 Exploration
- hdu 5222 Exploration
- hdu 5222 Exploration
- HDU Exploration (DFS判断环,以及重边可算的处理方法)
- poj 1637 判断混合图是否存在欧拉回路
- poj1637 判断一个混合图是否存在欧拉回路
- HDU 2444 判断是否为二分图
- hdu 1268(判断图是否强联通)
- HDU 5222 - Exploration (强连通 + 并查集)
- 【HDU】5222 Exploration(并查集+拓扑排序)
- hdu 5222 Exploration(并查集+拓扑排序)
- 【并查集+强连通】 HDU 5222 Exploration
- HDU 5222 Exploration 并查集+强连通分量
- poj 1673(Sightseeing tour)(判断混合图是否存在欧拉回路)
- POJ 1637 - Sightseeing tour 判断混合图是否是欧拉回路(最大流)
- 我每天都在github上做些什么
- ubuntu软件卸载
- iOS-跟着标哥微博&学习导航头像缩放
- codeforces-702A-Maximum Increase
- 机器学习:HMM:算法
- HDU 5222 Exploration 混合图判断是否成环
- Linux -- 正则表达式
- string使用
- redis 基础知识
- C语言#define的用法
- Linux环境下VI/VIM编辑文件时无权限保存的解决方法
- 【整理】JS中getElementsByName()方法
- 02-即时通讯-XMPP 简单介绍
- 《算法导论》第一章_读书笔记