zoj 3811||牡丹江網賽 c題 並查集
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5343
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
比如 4 2 1,我併入第二個的時候 必須要藉助 1,那麼就不行了,因為 1會出現在2的前面
整體 思路就是這樣了,最後再判斷一下 所有貨物的root是否相同就可以了,如果不同的話,說明它連 聯通圖都構不成
#include<stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
vector <int> v[100005];
int x,f[100005],vis[100005];
int n,m,l,k;
void init()
{
for(int i=0;i<100005;i++)
f[i]=i;
}
int find(int x)
{
if(f[x]==x)
return x;
return f[x]=find(f[x]);
}
void uni(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
f[x]=y;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(vis,0,sizeof(vis));
memset(v,0,sizeof(v));
scanf("%d%d%d",&n,&m,&k);
int a,b;
for(int i=0;i<k;i++)
{
scanf("%d",&a);
vis[a]=1;
}
init();
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
if(vis[a]==0&&vis[b]==0)
uni(a,b);
if(vis[a])
v[a].push_back(b);
if(vis[b])
v[b].push_back(a);
}
scanf("%d%d",&l,&a);
vis[a]=0;
for(int i=0;i<v[a].size();i++)
{
if(!vis[v[a][i]])
uni(v[a][i],a);
}
int flag=1;
if(l<k)
flag=0;
for(int i=1;i<l;i++)
{
scanf("%d",&b);
vis[b]=0;
if(flag==0)continue;
for(int j=0;j<v[b].size();j++)
if(!vis[v[b][j]])
uni(v[b][j],b);
if(find(a)!=find(b))
flag=0;
}
for(int i=2;i<=n;i++)
if(find(i)!=find(1))
{
flag=0;
break;
}
if(flag)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
相關文章
- ZOJ 3789 Gears(並查集)並查集
- 並查集題目合集並查集
- 關於並查集問題並查集
- 並查集到帶權並查集並查集
- 專題五 並查集【Kuangbin】並查集
- acdream群賽(4)Bad Horse(種類並查集)並查集
- HDU5441 Travel (2015年長春網路賽,並查集)並查集
- 【並查集】【帶偏移的並查集】食物鏈並查集
- 樹(tree) - 題解(帶權並查集)並查集
- C4top-排座位(並查集)並查集
- 並查集(一)並查集的幾種實現並查集
- 社交網路 (並查集的應用)並查集
- 3.1並查集並查集
- 並查集(小白)並查集
- 並查集 - 紅色警報 - 天梯賽 L2-013並查集
- 【圖論】-橋-並查集(1)題目描述圖論並查集
- 【題解】Solution Set - NOIP2024集訓Day8 並查集和可持久化並查集並查集持久化
- 並查集(Union Find)並查集
- 並查集應用並查集
- The Door Problem 並查集並查集
- 並查集練習並查集
- 並查集的使用並查集
- 並查集—應用並查集
- 寫模板, 並查集。並查集
- 並查集跳躍並查集
- 各種並查集並查集
- 食物鏈(並查集)並查集
- 【並查集】一種與時間賽跑的巧妙演算法並查集演算法
- 並查集(二)並查集的演算法應用案例上並查集演算法
- [kuangbin帶你飛]專題五 並查集 題解並查集
- 面試常考演算法題之並查集問題面試演算法並查集
- poj 1182 並查集經典問題並查集
- The Suspects-並查集(4)並查集
- [leetcode] 並查集(Ⅰ)LeetCode並查集
- [leetcode] 並查集(Ⅱ)LeetCode並查集
- [leetcode] 並查集(Ⅲ)LeetCode並查集
- 並查集演算法並查集演算法
- 並查集深度應用並查集