HDU 5222 Exploration

首先对于所有的无向边,我们使用并查集将两边的点并起来
若一条边未合并之前,两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为这两个点处于同一个并查集集合中,那
么它们之间至少存在一条路径)
如果上一步没有判断出环,那么仅靠无向边是找不到环的
考虑到,处于同一个并查集集合中的点之间必定存在一条路径互达,因此将一个集合的点合并之后,原问题等价于在新生成的有向
图中是否有环(包括自环)。。
求一下强连通分量就好了。。。

Exploration

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

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 <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define prt(k) cerr<<#k" = "<<k<<endl
#define pb push_back
#define MP make_pair
#define fi  first
#define se second
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define SZ(X) ((int)(v).size())
#define For(i,l,r) for (int i = l; i <= r; ++i)
#define Cor(i,l,r) for (int i = l; i >= r; --i)
#define Fill(a,b) memset(a,b,sizeof(a))
#define READ freopen("a.in","r",stdin);freopen("a.out","w",stdout)
void read(string t)
{
    freopen((t+".in").c_str(),"r",stdin);
    freopen((t+".out").c_str(),"w",stdout);
}
const int inf = 0x3f3f3f3f;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef pair<int,int> pii;
template <class T>
void Max(T &a, T b) { a=max(a, b); }
#pragma comment(linker, "/STACK:102400000,102400000")

const int N = 1001000;
struct Edge
{
	int v, next;
	Edge() {}
	Edge(int a, int b)
	{
		v = a, next = b;
	}
} e[N<<2];
int head[N], mm;
int n, m, m1, m2;
void init_edge()
{
	mm = 0;
	memset(head, -1, sizeof head);
}
void add(int u, int v)
{
	e[mm] = Edge(v, head[u]);
	head[u] = mm++;
}

bool flag = false;
#include <stack>
stack<int> S;
int cur, scc;
int dfn[N*3], low[N*2], in[N];
bool ins[N];
void dfs(int u)
{
	dfn[u] = low[u] = ++ cur;
	S.push(u);
	ins[u] = 1;
	for (int i = head[u]; ~i; i = e[i].next)
	{
		int v = e[i].v;
		if (dfn[v]==0) {
			dfs(v);
			low[u] = min(low[u], low[v]);
		}
		else {
			if (ins[v])
				low[u] = min(low[u], dfn[v]);
		}
	}
	if (low[u] == dfn[u] ) {
		++ scc; int v;
		do {
			v =  S.top();  S.pop();
			ins[v] = 0;
			in[v] = scc;
		} while (v - u);
	}
}
int fa[N];
int f(int x) { return x==fa[x] ? x : fa[x] = f(fa[x]); }
int C[N];
int main()
{
	int re,ca=1; cin>>re;
	while (re--)
	{
		init_edge();
		cin >> n >> m1 >> m2;
		for (int i=1;i<=n;i++) fa[i] = i;
		flag = false;
		for (int i=0;i<m1;i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			int x = f(u), y = f(v);
			if (x == y) {
				flag = true;
			}
			else fa[x] = y;
		}
		for (int i=0;i<m2;i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			u = f(u); v = f(v);
			add(u, v);
			if (u==v) flag = true;
		}
		int tot = 0;
		memset(C,0,sizeof C);
		for (int i=1;i<=n;i++) {
			int t = f(i);
			if (C[t] == 0)
			{
				C[t] = 1;
				tot++;
			}
		}

		memset(dfn, 0 , sizeof dfn);
		memset(low,0,sizeof low);
		memset(ins, 0, sizeof ins);
		memset(in, 0, sizeof in);
		cur = scc = 0;
		while (!S.empty()) S.pop();
		for (int i=1; !flag &&i<=n;i++) {
			int t = f(i);
			if (dfn[t]==0) dfs(t);
		}
		if(scc < tot) flag = true;
		if (flag) puts("YES");
		else puts("NO");
	}
	return  0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值