1073 家族(并查集模板)
来源:互联网 发布:辽宁11选5遗漏数据 编辑:程序博客网 时间:2024/04/30 11:58
题目描述 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
数据范围及提示 Data Size & Hint
n<=5000,m<=5000,p<=5000
#include<iostream>#include<string.h>#include<cstdio> using namespace std;int pre[30000];int n,m,p;void init(){for (int i=1; i<=n; i++){pre[i] = -1; } }int find(int num){while (pre[num]>-1){num = pre[num];}return num;}void union_set(int x, int y){int xx = find(x);int yy = find(y);if (xx != yy){pre[xx] += pre[yy];pre[yy] = xx;}}int main(){scanf("%d%d%d",&n,&m,&p); init();int x, y;while (m--){scanf("%d%d",&x,&y);union_set(x,y);}while (p--){scanf("%d%d",&x,&y); if (find(x) == find(y)) { cout<<"Yes"<<endl; }else{ cout<<"No"<<endl; }}return 0;}
阅读全文
0 0
- 1073 家族(并查集模板)
- CODEVS 1073 家族 并查集模板
- CodeVS 1073家族(并查集)
- 家族(并查集)
- 【并查集】家族
- 【并查集】家族
- 家族 并查集
- [并查集]家族
- 简单的并查集1073家族
- Codevs 1073 家族 并查集
- CODEVS 1073 家族 (并查集)
- 并查集——1073 家族
- codevs 1073 家族 并查集
- P1034 家族 (并查集 I)
- (并查集入门)P1034家族
- 【vijos P1034】家族(并查集)
- SSL1896家族(并查集)
- 【Vijos 1034】家族(并查集)
- 总结一下Java的数据类型体系
- CSS笔记
- 解决浏览器显示长数字不能自动换行或长字母不能换行的问题
- JVM内存模型
- c/c++获取当前时间 自1970年的秒数
- 1073 家族(并查集模板)
- 解决MyEclipse不能正常加载问题
- 【C#】流程控制语句—跳转语句
- WPF 定义自己的控件
- Deep Learning(深度学习)学习笔记整理系列之(四)
- 关于音视频直播技术的总结
- Java调用hdfs出现java.lang.VerifyError: class org.apache.hadoop.hdfs.protocol.proto异常
- POJ 1240 Pre-Post-erous! 笔记
- Android Studio 常见 Build 问题 Error:failed to find Build Tools revision 23.0.2