zoj 3811 Untrusted Patrol 图论 The 2014 ACM-ICPC Asia Mudanjiang Regional First Round

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3811

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

Author: DAI, Longao
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round


题意:有一个n<=10^5个点m<=2*10^5条边的无向图。在其中k个点有摄像头。第一次进入有摄像头的点会被记录。有l条记录按照时间顺序给出。问是否存在从某一点开始的一种走法(可以经过重复的点或边)访问了所有点且满足摄像头那些点的限制。

参考了大神的解法...http://www.mahoushojo.com:8080/mediawiki/index.php/14-09-07_Online

解法:如果l<k必然无解。否则先空降第一个点,每次先遍历周围所有不带摄像头的点,再看是否能走到序列中的下一个点。

代码:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>

using namespace std;

#define min2(x, y)     min(x, y)
#define max2(x, y)     max(x, y)
#define min3(x, y, z)  min(x, min(y, z))
#define max3(x, y, z)  max3(x, max(y, z))
#define clr(x, y)      memset(x, y, sizeof(x))
#define fr(i,n)        for(int i = 0; i < n; i++)
#define fr1(i,n)       for(int i = 1; i < n; i++)
#define upfr(i,j,n)    for(int i = j; i <= n; i++)
#define dowfr(i,j,n)   for(int i = n; i >= j; i--)
#define scf(n)         scanf("%d", &n)
#define scf2(n,m)      scanf("%d %d",&n,&m)
#define scf3(n,m,p)    scanf("%d %d %d",&n,&m,&p)
#define ptf(n)         printf("%d",n)
#define ptf64(n)       printf("%I64d",n)
#define ptfs(s)        printf("%s",s)
#define ptln()         printf("\n")
#define ptk()          printf(" ")
#define ptc(c)         printf("%c",c)
#define srt(a,n)       sort(a,n)
#define LL long long
#define pi acos(-1.0)
#define inf 1 << 31-1
#define eps 0.00001
#define maxn 100005
#define mod 10000007

vector <int >e[maxn];
int vis[maxn],jianshi[maxn],keda[maxn],id[maxn];
int N,M,K,L;
void bfs(int u)                  //从u开始,走到u周围所有的非监视器的点
{
    jianshi[u] = 0;
    vis[u] = 1;
    queue <int> q;
    q.push(u);
    while(!q.empty())
    {
        int uu = q.front();
        q.pop();
        vis[uu] = 1;
        fr(i, (int)e[uu].size())
        {
            int v = e[uu][i];
            if(!vis[v] && !keda[v])
            {
                keda[v] = 1;        //标记能到达的点
                if(!jianshi[v])
                q.push(v);
            }
        }
    }
}
int main()
{
    int t;
    scf(t);
    while(t--)
    {
        scf3(N, M, K);
        clr(e, 0);
        clr(vis, 0);
        clr(jianshi, 0);
        clr(keda, 0);
        fr(i, K)
        {
            int u;
            scf(u);
            u--;
            jianshi[u] = 1;
        }
        fr(i, M)
        {
            int u, v;
            scf2(u, v);
            u--,v--;
            e[u].push_back(v);
            e[v].push_back(u);
        }
        scf(L);
        fr(i, L)
        {
            scf(id[i]);
            id[i]--;
        }
        if(L < K)                  //监视器都没有走完,必然是No
        {
            printf("No\n");
            continue;
        }
        int ok = 1;
        bfs(id[0]);
        for(int j = 1; j < L && ok; j++)
        {
            if(!keda[id[j]])     //对于给定序列,如果不能到达下一个点,则结果为No
                ok = 0;
            else
                bfs(id[j]);
        }
        fr(i, N)
        if(!vis[i])             //最后遍历一下所有点,判断是否存在未访问到的点
            ok = 0;
        if(ok)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值