hdu5222 拓扑+并查集

20 篇文章 0 订阅
8 篇文章 0 订阅

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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值