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(2 ~ 200000),K(1 ~ N),代表有 N 个结点,M 条边,K 个传感器。后给出 M 条双向边,给出一个 L 长度的序列,代表要求传感器发生记录的顺序。传感器只会记录第一次记录的时间,问可不可能遍历完所有结点且存在 L 这样的顺序。

 

       思路:

       DFS。首先要弄清楚,走过的点可以重复走,所以比如存在 3,4,2,1 这样的序列,3 要到达 4 且不经过后面任何一个传感器(就是 2 和 1 ),接下来如果走到 2 到 1 的话,前面的传感器对他们是没有任何要求的,如果 3 或者 4 能够到达 1 这个结点的话,说明 2 可以通过 3 或者 4 结点到达 1 这个结点。所以每次 DFS 都是从一个传感器开始到另一个传感器结束,每次 DFS 之前先判断这个传感器是否已经被达到过了,如果不曾达到过的话,说明之前所有的点都到不了这个点,这时候就可以直接跳出循环了;如果达到过的话,就继续 DFS,以这个传感器为起点,直到找到新的另一个传感器。最后还有记得判断是否为连通图,可以直接依赖 vis 函数来判断是否连通,连通的话,应该所有结点的 vis 都为 true。

 

        AC:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int VMAX = 100005;

vector<int> G[VMAX];
int aim[VMAX];
bool vis[VMAX], sen[VMAX];

void dfs (int x) {

    for (int i = 0; i < G[x].size(); ++i) {
        int v = G[x][i];
        if (!vis[v]) {
            vis[v] = 1;
            if (!sen[v]) dfs(v);
        }
    }

}

int main() {

    int t;
    scanf("%d", &t);

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

        for (int i = 1; i <= n; ++i) {
            G[i].clear();
            vis[i] = sen[i] = 0;
        }

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

        while (m--) {
            int a, b;
            scanf("%d%d", &a, &b);
            G[a].push_back(b);
            G[b].push_back(a);
        }

        int l;
        scanf("%d", &l);
        for (int i = 1; i <= l; ++i) {
            scanf("%d", &aim[i]);
        }

        if (l < k) printf("No\n");
        else {
            int temp = 0;
            vis[ aim[1] ] = 1;
            for (int i = 1; i <= l; ++i) {
                if (!vis[ aim[i] ]) {
                    temp = 1;
                    break;
                }
                dfs(aim[i]);
                sen[ aim[i] ] = 0;
            }

            for (int i = 1; i <= n; ++i) {
                if (!vis[i]) {
                    temp = 1;
                    break;
                }
            }

            if (!temp) printf("Yes\n");
            else printf("No\n");
        }

    }

    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值