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

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
  
  
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++.
 
题意:判断一个混合图是否有环;
分析:对于无向图用并查集判断,对于有向图用拓扑排序来判断。如果两个点有相同的父亲节点,那么就是有环,否则就把他们连在一起。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1000009
int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值