ZOJ 3811 Untrusted Patrol

本文探讨了在大型饮料工厂中,保安人员如何通过安全传感器确保饮料仓库的安全性。通过多条路径检查仓库中的每一批饮料,同时考虑到工厂老板可能存在的不信任心理,文章详细描述了保安巡逻过程中的可能性与挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 <=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

Author: DAI, Longao

Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>

using namespace std;

#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis,pos) memset(vis,pos,sizeof(vis))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8

typedef long long ll;

int scan()
{
   char c;
   while(c=getchar(),(c<'0'||c>'9')&&(c!='-'));
   bool flag=(c=='-');
   if(flag) c=getchar();
   int x=0;
   while(c>='0'&&c<='9')
   {
      x=x*10+c-48;
      c=getchar();
   }
   return flag?-x:x;
}

const int mn=100000+100;

int n,m,k;
int a[mn],c[mn];
int vis[mn];
vector<int> p[mn];
int ans,cnt;

void dfs(int x)
{
    REP(i,p[x].size())
    {
        int v=p[x][i] ;
        if(vis[v]==0)
        {
            if(a[v]==1)
            {
                cnt++;
                ans++;
                a[v]=0,vis[v]=1;
            }
            else
            {
                cnt++;
                vis[v]=1;
                dfs(v);
            }
        }
    }
}


int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        CLR(a,0),CLR(vis,0);
        n=scan(),m=scan(),k=scan();

        FOR(i,0,n) p[i].clear();

        REP(i,k)   a[scan()]=1;

        int u,v;
        REP(i,m)
        {
            u=scan(),v=scan();
            p[u].PB(v);
            p[v].PB(u);
        }

        int t;
        t=scan();
        REP(i,t)
         c[i]=scan();
        if(t<k)
            printf("No\n");
        else
        {
            a[c[0]]=0,vis[c[0]]=1;
            cnt=1,ans=1;
            REP(i,t)
            {
                if(a[c[i]]==1) break;
                dfs(c[i]);
            }
            if(cnt==n && ans==t) printf("Yes\n");
            else                 printf("No\n");
        }

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值