关闭

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

标签: 题库-HDU图论-强连通分量图论-双联通分量数据结构-并查集
427人阅读 评论(0) 收藏 举报
分类:


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)

*/


1
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:264139次
    • 积分:8140
    • 等级:
    • 排名:第2451名
    • 原创:547篇
    • 转载:0篇
    • 译文:0篇
    • 评论:133条
    文章分类
    最新评论