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

来源:互联网 发布:铣床铣平面编程 编辑:程序博客网 时间:2024/06/06 19:01

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
原创粉丝点击