zoj 3811||牡丹江网赛 c题 并查集
2014-09-08 07:34
267 查看
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.
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.
解题思路:先把 没有 摄像头的 货物并在一起然后按照给定的顺序 来 并入有摄像头的 货物如果 中途出现 不能并入的,那么就说明 没有可能 出现给定的顺序
比如 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;
}
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;
}
相关文章推荐
- 并查集 + Bfs 之 zoj 3811 Untrusted Patrol
- zoj 3811 Untrusted Patrol 牡丹江网络赛c题
- Zoj 3811并查集
- 并查集 + Bfs 之 zoj 3811 Untrusted Patrol
- ZOJ 3811 Untrusted Patrol 并查集 染色 BFS
- ZOJ_3811_Untrusted Patrol(并查集)
- ZOJ 3811 —— Untrusted Patrol(2014牡丹江网络赛C题)
- ZOJ 3811 Untrusted Patrol 并查集+邻接表,注意所有点都要走过
- ZOJ 3811 Untrusted Patrol 并查集
- zoj 3811 Untrusted Patrol (按照顺序访问点+并查集||宽搜)
- zoj 3811 (2014 牡丹江网赛 C) Untrusted Patrol
- ZOJ 3811 dfs&并查集
- ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol
- ZOJ 3811 zoj 3811 Untrusted Patrol牡丹江网络赛C题
- ZOJ-3811 Untrusted Patrol DFS 2014牡丹江网络赛C题
- ZOJ 3811 Untrusted Patrol【并查集】
- ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集
- zoj 3811 Untrusted Patrol 2014牡丹江网络赛 dfs
- ZOJ 3811 Untrusted Patrol 并查集
- zoj 3261 - Connections in Galaxy War(并查集)