zoj 3811||牡丹江网赛 c题 并查集
来源:互联网 发布:ip mac 扫描工具 编辑:程序博客网 时间:2024/05/20 07:20
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
25 5 31 2 41 22 33 11 44 534 2 15 5 31 2 41 22 33 11 44 534 1 2
Sample Output
NoYes
比如 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 3811||牡丹江网赛 c题 并查集
- zoj 3811 (2014 牡丹江网赛 C) Untrusted Patrol
- zoj 3811 Untrusted Patrol 牡丹江网络赛c题
- ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol
- ZOJ-3811 Untrusted Patrol DFS 2014牡丹江网络赛C题
- ZOJ 3811 —— Untrusted Patrol(2014牡丹江网络赛C题)
- Zoj 3811并查集
- zoj 3818 2014牡丹江网赛
- zoj 3816 2014牡丹江网赛 DFS
- zoj 3827(牡丹江现场赛I题)
- ZOJ 3811 Untrusted Patrol 并查集
- ZOJ 3811 Untrusted Patrol 并查集
- ZOJ 3811 dfs&并查集
- zoj 3822 && 牡丹江d题
- zoj 3659 并查集
- zoj 3641 并查集
- zoj 3789 并查集
- zoj Friendship 并查集
- [xcode]常见错误
- 函数模板实例
- 代码可以编译和运行
- uPortal4.1.0安装部署
- 哎呀头疼的驱动
- zoj 3811||牡丹江网赛 c题 并查集
- Android应用开发-小巫CSDN博客客户端开发开篇
- 开源HTML5 APP开发神器CanTK发布
- 标签管理组织高层领导了解标签进销存情况是不是所谓的管理型业务用例
- 【Android】getwidth和getmeasuredwidth的区别以及两者的使用场景
- codeigniter读取数据库的公共配置并全局缓存的实现方案
- TimeChooser
- Cocos2d-x-Lua 开发简单的小游戏(记数字踩白块)
- Python 对文档内容TFIDF处理