[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. ...
随机推荐
- web自动化:元素定位(二)
一. 实例 如何定位到下图第二个"抢投标",有一种方法是利用xpath定位 //a[@href="/loan/loan_detail/Id/7190.html" ...
- Xcode 查找 TODO 清单
在 Xcode 中按 Shift+Command+F,显示在项目中查找窗口,选择按正则表达式查找(Find > Regular Expression): TODO: //\s*\bTODO\s ...
- JAVA常见函数
输入函数 : Scanner cin=new Scanner(System.in); int a=cin.nextInt(); //输入一个int数据 double dl=cin.nextDou ...
- AI探索(一)基础知识储备
AI的定义 凡是通过机器学习,实现机器替代人力的技术,就是AI.机器学习是什么呢?机器学习是由AI科学家研发的算法模型,通过数据灌输,学习数据中的规律并总结,即模型内自动生成能表达(输入.输出)数据之 ...
- jquery获取上传进度和取消上传操作
var xhrOnProgress=function(fun) { xhrOnProgress.onprogress = fun; //绑定监听 //使用闭包实现监听绑 return function ...
- java学习笔记 --- 多线程(1)
1:要想了解多线程,必须先了解线程,而要想了解线程,必须先了解进程,因为线程是依赖于进程而存在. 2:什么是进程? 通过任务管理器我们就看到了进程的存在. 而通过观察,我们发现只有运行的程序才会出现进 ...
- ICE的连接机制
1.当使用ICE的proxy进行方法调用时,ICE运行环境会建立一个到服务器的连接.当proxy提供了多个endpoint时 默认的ICE运行环境选择endpoint的行为为random,可以通过 ...
- pdp 的连接方法
<?php $dsn = "mysql:host=127.0.0.1;dbname=blog_edu"; try{ $pdo = new PDO($dsn,'root','' ...
- HNOI2004 宠物收养所 (平衡二叉树)
题目链接 平衡树基础题,用于测试各种平衡树的性能(雾) treap: #include<bits/stdc++.h> typedef long long ll; using namespa ...
- LeetCode Largest Palindrome Product
原题链接在这里:https://leetcode.com/problems/largest-palindrome-product/description/ 题目: Find the largest p ...