codevs1073 家族 并查集
2017-03-17 18:04
513 查看
1073 家族
时间限制: 1 s空间限制: 128000 KB
题目等级 : 大师 Master
题解
查看运行结果
题目描述 Description
若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系。 规定:x和y是亲戚,y和z是亲戚,那么x和z也是亲戚。如果x,y是亲戚,那么x的亲戚都是y的亲戚,y的亲戚也都是x的亲戚。
输入描述 Input Description
第一行:三个整数n,m,p,(n<=5000,m<=5000,p<=5000),分别表示有n个人,m个亲戚关系,询问p对亲戚关系。 以下m行:每行两个数Mi,Mj,1<=Mi,Mj<=N,表示Ai和Bi具有亲戚关系。 接下来p行:每行两个数Pi,Pj,询问Pi和Pj是否具有亲戚关系。
输出描述 Output Description
P行,每行一个’Yes’或’No’。表示第i个询问的答案为“具有”或“不具有”亲戚关系。
样例输入 Sample Input
6 5 3
1 2
1 5
3 4
5 2
1 3
1 4
2 3
5 6
样例输出 Sample Output
Yes
Yes
No
#include<cstdio> #include<iostream> using namespace std; const int mx=5000+5; int n,m,p,f[mx],a,b; int find(int x){ int t=x; while(f[x]!=x) x=f[x]; return f[t]=x; } void Union(int x,int y){ f[find(y)]=find(x); } int main(){ cin>>n>>m>>p; for(int i=1;i<=n;i++){//init f[i]=i; } for(int i=1;i<=m;i++){ cin>>a>>b; Union(a,b); } for(int i=1;i<=p;i++){ cin>>a>>b; if(find(a)==find(b)) cout<<"Yes\n"; else cout<<"No\n"; } return 0; }
相关文章推荐
- codevs 1073 家族 并查集
- codevs1073 家族 并查集
- CODEVS 1073 家族 并查集模板
- 【codevs1073】家族,胡写并查集
- Codevs 1073 家族 并查集
- 并查集 之 CODE[VS] 1073 家族
- [codevs1073]家族
- 家族(codevs 1073)
- 【CODEVS1073】家族
- CODEVS 1073家族
- Codevs 1073 家族
- bzoj 1050 codevs1001 舒适的路线[并查集]
- CODEVS 1073 家族 (并查集)
- codevs 1074食物链 并查集
- codevs 1001 舒适的路线(并查集) 题解
- CODEVS 1299 切水果(线段树||并查集)
- codevs 1540 银河英雄传说(加权并查集)
- 【基础练习】【并查集】codevs2796 最小完全图题解
- codevs 星际跳跃并查集的应用
- codevs 1001 舒适的路线 并查集