hdu 5222(Tarjan求强连通分量+dfs)
2016-07-07 16:51
375 查看
Exploration
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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%.
[align=left]Output[/align]
For each test queries, print the answer. If Miceren can do that, output "YES", otherwise "NO".
[align=left]Sample Input[/align]
2
5 2 1
1 2
1 2
4 5
4 2 2
1 2
2 3
4 3
4 1
[align=left]Sample Output[/align]
YES
NO
HintIf you need a larger stack size,
please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.
题意:给定一个既有有向边和无向边的图,问是否可以找到一个回路。
解题思路:首先把无向边看成两条来回的有向边,然后这样就可以看成是一个有向图了,用tarjan算法求强连通分量,然后再用dfs判断这个强连通块是否满足条件即可。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<stack> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int maxn = 1000005; struct Edge { int to,next; }edge[maxn<<2]; int n,m1,m2,time; int tot,head[maxn]; int dfn[maxn],low[maxn]; int block,belong[maxn]; bool Instack[maxn],vis[maxn],flag; stack<int> S; void addedge(int u,int v) { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } void Tarjan(int u) { dfn[u] = low[u] = ++time; S.push(u); Instack[u] = true; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(!dfn[v]) { Tarjan(v); low[u] = min(low[u],low[v]); } else if(Instack[v] && low[u] > dfn[v]) low[u] = dfn[v]; } if(dfn[u] == low[u]) { block++; while(true) { int v = S.top(); S.pop(); Instack[v] = false; belong[v] = block; if(u == v) break; } } } void dfs(int u,int fa) { vis[u] = true; if(flag) return; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(v == fa || belong[u] != belong[v]) continue; //重复走和不属于同一个连通块的都不符合要求 if(vis[v] == true) { flag = true; return; } else dfs(v,u); } } void solve() { time = block = 0; memset(dfn,0,sizeof(dfn)); memset(Instack,false,sizeof(Instack)); memset(vis,false,sizeof(vis)); while(!S.empty()) S.pop(); for(int i = 1; i <= n; i++) if(!dfn[i]) Tarjan(i); flag = false; for(int i = 1; i <= n; i++) if(!vis[i]) dfs(i,0); printf("%s\n",flag ? "YES":"NO"); } int main() { int t,u,v; scanf("%d",&t); while(t--) { tot = 0; memset(head,-1,sizeof(head)); scanf("%d%d%d",&n,&m1,&m2); while(m1--) { scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } while(m2--) { scanf("%d%d",&u,&v); addedge(u,v); } solve(); } return 0; }
相关文章推荐
- 初学图论-Kahn拓扑排序算法(Kahn's Topological Sort Algorithm)
- 初学图论-Bellman-Ford单源最短路径算法
- 初学图论-DAG单源最短路径算法
- 初学图论-Dijkstra单源最短路径算法
- 初学图论-Dijkstra单源最短路径算法基于优先级队列(Priority Queue)的实现
- 封装好的Folyd建图,C++源码
- LCA模板
- 图论学习笔记之一——Floyd算法
- 【LCA】SPOJ QTREE2
- poj 3249 Test for Job 最长路
- HDU 2544
- Timus 1557 Network Attack DFS+各种各种...
- HDU1289 Tarjan-模板题
- Poj2638 网络流+最短路+二分答案
- Aizu1311 分层图最短路 (...大概)
- HDU 3631 Shortest Path
- 根据图论构建可量化评估的产品设计模型-类图(Like Graph)
- 二分图匹配模板
- 最短路径 -- spfa
- POJ2377 Bad Cowtractors