HDOJ 5222 Exploration 并查集+拓扑排序 找环

Exploration

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

Total Submission(s): 715    Accepted Submission(s): 197

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

赛码"BestCoder"杯中国大学生程序设计冠军赛

/* ***********************************************
Author        :CKboss
Created Time  :2015年05月07日 星期四 22时48分45秒
File Name     :HDOJ5222.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

#pragma comment(linker, "/STACK:102400000,102400000") 

using namespace std;

const int maxn=1001000;

int fa[maxn];

int find(int x)
{
	if(fa[x]==x) return x;
	return fa[x]=find(fa[x]);
}

bool Union(int a,int b)
{
	a=find(a); b=find(b);
	if(a!=b)
	{
		fa[a]=b; return true;
	}
	return false;
}

int n,m1,m2;

struct Edge
{
	int to,next;
}edge[2*maxn];

int Adj[maxn],Size;
int degree[maxn];
bool vis[maxn];

void init()
{
	for(int i=0;i<=n+10;i++) fa[i]=i;
	memset(Adj,-1,sizeof(Adj)); Size=0;
	memset(degree,0,sizeof(degree));
	memset(vis,false,sizeof(vis));
}

void Add_Edge(int u,int v)
{
	edge[Size].to=v;
	edge[Size].next=Adj[u];
	Adj[u]=Size++;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d%d%d",&n,&m1,&m2);
		init();
		bool flag=false;
		for(int i=0;i<m1;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			if(Union(u,v)==false) flag=true;
		}

		for(int i=0;i<m2;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			if(flag) continue;
			u=find(u); v=find(v);
			if(u!=v)
			{
				/// u--->v
				Add_Edge(v,u);
				degree[u]++;
			}
			else flag=true;
		}

		if(flag)
		{
			puts("YES"); continue;
		}

		queue<int> q;

		for(int i=1;i<=n;i++)
		{
			int u=find(i);
			if(vis[u]) continue;
			if(degree[u]==0)
			{
				q.push(u);
				vis[u]=true;
			}
		}

		while(!q.empty())
		{
			int u=q.front(); q.pop();
			u=find(u);
			for(int i=Adj[u];~i;i=edge[i].next)
			{
				int v=edge[i].to;
				v=find(v);
				degree[v]--;
				if(degree[v]==0)
				{
					q.push(v);
					vis[v]=true;
				}
			}
		}

		bool ck=true;
		for(int i=1;i<=n&&ck;i++)
		{
			if(vis[find(i)]==false) ck=false;
		}

		if(ck) puts("NO");
		else puts("YES");
	}

    return 0;
}
时间: 2024-10-09 02:12:08

HDOJ 5222 Exploration 并查集+拓扑排序 找环的相关文章

HDU 1811:Rank of Tetris(并查集+拓扑排序)

http://acm.hdu.edu.cn/showproblem.php?pid=1811 Rank of Tetris Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球.为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜,定时更新,名堂要比福布斯富豪榜还响.关于如何排名,这个不用说都知道是根据Rating从高到低来排,如果两个人具有相同的Rating,那就按

hdu 1811Rank of Tetris (并查集 + 拓扑排序)

1 /* 2 题意:这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B,等于B,小于B. 3 4 现在Lele并不是让你来帮他制作这个高手榜,他只是想知道,根据这些信息是否能够确定出这个高手榜,是的话就输出"OK". 5 否则就请你判断出错的原因,到底是因为信息不完全(输出"UNCERTAIN"),还是因为这些信息中包含冲突(输出&quo

hdu--1811--并查集&amp;&amp;拓扑排序&lt;好题&gt;

做了这题 绝逼 累啊.. mle -- re<stack overflow>--tle--wa---ac 经过这么5步 终于AC了 这题 我觉得可以让你更好地来 理解 拓扑排序的一些细节问题 首先 这题 为什么要用到并查集呢? 因为 会有 A = B这种情况的出现 然后可能再来个 B =C A = D....那么我们就需要将它们全部表示成一个点 那么就是都用一个根结点来表示 然后 这边 是要判断 能不能根据给出的条件 形成一个排列 那么就是个 拓扑问题 根据 > <情况来判断 我觉

【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环

[题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一个简单环,则欲删除的边一定经过该环.尝试环上的每一条边(至多n条边)后再次拓扑排序判断全图是否有环. 拓扑排序后定位到简单环:剩余图是环+环内DAG,DFS过程中将走入死路的点标-1,访问过标1,找到访问过的点就是简单环.换起始点直到找到环为止. 复杂度O(nm). #include<cstdio>

HDU 5222 ——Exploration——————【并查集+拓扑排序判有向环】

Exploration Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 194    Accepted Submission(s): 63 Problem Description Miceren likes exploration and he found a huge labyrinth underground! This la

hdoj-1811-Rank of Tetris【并查集+拓扑排序】

Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6763 Accepted Submission(s): 1901 Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子

POJ 3660Cow Contest(并查集+拓扑排序)

Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7567   Accepted: 4206 Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others

hdu 1811 Rank of Tetris 【并查集+拓扑排序】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1811 分析: 很明显是道拓扑排序的题,有一点就是处理实力相等的问题: 可以用并查集把实力相等的组成一个集合. 说一下拓扑排序的性质: 1.如果入度为0的点大于1,则排序不唯一 2.如果排序的总数小于给定的数,则存在环路 献上代码: #include<stdio.h> #include<string.h> #include<algorithm> #include<ios

Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[],使得两个数组中最大的数尽量小 题解 按照偏序表,构造出从小到大的拓扑图 如何解决相等的数的偏序关系? 用并查集缩点后再进行拓扑排序 如何解决最大的数最小? 只需要使得同一层的数相同就行,可以一批处理栈中的元素,对于一批栈中的元素产生的新点,先放进一个容器里,然后等到这批栈清空了,再把这个容器中的点