您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图论