题意:有N名来自两个帮派的坏蛋,已知一些坏蛋两两不属于同一帮派,求判断给定两个坏蛋是否属于同一帮派。

思路:

解法一: 编号划分

定义并查集为:并查集里的元素i-x表示i属于帮派x,同一个并查集的元素同时成立

可见所有元素个数为2 * N,如果i表示属于帮派A,那么i + N表示属于帮派B,每次输入两个人不在同一帮派的时候,就合并他们分属两个帮派的元素。

#include <iostream>
using namespace std; #define MAX_N 100000 * 2 + 16
int parent[MAX_N];
int height[MAX_N]; void init(const int& n)
{
for (int i = 0; i < n; ++i)
{
parent[i] = i;
height[i] = 0;
}
} int find(const int& x)
{
return parent[x] == x ? x : parent[x] = find(parent[x]);
} void unite(int x, int y)
{
x = find(x);
y = find(y);
if (x == y)return; if (height[x] < height[y])
parent[x] = y;
else
{
parent[y] = x;
if (height[x] == height[y])
++height[x];
}
} bool same(const int& x, const int& y)
{
return find(x) == find(y);
} int main()
{
int T;
cin >> T;
while (T--)
{
int N, M;
cin >> N >> M;
init(N * 2);
char message;
int x, y;
getchar();
while (M--)
{
scanf("%c%d%d", &message, &x, &y);
getchar();
if (message == 'A')
{
if (same(x, y))
{
cout << "In the same gang." << endl;
}
else if (same(x, y + N))
{
cout << "In different gangs." << endl;
}
else
{
cout << "Not sure yet." << endl;
}
}
else
{
unite(x, y + N);
unite(x + N, y);
}
}
}
return 0;
}

解法二:

已知A与B不在一组,B与C不在一组,因为就两组,可得A与C一组。

r[] = 0 表示其根节点属于同一个帮派; r[] = 1表示与其根节点属于不同的帮派。

分析:

开始时初始化自己是自己的父亲 p[x] = x,自己与自己属于同一类 r[x] = 0.
一旦输入 D 断定 x 和 y 属于不同集合后,就连接 x 和 y 所在的树,同时更新 r[]
如果 find(x) 不等于 find(y) 说明还没有判断过 x 与 y 直接输出关系不确定即可
如果 find(x) 等于 find(y),但是他们的r不等,说明属于不同帮派,输出In different gangs.
如果他们的r相等,说明属于同一个帮派,则输出In the same gang
注意:

1.find()函数寻找根节点的时候要不断的更新 r
根据子节点与父亲节点的关系和父节点与爷爷节点的关系,推导子节点与爷爷节点的关系
如果 a 和 b 的关系是 r1, b 和 c 的关系是 r2,
那么 a 和 c 的关系就是(r1 + r2) % 2   //因为只用两种情况所以对 2 取模。

2.Union()联合两棵树的时候也要更新两棵树的根的关系
定义:fx 为 x的根节点, fy 为 y 的根节点
联合时,使得 p[fx] = fy; 同时也要寻找 fx 与 fy 的关系。关系为:(r[x] + r[y] + 1)% 2

