【HDU5222 2015赛码冠军杯I】【并查集找双连通 + tarjan求强连通】Exploration 双向边只能走一边是否图上存在环


Exploration

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1349    Accepted Submission(s): 363


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++.
 

Source
 


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=1e6+10,M=4e6+10,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int n,m1,m2,x,y;
int first[N],id;
int w[M],c[M],nxt[M];
int f[N];
bool e[N],vis[N],flag;
int find(int x)
{
	return f[x]==x?x:f[x]=find(f[x]);
}
void ins(int x,int y)
{
	++id;
	w[id]=y;
	nxt[id]=first[x];
	first[x]=id;
}
bool tarjan(int x)
{
	vis[x]=1;
	e[x]=1;
	for(int z=first[x];z;z=nxt[z])
	{
		int y=w[z];
		if(vis[y]==0&&tarjan(y))return 1;
		if(e[y])return 1;
	}
	e[x]=0;
	return 0;
}
int main()
{
	scanf("%d",&casenum);
	for(casei=1;casei<=casenum;++casei)
	{
		scanf("%d%d%d",&n,&m1,&m2);
		for(int i=1;i<=n;i++)f[i]=i;
		flag=0;
		for(int i=1;i<=m1;i++)
		{
			scanf("%d%d",&x,&y);
			x=find(x);
			y=find(y);
			if(x==y)flag=1;
			else f[y]=x;
		}
		if(flag)
		{
			for(int i=1;i<=m2;i++)scanf("%*d%*d");
			printf("YES\n");
			continue;
		}
		for(int i=1;i<=n;i++)
		{
			f[i]=find(i);
			first[i]=0;
			vis[i]=0;
			e[i]=0;
		}
		for(int i=1;i<=m2;i++)
		{
			scanf("%d%d",&x,&y);
			x=f[x];
			y=f[y];
			ins(x,y);
		}
		for(int i=1;i<=n;i++)if(vis[i]==0&&tarjan(i))
		{
			flag=1;
			break;
		}
		puts(flag?"YES":"NO");
	}
	return 0;
}
/*
【trick&&吐槽】
对于问题,如果其有多个因素怎么办?简化问题,拆开考虑!

【题意】
给你一个图,有n(1e6)个点,m1(1e6)条双向边,m2(1e6)条单向边。
对于双向边,只能走某个方向,即正着走过这条边就不能再反着走。
问你这个图上,是否可以从一点走回自己。

【类型】
并查集+强连通分量

【分析】
简化问题的思想很重要!
如果只有双向边,求是否有环即可,可以用并查集实现。
如果只有单向边,求是否有强连通分量即可,可以用tarjan实现。

现在两者都有,我们该怎么办?
我们可以先对于双向边并查集找环,找到环肯定YES,找不到环也把存在双向边的缩成了一个点。
事实上,这些点之间确实相当于一个点。
于是基于这个缩点后的结果,我们再根据单向边找强连通分量,这道题就做完啦,做完啦!

【时间复杂度&&优化】
O(n+m1+m2)

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值