题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集合,朴素并查集只能查询两者是否属于同一个集合,扩展并查集可以建立多个集合之间的关系. 本题我看了很多博客,对于两个集合,有许多博客都是采取2*n大小的并查集解决.大家的说法都是1-n属于一个集合,n-2*n属于另一个集合,我看的云里雾里,下面我想用我的方法来证明这样划分两个集合的正确性. 证明:我们…
题目链接:http://icpc.njust.edu.cn/Problem/Pku/1182/ 题意:给出动物之间的关系,有几种询问方式,问是真话还是假话. 定义三种偏移关系: x->y 偏移量0时 x和y同类 x->y 偏移量1时 x被y吃 x->y 偏移量2时 x吃y 定义 rela[x]=rx->x; 如x,y不在同一个集合中, 由rx->ry=rx->x + x->y + y->ry=(rx->x)+(x->y)-(ry->y)可得…
题目地址:http://poj.org/problem?id=1703 题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮.输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定),D x y表示x和y不在一个帮派. 思路:种类并查集.维护一个数组rel[x]表示x和根节点的关系(0表示在一个帮派,1表示不在),初始全为0(自己和自己在一个帮派),更新rel用偏移量(rel[x]表示px->x,D x y表示x->y=1).由于2要特判,输入里分三类:1.n==2&&…
Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42416   Accepted: 13045 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Drago…
Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 32909Accepted: 10158 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, t…
Find them, Catch them 直接翻译了 Descriptions 警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<=100000.将有M(M<=100000)次操作.D a b 表示a.b是不同帮派A a b 询问a.b关系 Input 多组数据.第一行是数据总数 T (1 <= T <= 20)每组数据第一行是N.M,接下来M行是操作 Output 对于每一个A操作,回答"In the same gang.…
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49867 Accepted: 15320 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon an…
题目链接:http://poj.org/problem?id=1703 题目大意:给n个人,m次询问.A代表询问a, b之间的关系,D代表给出a, b属于不同的帮派. 我的想法: 太菜了,上课的时候没想到这种方法. 思路: 1.感觉有点像2-sat的思想,对于D给出的a, b两人属于不同帮派.我们就将a于b的对立点用并查集维护在一个集合中,也将b于a的对立点连通. 2.查询时,若a,b两人pre值相等则属于同一帮派.若a与b的对立点属于同一集合,或b与a的对立点属于同一集合,则说明a与b属于不同…
题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<cmath> using namespace std; typedef long long ll; #de…
Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41463   Accepted: 12753 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Drago…