hdu5222 拓撲+並查集

life4711發表於2015-05-09

http://acm.hdu.edu.cn/showproblem.php?pid=5222

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++.
/**
hdu5222 拓撲+並查集
題目大意:給定一些有向邊和無向邊每個邊只能走一次,問是否有環
解題思路:
首先對於所有的無向邊,我們使用並查集將兩邊的點並起來
若一條邊未合併之前,兩端的點已經處於同一個集合了,那麼說明必定存在可行的環(因為這兩個點處於同一個並查集集合中,那麼它們之間至少存在一條路徑)
如果上一步沒有判斷出環,那麼僅靠無向邊是找不到環的
考慮到,處於同一個並查集集合中的點之間必定存在一條路徑互達,因此將一個集合的點合併之後,原問題等價於在新生成的有向圖中是否有環
我們知道,有向無環圖必定存在拓撲序,因此只需使用拓撲排序判定即可
時間複雜度O(N+M1+M2)

*/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
const int maxn=1000006;
int n,m1,m2,flag;
int par[maxn],indegree[maxn],seq[maxn];
int indeg[maxn];
void init()
{
    for(int i=0; i<=n; i++)
    {
        par[i]=i;
    }
}
int find(int x)
{
    if(par[x]==x)
        return x;
    return par[x]=find(par[x]);
}

void unite(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x==y)
    {
        flag=1;
        return;
    }
    par[x]=y;
}
int head[maxn],ip;
void init1()
{
    memset(head,-1,sizeof(head));
    ip=0;
}
struct note
{
    int v,next;
} edge[maxn];

void addedge(int u,int v)
{
    edge[ip].v=v,edge[ip].next=head[u];
    head[u]=ip++;
}

int topo()///拓撲
{
    queue<int>q;
    for(int i=1;i<=n;i++)
    {
        indeg[i]=indegree[i];
        if(indeg[i]==0)
            q.push(i);
    }
    int k=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        seq[k++]=u;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v;
            indeg[v]--;
            if(indeg[v]==0)
                q.push(v);
        }
    }
  //  printf("(%d)\n",k);
    if(k<n)return 0;
    return 1;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m1,&m2);
        flag=0;
        init();
        for(int i=0; i<m1; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(flag)continue;
            unite(u,v);
        }
        memset(indegree,0,sizeof(indegree));
        init1();
        for(int i=0; i<m2; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            u=find(u),v=find(v);
            if(v==u)flag=1;
            if(flag) continue;
            addedge(u,v);
            indegree[v]++;
        }
        if(!flag&&!topo())flag=1;
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


相關文章