赛码"BestCoder"杯中国大学生程序设计冠军赛1009——邻接表+并查集——Exploration...

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++.

 大意:判一个图是否有环,对于无向图来说用并查集来判,对于有向图来说用topo来判,如果这两个点有相同的父亲结点的话就说明是一个环,否则就把他们join一起,topo用邻接表实现比模拟链表方便~~复习了并查集的三个函数

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int MAX = 1e6 + 10;
int ans[MAX],in[MAX];
int n,m1,m2;
int p[MAX];
vector<int> G[MAX];
int find(int x)
{
    return (x == p[x])? x:p[x] = find(p[x]);
}
void join(int x,int y)
{
  int fx = find(x);
  int fy = find(y);
  if(fx != fy)
      p[fx] = fy;
}
int judge(int x,int y)
{
    return find(x) == find(y) ? 1 :0;
}

int topo()
{
 memset(in,0,sizeof(in));
 for(int i = 1; i <= n ; i++)
     for(int j = 0 ; j < G[i].size();j++)
         in[G[i][j]]++;
 queue<int> q;
 while(!q.empty())
     q.pop();
 int cnt = 1;
 for(int i = 1; i <= n ; i++)
     if(!in[i]) 
        q.push(i);
while(!q.empty()){
    int u = q.front();
    q.pop();
    ans[cnt++] = u;
    for(int i = 0; i < G[u].size();i++){
        int v = G[u][i];
        in[v]--;
        if(!in[v]) 
            q.push(v);
    }
}
if(cnt == n ) return 0;
else return 1;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&n,&m1,&m2);
        for(int i = 1; i <= n ; i++)
            p[i] = i;
        for(int i = 1; i <= n ; i++)
            G[i].clear();
    int flag = 0;
    int u,v;
    for(int i = 1; i <= m1 ; i++){
        scanf("%d%d",&u,&v);
        if(judge(u,v) == 1) {
            flag = 1;
           break;       
        }
        else    join(u,v);
    }
    for(int i = 1; i <= m2; i++){
        scanf("%d%d",&u,&v);
        if(judge(u,v) == 1){
            flag = 1;
            break;
        }
        if(flag == 1)
            break;
        G[u].push_back(v);
    }
    if(flag == 1) {printf("YES\n");continue;}
    if(topo() == 1) {
        printf("NO\n");
    }
    else printf("YES\n");
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/zero-begin/p/4479324.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值