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

zoj3811Untrusted Patrol((bfs+并查集)

时间:2014-09-09 13:12:18      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   io   使用   ar   strong   for   

题目链接:

huangjing

题意:

一个工厂有n个点,有k个点是由传感器的,然后最后给l个传感器先后出现的位置,一个传感器只能记录一次。。最后判断保安是否所有的点都可能走到了??
思路:
详见下面的代码中的解释

题目:

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
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#define eps 1e-9
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;

const int maxn=100000+10;

bool is_switch[maxn],vis[maxn];
int head[maxn],root[maxn],n,m,k,l,cnt;

struct Edge
{
    int from,to,next;
}edge[maxn<<1];

vector<int>vec[maxn],G;

int findroot(int x)
{
   if(root[x]!=x)
      root[x]=findroot(root[x]);
   return root[x];
}

void init()
{
    for(int i=1;i<maxn;i++)
        root[i]=i;
    memset(head,-1,sizeof(head));
    memset(is_switch,false,sizeof(is_switch));
    memset(vis,false,sizeof(vis));
    for(int i=0;i<maxn;i++)
       vec[i].clear();
    G.clear();
    cnt=0;
}

void add_Edge(int x,int y)
{
    edge[++cnt].from=x;
    edge[cnt].to=y;
    edge[cnt].next=head[x];
    head[x]=cnt;
}

void add(int u)
{
    for(int i=0;i<vec[u].size();i++)
    {
        int v=vec[u][i];
        if(!is_switch[v])
        {
            add_Edge(u,v);
            add_Edge(v,u); //把跟触发器线相连的没有触发器的点连接起来
        }
    }
    is_switch[u]=false;//这个触发器的跟它有关的联通快已经找好
}

void bfs(int u)
{
    queue<int>Q;
    while(!Q.empty()) Q.pop();
    Q.push(u);
    vis[u]=true;
    while(!Q.empty())
    {
        int x=Q.front();
        Q.pop();
        for(int i=head[x];~i;i=edge[i].next)
        {
            int temp=edge[i].to;
            int fx=findroot(x);
            int fy=findroot(temp);
            if(fx==fy)
                continue;
            else
                root[fx]=fy;
            if(!vis[temp])
            {
                vis[temp]=true;
                Q.push(temp);
            }
        }
    }
}

void read_Graph()
{
    init();
    int u,v;
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=k;i++)
    {
        scanf("%d",&u);
        is_switch[u]=true;
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&u,&v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    scanf("%d",&l);
    for(int i=1;i<=l;i++)
    {
        scanf("%d",&u);
        G.push_back(u);
    }
    for(int i=1;i<=n;i++)
    {
        if(!is_switch[i])
        {
            for(int j=0;j<vec[i].size();j++)
            {
                v=vec[i][j];
                if(!is_switch[v])
                    add_Edge(i,v);
            }
        }
    }//先把没有触发器的那些点连接
}

bool solve()
{
    if(k==0)  return true;
    if(l<k)  return false;
    for(int i=0;i<G.size();i++)
    {
        int u=G[i];
        add(u);
        bfs(u);
        if(i>=1)
        {
            int a=G[i-1];
            int b=G[i];
            int fx=findroot(a);
            int fy=findroot(b);
            if(fx!=fy)  return false;//说明两个联通块没有联通,说明这个保安使用看了诈骗术
        }
    }
    for(int i=1;i<=n;i++)
      if(!vis[i])
           return false;//最后检验是否每个地方都走到过。。
    return true;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        read_Graph();
        if(solve())
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}
/*
2
6 6 3
1 2 4
5 6
1 2
2 3
3 1
1 4
4 5
3
4 2 1
*/





zoj3811Untrusted Patrol((bfs+并查集)

标签:style   http   color   os   io   使用   ar   strong   for   

原文地址:http://blog.csdn.net/u014303647/article/details/39137591

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