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;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章