2014牡丹江网络赛C题 (第三水的题
The 2014 ACM-ICPC Asia Mudanjiang Regional First Round
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3811
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. InputThere 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. OutputFor each test case, output "Yes" if the security man worked normally and has checked all piles of drinks, or "No" if not. Sample Input2 Sample OutputNo Author: DAI, Longao Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round |
题意:
给出一个无向图,一些点上有监视器,监视器能在维修工第一次到达该点时记录,给出所有监视器记录的顺序,判断维修工是否有可能在图中走完所有点。
输入:T组数据,每组第一行n,m,k,表示有n个点m条双向边,k个点有监视器。下一行有k个点表示装监视器的点的编号。然后m行是m条边。然后一个L是有记录的监视器的数量,下面一行是L个点,表明这些监视器按这个顺序记录了。
题解:DFS。
先判L不等于K的话,它就有点没走到,直接判no。
然后开个数组b[],b[x]记录x点是由第几个监视器走到的。初始全部标为0,各个有监视器的点按最后一行输入的顺序标上1,2,3...L。
考虑监视器记录的情况,维修工需要在不经过3~L的情况下从1走到2,然后在不经过4~L的情况下从2走到3,依此类推。因为只记录第一次,走过的点是可以随便走的,所以从2走到3也就是从1或者2能到达的点走到3。这样,我们就每次从一个点dfs找看在不经过更高编号的点的情况下有没有路走到之前低编号的点走到的点。
从1~L号监视器的点依次开始dfs,不能经过b[x]比起始点高的点,只走b[x]=0的点,把走过的点标为b[x]=起点的b[]。并记录这次dfs是否能走到b[]比起始点小的点,不能的话就输出no。一直1~L都不no的话就输出yes。
具体是这样:我们有一个超碉变量now,表明这次dfs的起点。我们先从监视器1开始dfs,now=1,不能经过b[x]>1的点,并且把走过顶点都标为b[x]=1。
然后再从2开始,now=2,如果有边连向b[]<2的点,说明能走到比它小的监视器,把flag标为1。同样,这个dfs不能经过b[x]>2的点。
就这样对1~L号监视器都进行dfs。途中如果有一次dfs完flag为0,说明这个监视器和之前的监视器之间没有能走的路,就no。
代码不长,应该能直接看懂
代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back
const double eps=1e-;
const double pi=acos(-1.0); const int maxn=;
const int maxm=; struct Edge {
int v,next;
} e[maxm];
int head[maxn],en; void add(int x,int y) {
e[en].v=y;
e[en].next=head[x];
head[x]=en++;
} int n,m,k,l;
int a[maxn]; int b[maxn];
int now;
bool flag; void dfs(int x) {
//printf("[%d %d]\n",now,x);
b[x]=now;
for(int i=head[x]; i!=-; i=e[i].next) {
int v=e[i].v;
if(b[v]) {
if (b[v]<now) {
//printf("->%d b[%d]=%d\n",v,v,b[v]);
flag=;
}
continue;
}
dfs(v);
}
return;
} bool farm() {
if(l<k)return false;
int i;
mz(b);
FOR(i,,l) b[a[i]]=i;
now=;
dfs(a[]);
FOR(i,,l) {
flag=;
now=i;
dfs(a[i]);
if(!flag)return false;
}
FOR(i,,n)if(!b[i])return false;
return true;
} int main() {
int T,x,y,i;
scanf("%d",&T);
while(T--) {
scanf("%d%d%d",&n,&m,&k);
REP(i,k) {
scanf("%d",&x);
}
en=;
memset(head,-,sizeof(head));
REP(i,m) {
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
scanf("%d",&l);
FOR(i,,l)scanf("%d",&a[i]);
if(farm()) puts("Yes");
else puts("No");
}
return ;
}