首页 > 代码库 > ZOJ 3811 Untrusted Patrol 并查集+邻接表,注意所有点都要走过

ZOJ 3811 Untrusted Patrol 并查集+邻接表,注意所有点都要走过

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 Aiand 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
题意
n个点,m条边,k个点有报警器,每个报警器经过后报警一次就不能再使用。
L次报警,接下来L个数字是一次次的报警顺序
保安每经过一个报警器,报警器就报一次警;
判断保安在这种报警顺序下,有没有可能已经把所有的点都走过了一遍。可能yes 不可能no;

思路:
先把每个报警器用vis标记起来。vis=1代表是报警器
先把所有没警报器的用并查集放入图中,再把和每个报警器相连的存入edge。 如题:把3存入1报警器 edge[1].push_back(3);
然后每查询一个报警器,都把这个报警器的vis改成1,视为普通点,因为报警器只能用一次;
再把与他相连的点,且那个相连的点不是报警器,就放入图中。再检测其能否到达第一个点;
如果每个报警器都能经过的话。再逐一点判断是否和点1是同一个祖先,从而得到这个图是否全联通,也就是 是否 每个点保安都能走过,如果这点再符合,那么就是yes了。

ZOJ 3811 Untrusted Patrol 并查集+邻接表,注意所有点都要走过