并查集:POJ No1703 Find them, Catch them
题目链接:http://poj.org/problem?id=1703
题意:两个坏蛋属于不同的组织,给出两个坏蛋判定是否一个组织。
题解:已知每次输入的两个帮派人员 x, y; 合并 (x, y + N), (x + N, y)。判定时,如果 (x, y) 属于同一个 根,就是同一个组织,(x, y+N) 属于同一个根,则说明是不同组织。不确定则无解。
#include <iostream>
using namespace std; const int maxn = * + ;
int Rank[maxn], par[maxn];
void solve(); void init(const int& n) {
for (int i = ; i < n; i++) {
par[i] = i;
Rank[i] = ;
}
} int find(const int& x) {
if (par[x] == x) {
return x;
}
else {
return par[x] = find(par[x]);
}
} void unite(int x, int y)
{
x = find(x);
y = find(y);
if (x == y) return ; if (Rank[x] < Rank[y]) {
par[x] = y;
}
else {
par[y] = x;
if (Rank[x] == Rank[y]) {
++Rank[x];
}
}
} bool same(int x , int y)
{
return find(x) == find(y);
} void solve()
{
int T, N, M;
int x, y; char cmd;
scanf("%d", &T); while (T--)
{
scanf("%d%d", &N, &M);
init(N * ); getchar(); while (M--)
{
//忽略每次输入后的回车符
scanf("%c%d%d%*c", &cmd, &x, &y);
//检查
if (cmd == 'A') {
if (same(x, y)) {
printf("In the same gang.\n");
}
else if (same(x, y + N)) {
printf("In different gangs.\n");
}
else {
printf("Not sure yet.\n");
}
}
else {
//合并 (x, y+N) 或 (x+N, y)
unite(x, y + N);
unite(x + N, y);
}
} } } int main()
{
solve();
return ;
}
并查集:POJ No1703 Find them, Catch them的更多相关文章
- [并查集] POJ 1703 Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- [并查集] POJ 2236 Wireless Network
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 25022 Accepted: 103 ...
- [并查集] POJ 1182 食物链
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66294 Accepted: 19539 Description ...
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network
题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p 代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...
- 并查集--poj 2492
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- 并查集 POJ 1988
#include <cstdio> #define N 30010 int pa[N]; //parent int siz_tree[N]; //size of tree int d[N] ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
随机推荐
- mysql 随机获取一条或多条数据
若要在i ≤r≤ j 这个范围得到一个随机整数r ,需要用到表达式 FLOOR( RAND() * (j – i)+i),RLOOR()取整树部分,RAND()生成0~1的随机数.ROUND(x,n) ...
- js框架总结
参考地址 http://www.techweb.com.cn/network/system/2015-12-23/2245809.shtml https://www.cnblogs.com/mbail ...
- linux下安装jenkins
我们不用离线安装方式 第一步.必须验证java环境 第二步.我们这里使用yum命令进行在线安装,使用service命令进行启动 1.wget -O /etc/yum.repos.d/jenkins.r ...
- iphonex适配
这一篇具体适配步骤比较全面 iphonex适配 这一篇图文讲解比较全面 关于H5页面在iPhoneX适配
- 【Python】python 2 map() reduce()
利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字.输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']. ...
- Machine Learning CodeForces - 940F(带修改的莫队)
题解原文地址:https://www.cnblogs.com/lujiaju6555/p/8468709.html 给数组a,有两种操作,1 l r查询[l,r]中每个数出现次数的mex,注意是出现次 ...
- MVC如何设置启动页
1.解决方案下的项目,右键,属性,Web,特定页,切换下其他选项以保存
- Android热修复原理(一)热修复框架对比和代码修复
在Android应用开发中,热修复技术被越来越多的开发者所使用,也出现了很多热修复框架,比如:AndFix.Tinker.Dexposed和Nuwa等等.如果只是会这些热修复框架的使用那意义并不大,我 ...
- 51nod 1376 最长上升子序列的数量 | DP | vector怒刷存在感!
51nod 1376 最长上升子序列的数量 题解 我们设lis[i]为以位置i结尾的最长上升子序列长度,dp[i]为以位置i结尾的最长上升子序列数量. 显然,dp[i]要从前面的一些位置(设为位置j) ...
- 【bzoj4016】 FJOI2014—最短路径树问题
http://www.lydsy.com/JudgeOnline/problem.php?id=4016 (题目链接) 题意 给出一张无向图,求出它的最小路径树,然后求最小路径树上节点数为${K}$的 ...