HDU 5222 Exploration 混合图判断是否成环

Description

Miceren likes exploration and he found a huge labyrinth underground! 

This labyrinth has   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  , indicating the number of test cases. 

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

The next   lines contain the details of the undirectional tunnels. Each line contains two integers   meaning that there is a undirectional tunnel between  . (

The next   lines contain the details of the directional tunnels. Each line contains integers   meaning that there is a directional tunnel from   to  . (

 is about 100. 

 

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

The ratio of test cases with   is less than 5%.
 

Output

For each test queries, print the answer. If Miceren can do that, output "YES", otherwise "NO".
 题意:
N个点,M1个双向边,M2个单向边,问是否有环。
解题思路:
拓扑排序,并查集。
代码:
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1000100
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int rd[MAXN],fa[MAXN];
vector<int> vec[MAXN];

void init(int n)
{
    for(int i=1;i<=n;i++)
    {
        fa[i]=i;
        rd[i]=0;
        vec[i].clear();
    }
}
int find_fa(int x)
{
    int i, j=x;
    while(j != fa[j]) j = fa[j];
    while(x != j){ i=fa[x]; fa[x]=j; x=i; }
    return j;
}
int topsort(int n)
{
    queue<int> que;
    for(int i=1;i<=n;i++)
    {
        if(rd[i]==0) que.push(i);
    }
    while(!que.empty())
    {
        int u=que.front(); que.pop();
        if(rd[u]>0)
                return 1;
        for(int i=0;i<vec[u].size();i++)
        {
            rd[vec[u][i]]--;
            if(rd[vec[u][i]]==0)
                que.push(vec[u][i]);
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(rd[i]>0) return 1;
    }
    return 0;
}

int main()
{

    int tc;
    scanf("%d",&tc);
    while(tc--)
    {
        int n,m1,m2;
        scanf("%d%d%d",&n,&m1,&m2);
        init(n);
        int u,v,x,y;
        int flag=0;
        for(int i=0;i<m1;i++)
        {
            scanf("%d%d",&u,&v);
            x=find_fa(u);
            y=find_fa(v);
            if(x==y)
                flag=1;
            else 
            {
                fa[x]=y;
                //vec[u].push_back(v);    这题怪怪的,应该加上这几句吧,虽然加了就WA。
                //rd[v]++;
                //rd[u]++;
            }
        }
        for(int i=0;i<m2;i++)
        {
            scanf("%d%d",&u,&v);
            x=find_fa(u);
            y=find_fa(v);
            if(x==y)
                flag=1;
            else
            {
                vec[u].push_back(v);
                rd[v]++;
            }
        }
        if(flag)
            printf("YES\n");
        else
        {
            if(topsort(n))
                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、付费专栏及课程。

余额充值