并查集 POJ-1703 Find them, Catch them
来源:互联网 发布:网络借贷超市 编辑:程序博客网 时间:2024/06/01 07:38
传送门:vjudge 或者原网址
本题与食物连相似 传送门
题目大意:警察抓捕罪犯,罪犯有两个帮派(龙帮和蛇帮。。。抓罪犯还要分帮派,难道是龙蛇混杂,龙帮不敢抓??), 让你写个程序分辨两个罪犯是不是属于一个帮派,
输入 A 时判断两个罪犯是不是同一个帮派, 输入D 时说明两个罪犯不是同一个帮派的。
AC 代码:
#include <iostream>#include <cstdio>using namespace std;int par[200100];int Rank[200100];void init(int n){for(int i=0; i<n; i++){par[i] = i;Rank[i] = 0;}}int find_par(int n){if(par[n] == n) return n;return par[n] = find_par( par[n] );}void unit_par(int a, int b){a = find_par(a);b = find_par(b);if(a == b) return;if(Rank[a] < Rank[b]) par[a] = b;else{par[b] = a;if(Rank[b] == Rank[a]) Rank[a]++;}}bool same(int a, int b){return find_par(a) == find_par(b);}int main(){int T;scanf("%d", &T);while(T--){int n, m;char ch[10];int a, b;scanf("%d%d", &n, &m);init(n*2);for(int i=0; i<m; i++){scanf("%s%d%d", ch, &a, &b);if(ch[0] == 'A'){if(same(a,b)) printf("In the same gang.\n");else if(same(a,b+n) || same(a+n,b)) printf("In different gangs.\n");else printf("Not sure yet.\n");}else{unit_par(a, b+n);unit_par(a+n, b);}}}return 0;}
阅读全文
0 0
- POJ 1703 Find them, Catch them(并查集)
- POJ 1703 Find them, Catch them //并查集
- poj 1703 Find them, Catch them 并查集
- poj 1703 Find them, Catch them(并查集)
- poj 1703 Find them, Catch them //并查集
- POJ 1703 Find them, Catch them 并查集
- poj 1703 Find them, Catch them (并查集)
- poj 1703 Find them, Catch them(种类并查集)
- poj 1703 Find them, Catch them(种类并查集)
- poj 1703 -- Find them, Catch them(并查集)
- poj 1703 Find them, Catch them(并查集应用)
- POJ 1703 Find them, Catch them (并查集)
- poj 1703 - Find them, Catch them(并查集)
- poj 1703 Find them, Catch them(关系并查集)
- POJ 1703 Find them, Catch them 种类并查集
- POJ 1703 Find them, Catch them (关系并查集)
- POJ-1703 Find them, Catch them 经典并查集
- poj 1703 Find them,Catch them 并查集
- php学习——css复习六(格式清除和布局方法)
- JAVA核心技术卷一重要概念回顾(持续更新中)
- 设计模式——策略模式
- 有关maven的一些常见问题
- iOS 常用应用的url scheme
- 并查集 POJ-1703 Find them, Catch them
- 简单的系统命令
- linux基本命令
- SVM支撑向量机相关
- 前端经典总结(一)
- zookeeper原理:分布式协同技术
- DG Broker switchover的过程
- LeetCode
- Vue2.0全家桶仿腾讯课堂(移动端)