ZOJ 3811 Untrusted Patrol The 2014 ACM-ICPC Asia Mudanjiang Regional First Round

Description

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

Source

The 2014 ACM-ICPC Asia Mudanjiang Regional First Round

题意:给出n个点,m条边,在其中k个点上有传感器,有一个人在图上走。再给出长度为l的序列,为传感器第一次感知有人走到的顺序。求该人的行走是否合法且遍历全图。

思路:设没有传感器的点为普通点,有传感器的点为特殊点。先把普通点加入并查集中。然后按照l序列的顺序,把特殊点变为普通点,然后加入并查集。判断该点和之前的一个点是否联通,不联通则违法。注意还有两个要特判,l不等于k以及整张图不是联通图,这两种情况都要输出no。

  1 /*
  2  * Author:  Joshua
  3  * Created Time:  2014年09月09日 星期二 17时27分53秒
  4  * File Name: zoj3811.cpp
  5  */
  6 #include<cstdio>
  7 #include<vector>
  8 #include<cstring>
  9 using namespace std;
 10
 11 #define maxn 100005
 12
 13 typedef long long LL;
 14 int f[maxn],n,m,k,T;
 15 bool p[maxn];
 16 vector<int> r[maxn];
 17
 18 void init()
 19 {
 20     int l,v,u,x,y;
 21     memset(p,0,sizeof(p));
 22     scanf("%d%d%d",&n,&m,&k);
 23     for (int i=1;i<=k;++i)
 24     {
 25         scanf("%d",&x);
 26         p[x]=true;
 27     }
 28     for (int i=1;i<=n;++i) r[i].clear();
 29     for (int i=1;i<=m;++i)
 30     {
 31         scanf("%d%d",&u,&v);
 32         r[u].push_back(v);
 33         r[v].push_back(u);
 34     }
 35     for (int i=1;i<=n;++i) f[i]=i;
 36 }
 37
 38 int gf(int x)
 39 {
 40     if (f[x]==x) return x;
 41     return (f[x]=gf(f[x]));
 42 }
 43
 44 void update(int x)
 45 {
 46     int fx,fy;
 47     for (int j=0;j<r[x].size();++j)
 48         if (!p[r[x][j]])
 49         {
 50             fx=gf(x);
 51             fy=gf(r[x][j]);
 52             if (fx<fy) f[fy]=fx;
 53             else f[fx]=fy;
 54         }
 55 }
 56
 57 void solve()
 58 {
 59     int l,x,y;
 60     scanf("%d",&l);
 61     if (l!=k)
 62     {
 63         for (int i=1;i<=l;++i)
 64             scanf("%d",&x);
 65         printf("No\n");
 66         return;
 67     }
 68     for (int i=1;i<=n;++i)
 69         if (!p[i])
 70             update(i);
 71     for (int i=1;i<=l;++i)
 72     {
 73         scanf("%d",&x);
 74         p[x]=false;
 75         update(x);
 76         if ((i>1) && (gf(x)!=gf(y)))
 77         {
 78             printf("No\n");
 79             for (int j=i+1;j<=l;++j) scanf("%d",&x);
 80             return;
 81         }
 82         y=x;
 83     }
 84     for (int i=1;i<=n;++i)
 85         if (gf(i)!=1)
 86         {
 87             printf("No\n");
 88             return;
 89         }
 90     printf("Yes\n");
 91 }
 92 int main()
 93 {
 94     scanf("%d",&T);
 95     while (T--)
 96     {
 97         init();
 98         solve();
 99     }
100     return 0;
101 }
时间: 2024-10-01 08:02:22

ZOJ 3811 Untrusted Patrol The 2014 ACM-ICPC Asia Mudanjiang Regional First Round的相关文章

zoj 3811 Untrusted Patrol(bfs或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 hir

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 hir

2014 ACM/ICPC Asia Regional Guangzhou Online Wang Xifeng&#39;s Little Plot HDU5024

一道好枚举+模拟题目.转换思维视角 这道题是我做的,规模不大N<=100,以为正常DFS搜索,于是傻乎乎的写了起来.各种条件限制模拟过程 但仔细一分析发现对每个点进行全部八个方向的遍历100X100X100^8 .100X100个点,每个点在走的时候8中选择,TLE 于是改为另一个角度: 以符合要求的点为拐弯点,朝两个垂直的方向走,求出最远的距离.这样只要对每个点各个方向的长度知道,组合一下对应的就OK. 避免了每个点深搜. PS:搜索的时候x,y写反了,导致构图出现问题,以后用[dy][dx]

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que

poj 5024&amp;&amp;&amp;2014 ACM/ICPC Asia Regional Guangzhou Online 1003(预处理)

http://acm.hdu.edu.cn/showproblem.php?pid=5024 分析:预处理每个点在八个方向的射线长度,再枚举八种L形状的路,取最大值. 注意题意是求一条最长路,要么一条直线,要么只有一个90角,即L型.其实直线就是L形的一个方向长度为0. 代码: #include<iostream> #include<map> #include<cstdio> #include<string> #include<cstring>

2014 ACM/ICPC Asia Regional Xi&#39;an Online

03 hdu5009 状态转移方程很好想,dp[i] = min(dp[j]+o[j~i]^2,dp[i]) ,o[j~i]表示从j到i颜色的种数. 普通的O(n*n)是会超时的,可以想到o[]最大为sqrt(n),问题是怎么快速找到从i开始往前2种颜色.三种.四种...o[]种的位置. 离散化之后,可以边走边记录某个数最后一个出现的位置,初始为-1,而所要求的位置就等于 if(last[a[i]]==-1) 该数没有出现过,num[i][1] = i,num[i][j+1] = num[i-1

HDU 5000 2014 ACM/ICPC Asia Regional Anshan Online DP

Clone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Submission(s) : 8   Accepted Submission(s) : 5 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description After eating food from Chernobyl,

2014 ACM/ICPC Asia Regional Xi&#39;an Online(HDU 5007 ~ HDU 5017)

题目链接 A题:(字符串查找,水题) 题意 :输入字符串,如果字符串中包含“ Apple”, “iPhone”, “iPod”, “iPad” 就输出 “MAI MAI MAI!”,如果出现 “Sony” 就输出“SONY DAFA IS GOOD!” ,大小写敏感. 思路 : 字符串查找,水题. 1 #include <string.h> 2 #include <stdio.h> 3 #include <iostream> 4 5 using namespace st