题目链接:

K - Find them, Catch them

POJ - 1703

题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人。该城有N个罪犯,编号从1至N(N<=100000。将有M(M<=100000)次操作。
D a b 表示a、b是不同帮派
A a b 询问a、b关系。

具体思路:带权并查集模板题。一般并查集都是相同的放在一个联通块里面。对于这个题,我们可以利用一下这个性质,只要是有联系的,都放进一个连通块里面,然后我们查询的时候,如果说他们的祖先一样的话,就首先能够保证是有关系的。然后就开始判断是敌对关系还是朋友关系。我们再加一个数组r[x],表示x和他的祖先的关系,如果r[x]=1代表和祖先是敌对关系,否则就是朋友关系。然后在寻找路径的过程中,不停地更新就可以了。

 #include<iostream>
#include<stdio.h>
#include<map>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int maxn = 2e5+;
int father[maxn];
int r[maxn];
int Find(int t)
{
if(t==father[t])
return t;
int tmp=father[t];
father[t]=Find(father[t]);
r[t]=(r[tmp]+r[t])%;
return father[t];
}
void match(int t1,int t2)
{
int s1=Find(t1);
int s2=Find(t2);
father[s1]=s2;
r[s1]=(r[t1]+r[t2]+)%;//t1和t2是敌对关系
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=; i<=n; i++)
{
father[i]=i;
r[i]=;
}
char str[];
int st,ed;
while(m--)
{
scanf("%s",str);
if(str[]=='A')
{
scanf("%d %d",&st,&ed);
if(Find(st)==Find(ed))
{
if(r[st]==r[ed])
{
printf("In the same gang.\n");
}
else
printf("In different gangs.\n");
}
else
{
printf("Not sure yet.\n");
}
}
else
{
scanf("%d %d",&st,&ed);
match(st,ed);
}
}
return ;
}
}

K - Find them, Catch them POJ - 1703 (带权并查集)的更多相关文章

  1. POJ 1703 带权并查集

    直接解释输入了: 第一行cases. 然后是n和m代表有n个人,m个操作 给你两个空的集合 每个操作后面跟着俩数 D操作是说这俩数不在一个集合里. A操作问这俩数什么关系 不能确定:输出Not sur ...

  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 the ...

  3. poj 1182 (带权并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 71361   Accepted: 21131 Description ...

  4. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...

  5. Navigation Nightmare POJ - 1984 带权并查集

    #include<iostream> #include<cmath> #include<algorithm> using namespace std; ; // 东 ...

  6. Parity game POJ - 1733 带权并查集

    #include<iostream> #include<algorithm> #include<cstdio> using namespace std; <& ...

  7. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  8. (中等) POJ 1703 Find them, Catch them,带权并查集。

    Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...

  9. poj 1703 - Find them, Catch them【带权并查集】

    <题目链接> 题目大意: 已知所有元素要么属于第一个集合,要么属于第二个集合,给出两种操作.第一种是D a b,表示a,b两个元素不在一个集合里面.第二种操作是A a b,表示询问a,b两 ...

随机推荐

  1. mysql 启动和关闭外键约束

    在MySQL中删除一张表或一条数据的时候,出现 [Err] 1451 -Cannot delete or update a parent row: a foreign key constraint f ...

  2. python 模块之-hashlib

    python 模块hashlib import hashlib m=hashlib.md5()         # 生成MD5加密对象 m.update('jiami-string'.encode(' ...

  3. Luogu4238 【模板】多项式求逆(NTT)

    http://blog.miskcoo.com/2015/05/polynomial-inverse 好神啊! B(x)=B'(x)·(2-A(x)B'(x)) 注意ntt的时候防止项数溢出,即将多项 ...

  4. 遍历List、Map删除元素

    遍历List删除元素 方法一: List<String> list = new ArrayList<>(); list.add("1"); list.add ...

  5. Hdoj 1008.Elevator 题解

    Problem Description The highest building in our city has only one elevator. A request list is made u ...

  6. 自学Python1.4-Centos内vim中文乱码问题

    自学Python之路 自学Python1.4-Centos内vim中文乱码问题 1. 登陆的系统---区域语言设置 1.1查看安装中文包: 查看系统是否安装中文语言包 (列出所有可用的公共语言环境的名 ...

  7. Java 实现金额转换 代码示例

    金额转换,阿拉伯数字的金额转换成中国传统的形式如: (¥1011)→(壹仟零壹拾壹元整)输出. 分析: 金额转换,在开发财务相关软件时会经常用到,也是软件本地化的一个需要.一般开发公司或者团队都有相应 ...

  8. luogu3810 陌上花开 (cdq分治)

    求三维偏序 设三维为a,b,c.先对a排序,这样i的偏序就只能<i. 然而排序的时候需要三个维度都判断一遍,最后还要去重,不然会出现实际应该记答案的数出现在它后面的情况. (排序用的函数里不要写 ...

  9. A1036. Boys vs Girls

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  10. 【CF1042D】Petya and Array

    题目大意:给定一个 N 个数组成的序列,给定一个 T,求有多少个区间满足\(\sum_{i=l}^ra[i]<T\). 题解:区间和问题可以用前缀和优化,即:求有多少个区间满足\(sum[r]- ...