hdu 5222(Tarjan求强连通分量+dfs)

本文介绍了一个结合有向和无向边的图论问题,通过Tarjan算法寻找是否存在符合条件的回路。具体实现包括将无向边转换为双向有向边,使用Tarjan算法求解强连通分量,并通过深度优先搜索验证特定条件。

Exploration

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)


Problem Description
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.
 

Input
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%.
 

Output
For each test queries, print the answer. If Miceren can do that, output "YES", otherwise "NO".
 

Sample Input
2 5 2 1 1 2 1 2 4 5 4 2 2 1 2 2 3 4 3 4 1
 

Sample Output
YES NO
Hint
If 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值