junior19

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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


posted on 2017-03-04 17:55  junior19  阅读(129)  评论(0编辑  收藏  举报