做过一些的带权并查集,再来做所谓的“种类并查集",发现好像就顿悟了。

种类并查集与带权并查集实质上的区别并不大。 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已。然后余数就表示他属于哪个种类。

这题仅仅有两个种类,也就是仅仅有0和1两种, 对于两个不同的种类,那么之间的权值是相差1的,所以依照带权并查集的方法做加上1。然后取余2就可以。

#include<cstdio>

const int N = 100005;
int n, m, f[N], rank[N]; inline void init(){
for(int i=1; i<=n; ++i)
f[i]=i,rank[i]=0;
} int find(int x){
if(x==f[x])return f[x];
int fa=f[x];
f[x] = find(f[x]);
rank[x] = (rank[x]+rank[fa])&1;
return f[x];
} inline bool Union(int x,int y){
int a=find(x), b=find(y);
if(a==b) return false;
f[b] = a;
rank[b] = (rank[x]-rank[y]+1)&1;
} int main(){
int T,a,b,fa,fb;
char ch;
scanf("%d",&T);
while(T--){
scanf("%d%d%*c",&n,&m);
init();
for(int i=0; i<m; ++i){
scanf("%c%d%d%*c",&ch,&a,&b);
if(ch=='D'){
Union(a,b);
}
else{
fa = find(a), fb=find(b);
if(fa==fb){
if(rank[a]==rank[b]) puts("In the same gang.");
else puts("In different gangs.");
}
else
puts("Not sure yet.");
}
}
}
return 0;
}

poj1703 Find them,Catch them 【并查集】的更多相关文章

  1. poj1703 Find them, Catch them 并查集

    poj(1703) Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26992   ...

  2. POJ-1703 Find them, Catch them(并查集&数组记录状态)

    题目: The police office in Tadu City decides to say ends to the chaos, as launch actions to root up th ...

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

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

  4. poj1703--Find them, Catch them(并查集应用)

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

  5. POJ1703-Find them, Catch them 并查集构造

                                             Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...

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

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

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

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

  8. poj1703_Find them, Catch them_并查集

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

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

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

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

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

随机推荐

  1. Java-从一个字符串获取子字符串

    substring函数 package com.tj; public class MyClass implements Cloneable { public static void main(Stri ...

  2. appium+python自动化-adb shell按键操作(input keyevent)

    前言 接着上篇介绍input里面的按键操作keyevent事件,发送手机上常用的一些按键操作 keyevent 1.keyevent事件有一张对应的表,可以直接发送对应的数字,也可以方式字符串,如下两 ...

  3. 使用create datafile... as ...迁移数据文件到裸设备

    下面是一个测试过程 1.首先创建裸设备:root@ultra66 # cd /opt/app/oradata/test             root@ultra66 # lscontrol01.c ...

  4. MAC生成公钥私钥

    前言 需要开发者在本地上使用openssl来生成私钥和公钥 由于mac 自带openssl工具,所以不用像windows那样要下载安装openssl工具 步骤 1.创建一个文件夹,终端进入该文件夹 c ...

  5. [LA3620]Manhattan Wiring

    [LA3620]Manhattan Wiring 试题描述 输入 输出 输入示例 输出示例 数据规模及约定 见“输入” 题解 我们把“连线”的过程改为“铺地砖”的过程,总共有 11 种地砖,每种地砖上 ...

  6. 关于时区、时间戳引起的bug理解

    时间戳定义:0时区1970年1月1日到现在的毫秒数,所以全世界同一时刻的时间戳都是一样的. 北京时间对应时间戳=unix(0时区对应时间的时间戳)-8*60*60*1000(8小时的毫秒数)----- ...

  7. android中自定义下拉框(转)

    android自带的下拉框好用不?我觉得有时候好用,有时候难有,项目规定这样的效果,自带的控件实现不了,那么只有我们自己来老老实实滴写一个新的了,其实最基本的下拉框就像一些资料填写时,点击的时候出现在 ...

  8. 激活win10系统的方法(亲测)

    WIN+X 按A (或者点击左下角有个windows小图标“鼠标右键”选择选择“命令提示符号(管理员)”) 输入下面命令,回车(一行按一个回车键)slmgr.vbs /upkslmgr /ipk W2 ...

  9. 程序包com.sun.image.codec.jpeg不存在解决方法

    https://blog.csdn.net/u011627980/article/details/50436842

  10. 【深入Java虚拟机】之三:类初始化

    类初始化是类加载过程的最后一个阶段,到初始化阶段,才真正开始执行类中的Java程序代码.虚拟机规范严格规定了有且只有四种情况必须立即对类进行初始化: 遇到new.getstatic.putstatic ...