K - Find them, Catch them POJ - 1703 (带权并查集)
题目链接:
K - Find them, Catch them
题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人。该城有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 (带权并查集)的更多相关文章
- POJ 1703 带权并查集
直接解释输入了: 第一行cases. 然后是n和m代表有n个人,m个操作 给你两个空的集合 每个操作后面跟着俩数 D操作是说这俩数不在一个集合里. A操作问这俩数什么关系 不能确定:输出Not sur ...
- 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 ...
- poj 1182 (带权并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 71361 Accepted: 21131 Description ...
- poj 1733(带权并查集+离散化)
题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...
- Navigation Nightmare POJ - 1984 带权并查集
#include<iostream> #include<cmath> #include<algorithm> using namespace std; ; // 东 ...
- Parity game POJ - 1733 带权并查集
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; <& ...
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- (中等) 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 ...
- poj 1703 - Find them, Catch them【带权并查集】
<题目链接> 题目大意: 已知所有元素要么属于第一个集合,要么属于第二个集合,给出两种操作.第一种是D a b,表示a,b两个元素不在一个集合里面.第二种操作是A a b,表示询问a,b两 ...
随机推荐
- Gitblit 的安装使用
1.下载gitblit,可以网上下载,也可以在下面云盘链接取 gitblit-1.8.0 下载链接:https://pan.baidu.com/s/1x7dnbyDp1FmYjMosJbGR8w 密 ...
- HTML-XML数据解析
HTML代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...
- virtualenv 模块
作用 安装虚拟环境 下载组件 pip3 install virtualenv 使用 命令行创建使用 """ 创建虚拟环境 """ virtu ...
- 11 Zabbix Item类型之Zabbix Calculated 计算型Item类型
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 11 Zabbix Item类型之Zabbix Calculated 计算型Item类型 计算类型 ...
- CF848E Days of Floral Colours——DP+多项式求逆/分治NTT
官方题解:http://codeforces.com/blog/entry/54233 就是由简入繁 1.序列处理,只考虑一个半圆 2.环形处理(其实这个就是多了旋转同构) 然后基于分割线邻居的跨越与 ...
- 51nod1237 最大公约数之和 V3
题意:求 解: 最后一步转化是因为phi * I = Id,故Id * miu = phi 第二步是反演,中间省略了几步... 然后就这样A了......最终式子是个整除分块,后面用杜教筛求一下phi ...
- A1082. Read Number in Chinese
Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese ...
- Java中由常量类引发的思考
我们都知道,定义一个常量类是十分简单的.首先差固件一个类,然后类中声明一些public修饰的静态常量.没错就这么简单: 写好之后,在其他地方使用的时候,直接类名.属性名就可以使用了. 而LZ现在想的是 ...
- 代码实战之AdaBoost
尝试用sklearn进行adaboost实战 & SAMME.R算法流程,博客地址 初试AdaBoost SAMME.R算法流程 sklearn之AdaBoostClassifier类 完整实 ...
- window.scroll原生滚动
window.scroll({ top: , behavior: 'smooth' }) js原生已经支持模拟滚动的效果啦~~~