POJ 1703 Find them, Catch them(带权并查集)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 42463 | Accepted: 13065 |
Description
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.
2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Input
Output
Sample Input
1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4
Sample Output
Not sure yet. In different gangs. In the same gang.
思路
| (爷爷,父亲) | (父亲,儿子) | (爷爷,儿子) |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
2、 Union的时候更新两棵树的关系
定义:fx 为 x的根节点, fy 为 y 的根节点,联合时,使得fa[ fx ] = fy;同时也要寻找 fx 和 fy 的关系,其关系为(r[ x ] + 1 - r[ y ]) % 2;因为确定了 x 和 y 的关系是 1 ,因此 r[ fy ] = (r[ x ] + 1 - r[ y ]) % 2;
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 100005;
int fa[maxn*3];
int find(int x)
{
int r = x;
while (r != fa[r]) r = fa[r];
int i = x,j;
while (i != r)
{
j = fa[i];
fa[i] = r;
i = j;
}
return r;
}
void unite(int x,int y)
{
x = find(x),y = find(y);
if (x != y) fa[x] = y;
}
bool same(int x,int y)
{
return find(x) == find(y);
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
int N,M,x,y;
char opt[5];
scanf("%d%d",&N,&M);
for (int i = 0;i <= 3*N;i++) fa[i] = i;
while (M--)
{
scanf("%s %d %d",opt,&x,&y);
if (opt[0] == 'A')
{
if (find(x) == find(y))
printf("In the same gang.\n");
else if (same(x,y + N) && same(x + N,y))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
else if (opt[0] == 'D')
{
unite(x,y + N);
unite(x + N,y);
}
}
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 100005;
int fa[maxn],r[maxn];
int find(int x)
{
if (fa[x] == x) return fa[x];
int tmp = fa[x];
fa[x] = find(fa[x]);
r[x] = (r[tmp] + r[x]) % 2;
return fa[x];
}
void unite(int x,int y)
{
int fx = find(x),fy = find(y);
if (fx == fy) return;
fa[fy] = fx;
r[fy] = (r[x] + 1 - r[y]) % 2;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
int N,M,x,y;
char opt[5];
scanf("%d%d",&N,&M);
for (int i = 0;i <= N;i++) fa[i] = i,r[i] = 0;
while (M--)
{
scanf("%s %d %d",opt,&x,&y);
if (opt[0] == 'A')
{
if (find(x) == find(y))
{
if (r[x] == r[y]) printf("In the same gang.\n");
else printf("In different gangs.\n");
}
else printf("Not sure yet.\n");
}
else unite(x,y);
}
}
return 0;
}
POJ 1703 Find them, Catch them(带权并查集)的更多相关文章
- 【POJ 1984】Navigation Nightmare(带权并查集)
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
- [poj 2912] Rochambeau 解题报告 (带权并查集)
题目链接:http://poj.org/problem?id=2912 题目: 题目大意: n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000) 接下来m行形 ...
- poj 1733 Parity game【hash+带权并查集】
hash一下然后用带权并查集做模2下的前缀和 #include<iostream> #include<cstdio> #include<map> #include& ...
- POJ 2492 A Bug's Life 带权并查集
题意: 思路: mod2 意义下的带权并查集 如果两只虫子是异性恋,它们的距离应该是1. 如果两只虫子相恋且距离为零,则它们是同性恋. (出题人好猥琐啊) 注意: 不能输入一半就break出来.... ...
- 【poj 1182】食物链(图论--带权并查集)
题意:有3种动物A.B.C,形成一个"A吃B, B吃C,C吃A "的食物链.有一个人对N只这3类的动物有M种说法:第一种说法是"1 X Y",表示X和Y是同类. ...
- POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
- 【POJ 1988】 Cube Stacking (带权并查集)
Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...
- POJ 1703 Find them, Catch them (数据结构-并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31102 Accepted: ...
- POJ - 3728:The merchant (Tarjan 带权并查集)
题意:给定一个N个节点的树,1<=N<=50000 每个节点都有一个权值,代表商品在这个节点的价格.商人从某个节点a移动到节点b,且只能购买并出售一次商品,问最多可以产生多大的利润. 思路 ...
随机推荐
- Java怎么添加背景图片
首先,导入相关的包: import java.awt.BorderLayout; import java.awt.Container; import javax.swing.ImageIcon; im ...
- WPF 自定义雷达图
自定义雷达图表如下: Git下载地址:https://github.com/Kybs0/RadarChartControl 1.创建UserControl,名为“RadarChartControl” ...
- jquery2源码分析系列
学习jquery的源码对于提高前端的能力很有帮助,下面的系列是我在网上看到的对jquery2的源码的分析.等有时间了好好研究下.我们知道jquery2开始就不支持IE6-8了,从jquery2的源码中 ...
- Win Server 2008 RD案例:Client通过Server的浏览器上网
一.简介 RD是Windows Server远程桌面服务,可以实现从客户端运行服务器上的软件.首先在Server安装软件,设置能远程访问的应用和账号,并且创建.rdp快捷方式文件,然后Client打开 ...
- 升级Windows 10 正式版过程记录与经验
升级Windows 10 正式版过程记录与经验 [多图预警]共50张,约4.6MB 系统概要: 预装Windows 8.1中文版 64位 C盘Users 文件夹已经挪动到D盘,并在原处建立了符号链接. ...
- Machine Learning Algorithms Study Notes(6)—遗忘的数学知识
机器学习中遗忘的数学知识 最大似然估计( Maximum likelihood ) 最大似然估计,也称为最大概似估计,是一种统计方法,它用来求一个样本集的相关概率密度函数的参数.这个方法最早是遗传学家 ...
- 【2016-11-5】【坚持学习】【Day20】【通过委托事件,关闭窗口】
Window1 UserControl viewModel 在viewModel 关闭window1
- 洛谷P3407 散步[分组]
题目描述 一条道路上,位置点用整数A表示. 当A=0时,有一个王宫.当A>0,就是离王宫的东边有A米,当A<0,就是离王宫的西边有A米. 道路上,有N个住宅从西向东用1-N来标号.每个住宅 ...
- 洛谷P3406 海底高铁[差分 贪心]
题目背景 大东亚海底隧道连接着厦门.新北.博艾.那霸.鹿儿岛等城市,横穿东海,耗资1000亿博艾元,历时15年,于公元2058年建成.凭借该隧道,从厦门可以乘坐火车直达台湾.博艾和日本,全程只需要4个 ...
- jmeter(五)Sample之JDBC Request
jmeter中取样器(Sampler)是与服务器进行交互的单元.一个取样器通常进行三部分的工作:向服务器发送请求,记录服务器的响应数据和记录相应时间信息 有时候工作中我们需要对数据库发起请求或者对数据 ...
因为确定了 x 和 y 的关系是 1 ,因此 r[ fy ] = (r[ x ] + 1 - r[ y ]) % 2;