ZOJ 3811 Untrusted Patrol(BFS)

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


解题思路:给一定要访问的探测器编号,从小到大去访问下一个探测器,每次都保证到达一个探测器一定能从上一个编号最小的探测器到达,通过dfn数组记录从该点过来的最小探测器编号,如果没有访问过,如果访问的是探测器,且编号比前面的大,入队列,dfn置为前面的值,如果访问过,前面的值比当前的小,更新当前的值,并把当前值如队列,最后监视器全访问过为Yes,否则为No


#include <iostream>
#include <cstdio>
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=100005;

vector <int > G[maxn];
vector <int > w;
bool vis[maxn],flag;
int m,n,k,l,a[maxn],dfn[maxn];
int f[maxn];
void initial()
{
    for(int i=0;i<maxn;i++)  G[i].clear();
    memset(vis,0,sizeof(vis));
    memset(f,-1,sizeof f);
    memset(dfn,0x3f,sizeof dfn);
}

void input()
{
    int x,u,v;
    scanf("%d %d %d",&n,&m,&k);
    for(int i=1;i<=k;i++)  scanf("%d",&x);
    for(int i=1;i<=m;i++)
    {
         scanf("%d %d",&u,&v);
         G[u].push_back(v);
         G[v].push_back(u);
    }
    scanf("%d",&l);
    for(int i=1;i<=l;i++)
    {
        scanf("%d",&a[i]);
        f[a[i]]=i;
    }
}
/*
void dfs(int u,int fa,int p,int z){
    pass[u].push_back(z);
    dfn[u]=min(dfn[u],p);
    if(f[u]!=-1) vis[u]=1;
    int nn=G[u].size();
    for(int i=0;i<nn;i++){
        int v=G[u][i];
        if(v!=fa){
            if(!vis[v]){
                if(!pass[v].empty()&&lower_bound(pass[v].begin(),pass[v].end(),z)!=pass[v].end()) continue;
                cout<<u<<" "<<f[v]<<" "<<dfn[u]<<endl;
                if(f[v]==-1) dfs(v,u,p,z);
                else if(f[v]>dfn[u]) dfs(v,u,f[v],z+1);
            }
        }
    }
}*/
void bfs(){
    queue<int> q;
    q.push(a[1]);
    dfn[a[1]]=1;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        int nn=G[x].size();
        for(int i=0;i<nn;i++){
            int v=G[x][i];
            if(dfn[v]==inf){
                if(f[v]==-1){
                    dfn[v]=dfn[x];
                    q.push(v);
                }else{
                    if(f[v]>dfn[x]){
                        dfn[v]=f[v];
                        q.push(v);
                    }
                }
            }else{
                if(f[v]==-1&&dfn[x]<dfn[v]){
                    dfn[v]=dfn[x];
                    q.push(v);
                }
            }
        }
    }
}
bool visited[maxn];
void dfs1(int u){
    visited[u]=1;
    int nn=G[u].size();
    for(int i=0;i<nn;i++){
        int v=G[u][i];
        if(!visited[v]) dfs1(v);
    }
}
bool solve(){
    if(l<k)  return false;
    memset(visited,0,sizeof visited);
    int ct=0;
    for(int i=1;i<=n;i++){
        if(!visited[i]) dfs1(i),ct++;
    }
    if(ct>1) return false;
    bfs();
    for(int i=1;i<=l;i++) if(dfn[a[i]]==inf) return false;
    return true;
}

int main()
{
    int T;
    scanf("%d",&T);
    for(int co=1;co<=T;co++)
    {
        initial();
        input();
        if(solve()) printf("Yes\n");
        else  printf("No\n");
    }
    return 0;
}
/*
1
8 9 6
1 3 4 5 7 8
1 2
2 3
2 4
2 5
5 6
6 7
6 8
1 8
2 6
6
7 4 1 8 5 3
*/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值