并查集: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 小希的迷宫 基 ...
随机推荐
- Java编写的电梯模拟系统《结对作业》
作业代码:https://coding.net/u/liyi175/p/Dianti/git 伙伴成员:李伊 http://home.cnblogs.com/u/Yililove/ 对于这次作业,我刚 ...
- Internet History, Technology and Security (Week 2)
Week 2 History: The First Internet - NSFNet Welcome to week 2! This week, we'll be covering the hist ...
- jndi连接数据库配置过程总结
一.我们先找到tomcat安装目录中conf目录下的context.xml更改里面的内容: <?xml version='1.0' encoding='utf-8'?> <Conte ...
- [转帖] 中国SaaS死或生之二: ERP两大邪术,尽出歪门邪路 ---- 挺好玩的
中国SaaS死或生之二: ERP两大邪术,尽出歪门邪路 http://www.cniteyes.com/archives/33753 文章摘要:在数字化浪潮中,油腻ERP大叔的那些“歪门邪术” ...
- Android UI测量、布局、绘制过程探究
在上一篇博客<Android中Activity启动过程探究>中,已经从ActivityThread.main()开始,一路摸索到ViewRootImpl.performTraversals ...
- matplotlib删除地图投影上的等值线及风场
[前言]最近在编写一个气象应用程序,用来显示某一时刻某一地区的气温等值线和风场,程序主要用到了第三方库matplotlib及Basemap.在编写的过程中发现,如果不进行擦除操作直接绘制新的等值线或风 ...
- hbase 过滤器属性及其兼容性
内容来自于<HBASE权威指南>,留存备查,由于版本的原因,可能已经有变化,在应用前兼容性需要测试.
- 【BZOJ4903/UOJ300】【CTSC2017】吉夫特
Description 传送门 简述题意:给一个序列,询问有多少子序列满足其中不会出现\(a\choose b\)是偶数的情况,其中\(a\)在\(b\)前面. Solution 首先探究组合数的 ...
- 20165218 《网络对抗技术》Exp1 逆向及Bof基础
Exp1 逆向及Bof基础 基础知识 1. NOP, JNE, JE, JMP, CMP汇编指令的机器码 指令 机器码 NOP NOP指令即"空指令",在x86的CPU中机器码为0 ...
- IntelliJ IDEA详细配置和使用教程-字体、编码和基本设置
IDEA使用--字体.编码和基本设置 https://blog.csdn.net/frankcheng5143/article/details/50779149 IntelliJ IDEA详细配置和使 ...