ZOJ 3811-Untrusted Patrol(DFS)

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 (1~100000)堆货物,M(1~100000)条路联通,K (1~N )个监控器。给出带有监控器的货物堆,M条联通路。L(1~N)个数据,表示监控器亮的顺序。监控器只会记录第一次记过的时间。题目要求判断工人是否将所有的堆都巡查完。

思路:

使用vector容器记录相连通的堆。根据L 序列DFS,使用vis 数组记录这个监控器能到达的所有监控器,记录之前应该判断这个监控器是否被记录过。若搜索到的堆没有监控器且没有被走过则应该继续DFS,直到此个监控器连接的下一些监控器都能被记录到,则跳出DFS。接着判断L 序列中的下一个亮的监控器是否已经被记录。若没有则no,若有则重复以上操作。由于路是可以重复走的,所以不需要清空vis。最后记得判断一下各个堆是否遍历,vis数组循环一遍,倘若都为1 则全部遍历。

CODE:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
const int inf=0xfffffff;
typedef long long ll;
using namespace std;

vector<int> vec[100005];
int sen[100005], lig[100005], vis[100005];
int N, M, K;

void dfs(int ii)
{
    vis[ii]=1;
    for(int i=0; i<vec[ii].size(); i++){
        int v = vec[ii][i];
        if(sen[v] && vis[v] == 0){
            vis[v]=1;
        }
        else if(sen[v] == 0 && vis[v] == 0){
            dfs(v);
        }
    }
    return ;
}

int main()
{
    //freopen("in", "r", stdin);
    int T;
    scanf("%d", &T);
    while(T--){
        memset(sen, 0, sizeof(sen));
        memset(vis, 0, sizeof(vis));
        memset(lig, 0, sizeof(lig));
        for(int i=0; i<100005; i++)
            vec[i].clear();
        scanf("%d %d %d", &N, &M, &K);
        int kk;
        for(int i=0; i<K; i++){
            scanf("%d", &kk);
            sen[kk]=1;
        }
        int a, b;
        while(M--){
            scanf("%d %d", &a, &b);
            vec[a].push_back(b);
            vec[b].push_back(a);
        }
        int L;
        scanf("%d", &L);
        for(int i=0; i<L; i++){
            scanf("%d", &lig[i]);
        }
        if(K != L){
            printf("No\n");
            continue;
        }
        int ok=1;
        vis[lig[0]] = 1;
        for(int i=0; i<L; i++){
            if(vis[lig[i]] == 0){
                ok=0;
                break;
            }
            dfs(lig[i]);
        }
        for(int i=1; i<=N; i++){
            if(vis[i] == 0){
                ok=0;
                break;
            }
        }
        if(ok) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值