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两 ...
随机推荐
- 【BZOJ1826】[JSOI2010]缓存交换(贪心)
[BZOJ1826][JSOI2010]缓存交换(贪心) 题面 BZOJ 洛谷 题解 当缓存不满显然直接放进去,满了之后考虑拿走哪一个.不难发现拿走下一次出现时间最晚的那个一定不会更差. 那么用一个堆 ...
- 【转】【JLINK下载失败,STLINK下载失败万能解决方案】JLINK和STLINK都无法下载时的解决办法,此时芯片并没有报废
很多时候都会认为这个板子无法下载了,其实不是的,有解决办法. 原因如下: 由于客户设置的晶振频率和PLL配置错误时,就会出现这种问题,下载一次程序锁住一次板子,搞的人痛不欲生. 还有错误的外设配置,S ...
- A1041. Be Unique
Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...
- A1071. Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- 关于Picasso load本地图片显示失败的探究
今天测试找过来说图片不显示了,查了一下是Picasso加载本地图片没有显示,奇怪了,以前都是这样写为什么现在不行了,难道是Picasso有bug了,怀着激动的心情断点跟进去发现 Picasso所有lo ...
- 投票wchat
https://jingyan.baidu.com/article/0964eca27d3c8d8285f5363f.html 打开微信,进入“通讯录”,如下图中1对应位置. 2 在微信中“添加好友” ...
- Code-force 1003 E Tree Constructing
E. T ...
- Linux网络协议栈(一)——Socket入门(1)
转自: http://www.cnblogs.com/hustcat/archive/2009/09/17/1568738.html 1.TCP/IP参考模型为了实现各种网络的互连,国际标准化组织(I ...
- STM32 --- 什么时候打开复用IO的时钟(比如RCC_APB2Periph_AFIO)
需要用到外设的重映射功能时才需要使能AFIO的时钟,包括外部中断. 外部中断(EXTI)中与AFIO有关的寄存器是AFIO-EXTICR1.2.3,它们是用来选择EXTIx外部中断的输入脚之用. 举例 ...
- 13 款高逼格且实用的 Linux 运维必备工具
转载于民工哥技术之路 1. 查看进程占用带宽情况 - Nethogs Nethogs 是一个终端下的网络流量监控工具可以直观的显示每个进程占用的带宽. 下载:http://sourceforge.ne ...