POJ1703 Find them, Catch them(并查集)
来源:互联网 发布:远程桌面控制软件 编辑:程序博客网 时间:2024/05/19 15:21
题意:
两个两个输入一些数,D代表这两个数不同,A代表查询这两个数是否相同
要点:
与食物链那一题类似,都是开多倍数组,储存每种情况
#include<stdio.h>#include<string.h>#include<stdlib.h>#define maxn 100050int p[maxn * 2], rank[maxn * 2];int m, n;void init(){for (int i = 1; i <= 2*m; ++i)//注意这里初始化要2*m{p[i] = i;rank[i] = 0;}}int find(int x){if (p[x] == x) return x;return p[x] = find(p[x]);}void merge(int x, int y){x = find(x);y = find(y);if (x == y) return;if (rank[x] > rank[y])p[y] = x;else{p[x] = y;if (rank[x] == rank[y]) ++rank[y];}}bool same(int x, int y){return find(x) == find(y);}int main(){int t,i,x,y;char str;scanf("%d", &t);while (t--){scanf("%d%d", &m, &n);getchar();init();while(n--){scanf("%c%d%d", &str,&x,&y);getchar();//这一步比较重要,清除缓冲区if (str == 'A'){if (same(x, y))printf("In the same gang.\n");else if (same(x, y + m))printf("In different gangs.\n");elseprintf("Not sure yet.\n");}else{merge(x, y + m);//合并X为A,Y为B的情况merge(x + m, y);//合并X为B,Y为A的情况}}}return 0;}
0 0
- poj1703 Find them, Catch them [并查集]
- POJ1703--Find them, Catch them--并查集
- poj1703 Find them, Catch them 并查集
- poj1703 Find them, Catch them 种类并查集
- poj1703 Find them,Catch them 【并查集】
- POJ1703 Find them, Catch them 并查集
- poj1703--Find them, Catch them(并查集应用)
- POJ1703 Find them, Catch them 【并查集】
- [并查集] poj1703 Find them,Catch them
- POJ1703 Find them, Catch them(并查集)
- poj1703 Find them, Catch them(关系并查集)
- poj1703 Find them, Catch them(并查集)
- 【Poj1703】Find them, Catch them(关系并查集)
- POJ1703-Find them, Catch them 并查集构造
- POJ1703 Find Them,Catch Them 种类并查集
- poj1703 Find them, Catch them 基础并查集问题
- POJ1703 Find them, Catch them (分类并查集)
- Find them, Catch them 并查集
- Swift2中的标准函数
- 第58课:使用Java和Scala在IDE中开发DataFrame实战学习笔记
- 数据库的基本操作
- 你真的会写单例模式吗——Java实现
- 62. Unique Paths
- POJ1703 Find them, Catch them(并查集)
- Uva299——Train Swapping
- onWindowFoucusedChanged
- JVMGC——垃圾收集器的分类
- DRP之旅(2)——当ASP遇到JSP
- Lintcode 合并两个排序的链表
- leetcode 77. Combinations
- CodeForces 332B Maximum Absurdity
- Struts2内建的拦截器