码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 3818 Untrusted Patrol(dsf+并査集+邻接表)

时间:2014-09-09 13:27:39      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:style   color   os   io   ar   strong   for   art   div   

ZOJ Problem Set - 3811
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 <= Ai, Bi <= 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



题意:就是给你n个点,m条边,其中k个点有摄像头(只能摄到第一次出现的),然后给x个摄像头摄到人的排序,问这个人能不能,成功的把所有的点满足上面要求成功访问到所有的点


直接dfs肯定会超时,现在就是要把已经走到的点变成一个集合,这个事仿照别人写的,待会贴上自己的代码



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010

int father[N],vis[N];
int head[N],n,m,k;
int a[N];
int ans;
int num;

struct stud{
int to,next;
}e[N*4];

void build(int u,int v)
{
    e[num].to=v;
    e[num].next=head[u];
    head[u]=num;
    num++;
}

int cha(int x)
{
    if(x!=father[x])
        father[x]=cha(father[x]);
    return father[x];
}

int uin(int x,int y)
{
    int xx=cha(x);
    int yy=cha(y);
    
    if(xx<yy)
        father[xx]=yy;
    else
        father[yy]=xx;

    return xx==yy;
}

void dfs(int x)
{
     int i;
     for(i=head[x];i!=-1;i=e[i].next)
     {
         int xx=e[i].to;
         if(!vis[xx])
         {
             if(!uin(x,xx))  //这里是我感觉关键的地方,剪纸
               dfs(xx);
         }
     }
}

int main()
{
    int t,i,j;
    scanf("%d",&t);
    while(t--)
    {
       scanf("%d%d%d",&n,&m,&k);

       for(i=1;i<=n;i++)
       {
           father[i]=i;
           head[i]=-1;
           vis[i]=0;
       }

       int x,u,v;
       i=k;
       while(i--)
       {
           scanf("%d",&x);
       }

       num=0;

       i=m;
       while(i--)
       {
           scanf("%d%d",&u,&v);
           build(u,v);
           build(v,u);
       }

       scanf("%d",&x);
       for(i=0;i<x;i++)
        {
          scanf("%d",&a[i]);
          vis[a[i]]=1;
        }

       if(x<k)  //出现了没有访问到的点(该点有摄像头却没有记录)
       {
           printf("No\n");
           continue;
       }

       if(m<n-1) //n个点至少要n-1条边相连才可能全部走到
       {
           printf("No\n");
           continue;
       }

       int flag=1;


       dfs(a[0]);
       vis[a[0]]=0;

       for(i=1;i<k;i++)
       {

            dfs(a[i]);
            vis[a[i]]=0;
           if(cha(a[i])!=cha(a[i-1]))
           {
               flag=0;
               break;
           }
       }

       if(flag==0)
       {
           printf("No\n");
           continue;
       }

       for(i=1;i<=n;i++)
        cha(i);

       for(i=1;i<=n;i++)
        if(father[i]!=father[1])
       {
            flag=0;
            break;
       }

       if(flag==0)
         printf("No\n");
       else
         printf("Yes\n");
    }
    return 0;
}

/*

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

*/




zoj 3818 Untrusted Patrol(dsf+并査集+邻接表)

标签:style   color   os   io   ar   strong   for   art   div   

原文地址:http://blog.csdn.net/u014737310/article/details/39135839

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!