并查集: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 小希的迷宫 基 ...
随机推荐
- 结对项目:四则运算web
1)Coding.Net项目地址 https://git.coding.net/DandelionClaw/WEB_Calculator.git 注:本项目为web端,并且需要连接SQL Server ...
- 性能分析_linux服务器CPU_Load Average
CPU度量Load Average 1. 概念介绍 1.1 Linux系统进程状态 在linux中,process有以下状态: runnable (就绪状态):blocked waiting fo ...
- display:flex 布局教程,弹性布局!
display:flex 布局教程 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现. ...
- mysql EXPLAIN 参数表
测试样式: 参数详情:
- VIM 命令收藏
1.vim#在命令行中输入vim,进入vim编辑器2.i#按一下i键,下端显示 --INSERT--#插入命令,在vim中可能任意字符都有作用3.Esc#退出i(插入)命令进行其它命令使用4.:r f ...
- Memcache介绍与应用场景
一:概念介绍 Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库检索的结果 ...
- Hadoop 2.6.0 HIVE 2.1.1配置
我用的hadoop 是2.6.0 版本 ,hive 是 2.1.1版本进入:/home/zkpk/apache-hive-2.1.1-bin/执行hive 后报错: (1)Exception in t ...
- js & get recursive ids
js & get recursive ids len = 0; bug for(let i = 0; i < 3; i++) { console.log(`i =`, i); let y ...
- python3 执行AES加密方法
cmd执行命令:pip install pycryptodome # -*- coding: utf-8 -*- # __author__ = 'Carry' import base64 from C ...
- DAY4-Flask项目
项目出现的问题: 问题处在import requests.requests库已经安装了啊; 找了半天也不知道具体错误在哪里,根据提示想是不是http.py这个模块与Python内置的同名模块冲突了?所 ...