[poj1703]Find them, Catch them(种类并查集)
题意:食物链的弱化版本
解题关键:种类并查集,注意向量的合成。
$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(种类并查集)的更多相关文章
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- poj1703 Find them,Catch them 【并查集】
做过一些的带权并查集,再来做所谓的"种类并查集",发现好像就顿悟了. 种类并查集与带权并查集实质上的区别并不大. 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已.然后 ...
- poj1703 Find them, Catch them(并查集)
https://vjudge.net/problem/POJ-1703 9ms多,卡着时间过了.上次一道并查集也是这样,总觉得要学一波并查集的优化.. 续:好像是可以只做一层存放敌人即可. #incl ...
- poj1703 Find them, Catch them(并查集的应用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32225 Accepte ...
- POJ 1703 Find them,Catch them ----种类并查集(经典)
http://blog.csdn.net/freezhanacmore/article/details/8774033?reload 这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- poj1703 Find them, Catch them(种类并查集
题目地址:http://poj.org/problem?id=1703 题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮.输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定), ...
- POJ1703 Find them Catch them 关于分集合操作的正确性证明 种类并查集
题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...
- poj1703(种类并查集)
题意:有两个犯罪集团,现在有两种操作,D [a] [b]表示a和b是属于不同犯罪集团的,A [a] [b] 是询问你a和b的关系,如果ab属于同一个犯罪集团,输出In the same gang. ...
随机推荐
- 下载 OS X 10.11 GM
不清楚为什么OS X 10.11 GM版本是Coming Soon,可以通过下面简单方法启用App Store下载. 在终端执行: $ sudo softwareupdate --clear-cata ...
- javascript中some,every,map,filter是只用和ansyc中的each,eachLimit,map,mapLImit,filter的使用
var t = [1,2,3,4,5]; //some找到数组中第一个符合要求的值后就不在继续执行//用来判断数组中是否存符合要求的值,返回结果true|false//function返回类型为boo ...
- mobiscroll时间控件
https://docs.mobiscroll.com/3-0-0_beta/javascript/numpad#events 这个是官方的日期插件,日历,什么效果都有,很强大的. mobiscrol ...
- xml获取指定节点的路径
引用自http://www.w3school.com.cn/xpath/xpath_syntax.asp XPath 语法 Previous Page Next Page XPath 使用路径表达式来 ...
- Codeforces Round #259(div2)C(数学期望)
数学题. 关键是求最大值为k时有多少种情况,结果是kn-(k-1)n-1.可以这么想:每一次都从1至k里选,共kn种,这里需要再减去每一次都从1至k-1里面选的情况.当然也可以分类计数法:按出现几次k ...
- 1.mysql优化---优化入门之MySQL的优化介绍及执行步骤
优化到底优化什么? 优化,一直是面试最常问的一个问题.因为从优化的角度,优化的思路,完全可以看出一个人的技术积累.那么,关于系统优化,假设这么个场景,用户反映系统太卡(其实就是高并发),那么 ...
- C++继承细节 -1
为什么基类析构函数最好要使用 virtual 进行修饰? class A { private: ...... public: ~A(); A() {} }; class B : public A { ...
- Smarty的模板中不允许PHP的代码?
/****************************************************************************** * Smarty的模板中不允许PHP的代 ...
- HNOI2004 宠物收养所 (平衡二叉树)
题目链接 平衡树基础题,用于测试各种平衡树的性能(雾) treap: #include<bits/stdc++.h> typedef long long ll; using namespa ...
- java-04类和对象课堂练习
1.请运行并输入以下代码,得到什么结果 public class Test { public static void main(String[] args){ Foo obj1=new Foo(); ...