程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> zoj3811Untrusted Patrol((bfs+並查集)

zoj3811Untrusted Patrol((bfs+並查集)

編輯:C++入門知識

zoj3811Untrusted Patrol((bfs+並查集)


題目鏈接:

huangjing

題意:

一個工廠有n個點,有k個點是由傳感器的,然後最後給l個傳感器先後出現的位置,一個傳感器只能記錄一次。。最後判斷保安是否所有的點都可能走到了??
思路:
詳見下面的代碼中的解釋

題目:

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 <=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
代碼:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define eps 1e-9
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;

const int maxn=100000+10;

bool is_switch[maxn],vis[maxn];
int head[maxn],root[maxn],n,m,k,l,cnt;

struct Edge
{
    int from,to,next;
}edge[maxn<<1];

vectorvec[maxn],G;

int findroot(int x)
{
   if(root[x]!=x)
      root[x]=findroot(root[x]);
   return root[x];
}

void init()
{
    for(int i=1;iQ;
    while(!Q.empty()) Q.pop();
    Q.push(u);
    vis[u]=true;
    while(!Q.empty())
    {
        int x=Q.front();
        Q.pop();
        for(int i=head[x];~i;i=edge[i].next)
        {
            int temp=edge[i].to;
            int fx=findroot(x);
            int fy=findroot(temp);
            if(fx==fy)
                continue;
            else
                root[fx]=fy;
            if(!vis[temp])
            {
                vis[temp]=true;
                Q.push(temp);
            }
        }
    }
}

void read_Graph()
{
    init();
    int u,v;
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=k;i++)
    {
        scanf("%d",&u);
        is_switch[u]=true;
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&u,&v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    scanf("%d",&l);
    for(int i=1;i<=l;i++)
    {
        scanf("%d",&u);
        G.push_back(u);
    }
    for(int i=1;i<=n;i++)
    {
        if(!is_switch[i])
        {
            for(int j=0;j=1)
        {
            int a=G[i-1];
            int b=G[i];
            int fx=findroot(a);
            int fy=findroot(b);
            if(fx!=fy)  return false;//說明兩個聯通塊沒有聯通,說明這個保安使用看了詐騙術
        }
    }
    for(int i=1;i<=n;i++)
      if(!vis[i])
           return false;//最後檢驗是否每個地方都走到過。。
    return true;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        read_Graph();
        if(solve())
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}
/*
2
6 6 3
1 2 4
5 6
1 2
2 3
3 1
1 4
4 5
3
4 2 1
*/





  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved