ZOJ3811:Untrusted Patrol(并查集)

reference:http://blog.csdn.net/lvshubao1314/article/details/39134147
Untrusted Patrol

Time Limit: 3 Seconds       Memory Limit: 65536 KB

Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory.

To ensure the safety of drinks, Edward hired a security man to patrol the warehouse. The warehouse has N piles of drinks and M passageways connected them (warehouse is not big enough). When the evening comes, the security man will start to patrol the warehouse following a path to check all piles of drinks.

Unfortunately, Edward is a suspicious man, so he sets sensors on K piles of the drinks. When the security man comes to check the drinks, the sensor will record a message. Because of the memory limit, the sensors can only record for the first time of the security man's visit.

After a peaceful evening, Edward gathered all messages ordered by recording time. He wants to know whether is possible that the security man has checked all piles of drinks. Can you help him?

The security man may start to patrol at any piles of drinks. It is guaranteed that the sensors work properly. However, Edward thinks the security man may not works as expected. For example, he may digs through walls, climb over piles, use some black magic to teleport to anywhere and so on.

Input

There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:

The first line contains three integers N (1 <= N <= 100000), M (1 <= M <= 200000) and K (1 <= K <= N).

The next line contains K distinct integers indicating the indexes of piles (1-based) that have sensors installed. The following M lines, each line contains two integers Ai and Bi (1 <= AiBi<= N) which indicates a bidirectional passageway connects piles Ai and Bi.

Then, there is an integer L (1 <= L <= K) indicating the number of messages gathered from all sensors. The next line contains L distinct integers. These are the indexes of piles where the messages came from (each is among the K integers above), ordered by recording time.

Output

For each test case, output "Yes" if the security man worked normally and has checked all piles of drinks, or "No" if not.

Sample Input
2
5 5 3
1 2 4
1 2
2 3
3 1
1 4
4 5
3
4 2 1
5 5 3
1 2 4
1 2
2 3
3 1
1 4
4 5
3
4 1 2
Sample Output
No
Yes

Author:  DAI, Longao
Source:  The 2014 ACM-ICPC Asia Mudanjiang Regional First Round

题意:n个点,m条路,k个点有传感器,给出l个点,判断能否l的顺序走过整幅图,走过的路可重复。

思路:并查集,将无传感器的路并在一起,然后按顺序并入l个点,每次并入判断能否连通。

# include <iostream>
# include <cstdio>
# include <cstring>
# include <vector>
# include <algorithm>
# define MAXN 100000
using namespace std;
int pre[MAXN+1], vis[MAXN+1],road;
vector<int>v[MAXN+1];

int find(int x)
{
    if(x != pre[x])
        pre[x] = find(pre[x]);
    return pre[x];
}
void Uni(int x, int y)
{
    int px = find(x);
    int py = find(y);
    if(px != py)
    {
        pre[px] = py;
        ++road;
    }
}

int main()
{
    int t, m, n, k, l, a, b, node, x, y;
    scanf("%d",&t);
    while(t--)
    {
        road = 0;
        memset(v, 0, sizeof(v));
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1; i<=n; ++i)
        {
            pre[i] = i;
            vis[i] = 0;
        }
        for(int i=0; i<k; ++i)
        {
            scanf("%d",&node);
            vis[node] = 1;
        }
        for(int i=0; i<m; ++i)
        {
            scanf("%d%d",&x,&y);
            if(!vis[x] && !vis[y])
                Uni(x, y);
            if(vis[x])
                v[x].push_back(y);
            if(vis[y])
                v[y].push_back(x);
        }
        scanf("%d%d",&l,&a);
        vis[a] = 0;
        for(int i=0; i<v[a].size(); ++i)
            if(!vis[v[a][i]])
                Uni(v[a][i], a);
        bool flag = true;
        for(int i=1; i<l; ++i)
        {
            scanf("%d",&b);
            if(!flag) continue;
            vis[b] = 0;
            for(int j=0; j<v[b].size(); ++j)
                if(!vis[v[b][j]])
                    Uni(v[b][j], b);
            if(find(a) != find(b))
                flag = false;
        }
        if(road < n-1)//整体不连通也不行
            flag = false;
        if(flag)
            puts("Yes");
        else
            puts("No");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值