HDU - 5222 Exploration (并查集+拓扑排序)

Miceren likes exploration and he found a huge labyrinth underground! 

This labyrinth has  NN 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.
InputThe first line contains a single integer  TT, indicating the number of test cases. 

Each test case begins with three integers  N, M1, M2N, M1, M2, indicating the number of caves, the number of undirectional tunnels, the number of directional tunnels. 

The next  M1M1 lines contain the details of the undirectional tunnels. Each line contains two integers  u, vu, v meaning that there is a undirectional tunnel between  u, vu, v. ( u  vu ≠ v

The next  M2M2 lines contain the details of the directional tunnels. Each line contains integers  u, vu, v meaning that there is a directional tunnel from  uu to  vv. ( u  vu ≠ v

TT is about 100. 

1  N,M1,M2  1000000.1 ≤ N,M1,M2 ≤ 1000000. 

There may be some tunnels connect the same pair of caves. 

The ratio of test cases with  N > 1000N > 1000 is less than 5%.OutputFor 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++.
        
 

题意:给你n个点,m1条无向边,m2条有向边。判断是否存在环。

思路:对于无向边,我们使用并查集,对每一次输入的两个无向边的端点查找祖先,若祖先一样,则将该边连上去之后就会形成一个环。

若在对无向边操作的过程中没有找到环,这个时候我们考虑有向边,由于使用并查集的时候,我们将一个集合压缩成了一个点(查找同一个集合的元素返回的值都是他们的祖先),所以我们将之前的无向图集合变成了点,所以说之前的无向边不会影响我们有向边的判断,然后就是拓扑排序判断有向无环图了。。。。。

附AC代码:

//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstdio>
#include<queue>
using namespace std;

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

#define up(i,j,k) for(int i=j;i<k;i++)
#define uup(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof i)
#define sfi(i) scanf("%d",&i)
#define sfl(i) scanf("%lld",&i)
#define sfd(i) scanf("%lf",&i)
#define sfc(i) scanf("%c",&i)
#define sfs(i) scanf("%s",i)
#define sf() cout<<"This is a flag!"<<endl;
#define wt(i) cout<<"This is "<<i<<endl;
#define ll long long
#define mod(x) (x)%mod
#define fre() freopen("d:\\1.txt","r",stdin)
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int MAX=1e6+50;

struct node
{
    int v,nxt;
}p[MAX];
int head[MAX];
int in[MAX];
int pre[MAX];
int find(int x)
{
    if(x!=pre[x])
        pre[x]=find(pre[x]);
    return pre[x];
}
int n,m1,m2;
int tt;
void init()
{
    tt=0;
    up(i,1,n+1)
    pre[i]=i;
    mem(head,-1);
    mem(in,0);
}

void add(int u,int v)
{
    p[tt].v=v;
    p[tt].nxt=head[u];
    head[u]=tt++;
}
bool topo()
{
    queue<int>q;
    while(!q.empty())
        q.pop();
    up(i,1,n+1)
    {
        if(in[i]==0)
            q.push(i);
    }
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        for(int i=head[temp];i!=-1;i=p[i].nxt)
        {
            in[p[i].v]--;
            if(in[p[i].v]==0)
                q.push(p[i].v);
        }
    }
    up(i,1,n+1)
    {
        if(in[i]>1)
            return false;
    }
    return true;
}
int main()
{
    int T;
    sfi(T);
    while(T--)
    {
        sfi(n);
        sfi(m1);
        sfi(m2);
        init();
        int a,b;
        bool flag=0;
        up(i,0,m1)
        {
            sfi(a);
            sfi(b);
            int fa=find(a);
            int fb=find(b);
            if(fa==fb)
            {
                flag=1;
            }
            else{
                pre[fa]=fb;
            }
        }
        up(i,0,m2)
        {
            sfi(a);
            sfi(b);
            int fa=find(a);
            int fb=find(b);
            if(fa==fb)
                flag=1;
            else if(!flag)
            {
                in[fb]++;
                add(fa,fb);
            }
        }
        if(flag)
        {
            printf("YES\n");
        }
        else
        {
            if(topo())
            {
                printf("NO\n");
            }
            else{
                printf("YES\n");
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值