#include<cstdio>
const int maxn = 100000 + 10; int p[maxn]; //存父亲节点
int r[maxn]; //存与根节点的关系,0 代表同类, 1代表不同类 int find(int x) //找根节点
{
if (x == p[x]) return x; int t = p[x]; //记录父亲节点 方便下面更新r[]
p[x] = find(p[x]);
r[x] = (r[x] + r[t]) % 2; //根据子节点与父亲节点的关系和父节点与爷爷节点的关系,推导子节点与爷爷节点的关系
return p[x];
} void Union(int x, int y)
{
int fx = find(x);
int fy = find(y); p[fx] = fy;
r[fx] = (r[x] + 1 + r[y]) % 2; //fx与x关系 + x与y的关系 + y与fy的关系 = fx与fy的关系
}
void set(int n)
{
for (int x = 1; x <= n; x++)
{
p[x] = x; //自己是自己的父节点
r[x] = 0; //自己和自己属于同一类
}
} int main()
{
int T;
int n, m;
scanf("%d", &T);
while (T--)
{
scanf("%d%d%*c", &n, &m);
set(n); char c;
int x, y;
while (m--)
{
scanf("%c%d%d%*c", &c, &x, &y); //注意输入
if (c == 'A')
{
if (find(x) == find(y)) //如果根节点相同,则表示能判断关系
{
if (r[x] != r[y]) printf("In different gangs.\n");
else printf("In the same gang.\n");
}
else printf("Not sure yet.\n");
}
else if (c == 'D')
{
Union(x, y);
}
}
}
return 0;
}

POJ 1703 Find them, Catch them (并查集)的更多相关文章

  1. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  2. poj.1703.Find them, Catch them(并查集)

    Find them, Catch them Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  3. POJ 1703 Find them, catch them (并查集)

    题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且 ...

  4. POJ 1703 Find them, Catch them 并查集的应用

    题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...

  5. POJ 1703 Find them, Catch them(并查集高级应用)

    手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...

  6. POJ 1703 Find them, Catch them 并查集,还是有点不理解

    题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派.于是需要一个数组r[]来判断父亲节点和子节点的关系.具体思路可参考http://blog.csdn.net/freezhanacmor ...

  7. [并查集] POJ 1703 Find them, Catch them

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: ...

  8. POJ 1703 Find them, Catch them(种类并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41463   Accepted: ...

  9. 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条臭 ...

  10. POJ 1703 Find them, Catch them (数据结构-并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31102   Accepted: ...

随机推荐

  1. 出栈顺序 与 卡特兰数(Catalan)的关系

    一,问题描述 给定一个以字符串形式表示的入栈序列,请求出一共有多少种可能的出栈顺序?如何输出所有可能的出栈序列? 比如入栈序列为:1 2 3  ,则出栈序列一共有五种,分别如下:1 2 3.1 3 2 ...

  2. android contentprovider内容提供者

    contentprovider内容提供者:让其他app可以访问私有数据库(文件) 1.AndroidManifest.xml 配置provider <?xml version="1.0 ...

  3. C# 面向对象的base的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  4. Java SE之String,字符串和子字符串的存储与区别

    理解String 是怎么占用内存的       来看一个每个String对象的各个属性,一个String包括如下的属性: 一个char数组(是个独立的对象用来存储字符串中的字符) 一个int 的off ...

  5. python作业简单FTP(第七周)

    作业需求: 1. 用户登陆 2. 上传/下载文件 3. 不同用户家目录不同 4. 查看当前目录下文件 5. 充分使用面向对象知识 思路分析: 1.用户登陆保存文件对比用户名密码. 2.上传用json序 ...

  6. Ubuntu/Debian 8 安装 Intel realsense 摄像头驱动

    ## Make Ubuntu/Debian Up-to-date1. sudo apt-get update && sudo apt-get upgrade && su ...

  7. Linux Samba服务主配文件smb.conf中文详解【转】

    转自:https://blog.csdn.net/maotianwang/article/details/52524732 从网上找到描述比较详细的smb.conf中文解释: 服务名:smb 配置目录 ...

  8. python中对列表和循环使用的小练习

    #author devilf product_list = [ (), (), (), (), () ] shop_list = [] salary = input('pls enter your s ...

  9. 编译时bad substitution的解决办法

    由于使用的使用的编译器不同导致, 需要使用shell为 #!/bin/bash 即可.

  10. centos6.5环境自动化运维之puppet实现nginx反向代理功能及puppet安装配置详解

    puppet是一种Linux.Unix.windows平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件.用户.cron任务.软件包.系统服务等.puppet把这些系统实体称之为资 ...