snowy_smile的博客

还没有退役!再打一年!要进Final!有时间还会继续写题解~

【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)

*/


阅读更多
版权声明:题解中哪里写错请一定要指出来QwQ 转载还请注明下出处哦,谢谢^_^ https://blog.csdn.net/snowy_smile/article/details/49930715
上一篇【HDU5544 2015CCPC 南阳国赛E】【树上dfs找本质不同环 高斯消元 时间戳优化】Ba Gua Zhen 连通图上最大异或环
下一篇【HDU5213 BestCoder Round 39D】【莫队算法+容斥】Lucky 两个区间各选一个数使得和为K的方案数
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