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两 ...
随机推荐
- codeforces472B
Design Tutorial: Learn from Life CodeForces - 472B One way to create a task is to learn from life. Y ...
- Linux 4.20内核得到更新,英特尔CPU 性能降低50%
根据HKEPC的报道,Linux近日发布了 4.20 内核的一些漏洞修复更新,更新后可能会出现50% 的性能损失,是今年内所有安装Spectre/Meltdown 修补程式中效能跌幅最大的一次. 据报 ...
- List泛型集合
List和数组 相同点: 都可以控制元素类型 不同点: List的长度是可变的,所以list比数组更容易掌控 List属性 1.Count 获取集合中实际包含的元素个数 2.Capcity 集合中可以 ...
- webapi返回泛型给easyui
由于之前遇到的easyui调用webapi的问题. 参见 :http://blog.csdn.net/hanjun0612/article/details/51144991 所以就考虑,封装一个泛型用 ...
- CodeForces 464E The Classic Problem | 呆克斯歘 主席树维护高精度
题意描述 有一个\(n\)点\(m\)边的无向图,第\(i\)条边的边权是\(2^{a_i}\).求点\(s\)到点\(t\)的最短路长度(对\(10^9 + 7\)取模). 题解 思路很简单--用主 ...
- Python--Django学习笔记1
Django:The Web framework for perfectionists with deadlines Django是一个Python语言开发的高级Web框架,采用MVC架构,通过配置可 ...
- AutoCompleteTextView 简单用法 实现自定义list adapter
网上有不少教程,那个提示框字符集都是事先写好的,例如用一个String[] 数组去包含了这些数据,但是,我们也可以吧用户输入的作为历史记录保存 下面先上我写的代码:import andro ...
- 【POJ1151】Atlantis
题目大意:给定 N 个矩形,求这些矩形的面积并. 题解:采用扫描线算法. 首先,按照矩形的横坐标排序,在纵坐标方向上维护一根扫描线被覆盖的长度,在这里采用线段树维护.统计答案时,从左到右扫描 2N 个 ...
- 简单认识python(一)
最近本宝宝被一部小说迷的神魂颠倒的,在网络上四处找免费的小说资源,一直哭唧唧的等待着每天更新的一章.实在是太可怜了,本宝宝决定自己学python,自己抓包小说. 既然知道目的地了,那就和本宝宝一起打怪 ...
- es6中的函数
ES6 允许为函数的参数设置默认值,即直接写在参数定义的后面. function log(x, y = 'World') { console.log(x, y); } log('Hello') // ...