zoj 3811||牡丹江網賽 c題 並查集

life4711發表於2014-09-08

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5343

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

解題思路:先把  沒有  攝像頭的  貨物並在一起然後按照給定的順序  來   併入有攝像頭的  貨物如果  中途出現   不能併入的,那麼就說明   沒有可能 出現給定的順序
比如  4 2 1,我併入第二個的時候  必須要藉助  1,那麼就不行了,因為 1會出現在2的前面

整體  思路就是這樣了,最後再判斷一下   所有貨物的root是否相同就可以了,如果不同的話,說明它連  聯通圖都構不成

#include<stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
vector <int> v[100005];
int x,f[100005],vis[100005];
int n,m,l,k;
void init()
{
    for(int i=0;i<100005;i++)
        f[i]=i;
}
int find(int x)
{
    if(f[x]==x)
         return x;
    return f[x]=find(f[x]);
}
void uni(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x!=y)
        f[x]=y;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(vis,0,sizeof(vis));
        memset(v,0,sizeof(v));
        scanf("%d%d%d",&n,&m,&k);
        int a,b;
        for(int i=0;i<k;i++)
        {
            scanf("%d",&a);
            vis[a]=1;
        }
        init();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            if(vis[a]==0&&vis[b]==0)
                uni(a,b);
            if(vis[a])
                v[a].push_back(b);
            if(vis[b])
                v[b].push_back(a);
        }
        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);
        }
        int flag=1;
        if(l<k)
            flag=0;
        for(int i=1;i<l;i++)
        {
            scanf("%d",&b);
            vis[b]=0;
            if(flag==0)continue;
            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=0;
        }
        for(int i=2;i<=n;i++)
            if(find(i)!=find(1))
            {
                flag=0;
                break;
            }
        if(flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}


相關文章