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)

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

HintIf 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&&吐槽】

【题意】

【类型】

【分析】

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

*/

1
0

【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐（算法+实战）--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：264139次
• 积分：8140
• 等级：
• 排名：第2451名
• 原创：547篇
• 转载：0篇
• 译文：0篇
• 评论：133条
文章分类
最新评论