题意:食物链的弱化版本

解题关键:种类并查集,注意向量的合成。

$rank$为1代表与父亲对立,$rank$为0代表与父亲同类。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long ll;
#define M 100005
int fa[M],rank1[M];
int find1(int x){
if(x==fa[x]) return x;
int tmp=fa[x];
fa[x]=find1(tmp);
rank1[x]=(rank1[x]+rank1[tmp])%;
return fa[x];
}
void unite(int x,int y){
int t=find1(x);
int t1=find1(y);
if(t!=t1){//合并的时候需要用到向量的合成和差
fa[t1]=t;
rank1[t1]=(-rank1[y]+rank1[x])%;
}
}
char ch[];
int main(){
int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
fa[i]=i;
rank1[i]=;
}
for(int i=;i<m;i++){
int tmp,tmp1;
scanf("%s%d%d",ch,&tmp,&tmp1);
if(ch[]=='D') unite(tmp,tmp1);
else{
int x=find1(tmp);
int y=find1(tmp1);
if(x==y){//可以判断出关系
int r=(-rank1[tmp]+rank1[tmp1])%;
if(r==) printf("In the same gang.\n");
else printf("In different gangs.\n");
}
else printf("Not sure yet.\n");
}
}
}
return ;
}

法二:$fa$数组代表$i$属于$A$或$i$属于$B$

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;
const int maxn=;
int fa[maxn*];
int find1(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=find1(x),y=find1(y);
if(x!=y) fa[x]=y;
} int main(){
int T;
scanf("%d",&T);
while(T--){
int N,M,x,y;
char opt[];
scanf("%d%d",&N,&M);
for(int i=;i<=*N;i++) fa[i]=i;
while(M--){
scanf("%s%d%d",opt,&x,&y);
if(opt[]=='A'){
if(find1(x)==find1(y)) printf("In the same gang.\n");
else if(find1(x)==find1(y+N)&&find1(x+N)==find1(y)) printf("In different gangs.\n");
else printf("Not sure yet.\n");
}
else{
unite(x,y+N);
unite(x+N,y);
}
}
}
return ;
}

[poj1703]Find them, Catch them(种类并查集)的更多相关文章

  1. POJ1703Find them, Catch them[种类并查集]

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

  2. poj1703 Find them,Catch them 【并查集】

    做过一些的带权并查集,再来做所谓的"种类并查集",发现好像就顿悟了. 种类并查集与带权并查集实质上的区别并不大. 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已.然后 ...

  3. poj1703 Find them, Catch them(并查集)

    https://vjudge.net/problem/POJ-1703 9ms多,卡着时间过了.上次一道并查集也是这样,总觉得要学一波并查集的优化.. 续:好像是可以只做一层存放敌人即可. #incl ...

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

    Find them, Catch them   Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32225   Accepte ...

  5. POJ 1703 Find them,Catch them ----种类并查集(经典)

    http://blog.csdn.net/freezhanacmore/article/details/8774033?reload  这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...

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

    题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...

  7. poj1703 Find them, Catch them(种类并查集

    题目地址:http://poj.org/problem?id=1703 题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮.输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定), ...

  8. POJ1703 Find them Catch them 关于分集合操作的正确性证明 种类并查集

    题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...

  9. poj1703(种类并查集)

    题意:有两个犯罪集团,现在有两种操作,D [a] [b]表示a和b是属于不同犯罪集团的,A [a] [b] 是询问你a和b的关系,如果ab属于同一个犯罪集团,输出In the same gang.   ...

随机推荐

  1. php开发工程师面必问题

    随着培训机构的增加,越来越多的php从业者流入市场,从而影响了php就业环境.公司对人才的要求越来越高,而技术者本身也要技术过硬,学习越来越多的东西,因为只有这样,你才能跑在别人前面,才不被市场抛弃, ...

  2. python中利用正则表达式匹配ip地址

    现在有一道题目,要求利用python中re模块来匹配ip地址,我们应如何着手? 首先能想到的是ip地址是数字,正则表达式是如何匹配数字的呢? \d或[0-9] 对于这个问题,不要一下子上来就写匹配模式 ...

  3. 解决:phantomjs helloworld.js报错: Can't open 'helloworld.js'

    PhantomJS是一个无界面的,可脚本编程的WebKit浏览器引.它原生支持多种web 标准:DOM 操作,CSS选择器,JSON,Canvas 以及SVG. 当我安装好PhantomJS后,写下第 ...

  4. Java面试题上

    1.面向对象的特征有哪些方面?答:面向对象的特征主要有以下几个方面:- 抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注这些 ...

  5. 关于const_cast转换

    第一次看到const_cast转换,将const指针转换成普通的指针.很自然的想到:什么时候用const_cast?为什么要用它?这根const不是相互矛盾吗? (const_cast<ICef ...

  6. linux命令学习笔记( 2 ) : cd 命令

    Linux cd 命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的. 所以,学习Linux 常用命令,首先就要学好 cd 命令的使用方法技巧. . 命 ...

  7. web 压力测试工具ab压力测试详解

    Web性能压力测试工具之ApacheBench(ab)详解 原文:http://www.ha97.com/4617.html PS:网站性能压力测试是性能调优过程中必不可少的一环.只有让服务器处在高压 ...

  8. poj1778

    在一个 8*8 的棋盘里有一个国王和一些骑士,我们要把他们送到同一顶点上去. 国王能够选择一名骑士作为坐骑,而与骑士一起行动(相当于一个骑士),同一位置, 同一时刻可以有多个骑士.问最少走的步数. 骑 ...

  9. 在Arcmap中加载互联网地图资源的4种方法(转载)

    前一段时间想在Arcmap中打开互联网地图中的地图数据,如影像数据.基础地图数据等,经过简单研究目前总结了四种方法,整理下与大家分享,有些内容可能理解有误,希望大家多多指教.4种方法如下: a)    ...

  10. 部署你的分布式调用链跟踪框架skywalking

    使用docker-compose 一键部署你的分布式调用链跟踪框架skywalking https://www.cnblogs.com/huangxincheng/p/9666930.html 一旦你 ...