HDU5222 Exploration(拓扑排序+并查集)

来源:互联网 发布:网络摄像头网址 编辑:程序博客网 时间:2024/05/17 02:38

Exploration

                                                                      Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
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
25 2 11 21 24 54 2 21 22 34 34 1
 

Sample Output
YESNO
Hint
If you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.
 
题意:判断一个混合图是否有环;
分析:对于无向图用并查集判断,对于有向图用拓扑排序来判断。如果两个点有相同的父亲节点,那么就是有环,否则就把他们连在一起。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 1000009int n,m1,m2;int fir[maxn];int u[maxn],v[maxn],nex[maxn];int e_max;int pre[maxn];int q[maxn],d[maxn];void addedge(int s,int t){    int e=e_max++;    u[e]=s;v[e]=t;    nex[e]=fir[u[e]];    fir[u[e]]=e;}int root(int x){    int k, j, r;    r = x;    while(r != pre[r])        r = pre[r];    k = x;    while(k != r)    {        j = pre[k];        pre[k] = r;        k = j;    }    return r;}bool same(int u,int v){    return  root(u)==root(v);}void merge(int u,int v){    pre[root(u)]=root(v);}bool judge(){    int f=0,r=-1;    memset(d,0,sizeof(d));    for(int e=0;e<e_max;e++)    {        d[v[e]]++;    }    for(int i=1;i<=n;i++)    {        if(d[i]==0)            q[++r]=i;    }    for(int i=0;i<n;i++)    {        if(f>r) return true;        int x=q[f++];        for(int e=fir[x];e!=-1;e=nex[e])        {            d[v[e]]--;            if(d[v[e]]==0)                q[++r]=v[e];        }    }    return false;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d",&n,&m1,&m2);        for(int i=1;i<=n;i++)        {            pre[i]=i;        }        bool ok=false;        while(m1--)        {            int u,v;            scanf("%d%d",&u,&v);            if(same(u,v)) ok=true;            merge(u,v);        }        memset(fir,-1,sizeof(fir));        e_max=0;        while(m2--)        {            int u,v;            scanf("%d%d",&u,&v);            addedge(root(u),root(v));        }        if(!ok) ok=judge();        puts(ok?"YES":"NO");    }    return 0;}


0 0