poj1703 Find them, Catch them(种类并查集
题目地址: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&&opr=='A' 2.opr=='D' 3.其他【由于至少有两个帮派的人,当n==2是两个人一定分别是两个帮派的,输出为different】【初始化时记得n从1到n】
#include <cstdio> using namespace std; const int N = +;
int fa[N], rel[N]; void init(int n)
{
for(int i = ; i <= n; i++)
{
fa[i] = i;
rel[i] = ;
}
} //注意下查找时更新rel[]里那个px
int found(int x)
{
if(fa[x] == x)
return x;
int px = fa[x];
fa[x] = found(fa[x]);
rel[x] = (rel[px]+rel[x])%;
return fa[x];
} void unite(int x, int y)
{
int px = found(x);
int py = found(y);
if(px != py)
{
fa[py] = px;
rel[py] = (rel[x]++-rel[y])%;
}
} int main()
{
int n, m, t, a, b;
char opr;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
init(n);
for(int i = ; i < m; i++)
{
getchar();
scanf("%c%d%d", &opr, &a, &b);
if(n == && opr =='A')//2要特判。分类一共是A2、其他A、D
{
printf("In different gangs.\n");
}
else if(opr == 'D')
unite(a, b);
else
{
int pa = found(a);
int pb = found(b);
if(pa != pb)
printf("Not sure yet.\n");
else if(rel[a] == rel[b])
printf("In the same gang.\n");
else
printf("In different gangs.\n");
}
}
}
return ;
}
【特判2wa了好久!!!!!!!!!!!!!!!!!!!初始化函数也写错了。。刚开始写成<n了!!!!!!!!!!!】
poj1703 Find them, Catch them(种类并查集的更多相关文章
- [poj1703]Find them, Catch them(种类并查集)
题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- poj1703 Find them,Catch them 【并查集】
做过一些的带权并查集,再来做所谓的"种类并查集",发现好像就顿悟了. 种类并查集与带权并查集实质上的区别并不大. 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已.然后 ...
- poj1703 Find them, Catch them(并查集)
https://vjudge.net/problem/POJ-1703 9ms多,卡着时间过了.上次一道并查集也是这样,总觉得要学一波并查集的优化.. 续:好像是可以只做一层存放敌人即可. #incl ...
- poj1703 Find them, Catch them(并查集的应用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32225 Accepte ...
- POJ 1703 Find them,Catch them ----种类并查集(经典)
http://blog.csdn.net/freezhanacmore/article/details/8774033?reload 这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- POJ1703 Find them Catch them 关于分集合操作的正确性证明 种类并查集
题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...
- poj1703(种类并查集)
题意:有两个犯罪集团,现在有两种操作,D [a] [b]表示a和b是属于不同犯罪集团的,A [a] [b] 是询问你a和b的关系,如果ab属于同一个犯罪集团,输出In the same gang. ...
随机推荐
- WLC-安装license
在CLI界面安装licenseStep 1 Install a license on the controller by entering this command:①license install ...
- FileUpload之FileItem类的常用方法
http://blog.csdn.net/chinaliuyan/article/details/7002014
- Promise解决回调地狱(多层调用问题)
Promise # Promise 是异步编程的一种解决方案:从语法上讲,promise是一个对象,从它可以获取异步操作的消息:从本意上讲,它是承诺,承诺它过一段时间会给你一个结果.promise有三 ...
- 全排列next_permutation()用法和构造函数赋值
全排列next_permutation()用法 在头文件aglorithm里 就是1~n数组的现在的字典序到最大的字典序的依次增加.(最多可以是n!种情况) int a[n]; do{ }while( ...
- redhat 7.6 安装 git
1.下载git包 下载网址:https://mirrors.edge.kernel.org/pub/software/scm/git/ //自己下载想要的版本 或者直接在Linux 直接使用wge ...
- spring aop @after和@before之类的注解,怎么指定多个切点
有如下两个切点: @Pointcut("execution(public * com.wyh.data.controller.DepartmentController.*(..))" ...
- PAT T1004 To Buy or Not to Buy - Hard Version
暴力搜索加剪枝~ #include<bits/stdc++.h> using namespace std; ; string t; string s[maxn]; int pos[maxn ...
- Solr搜索引擎服务器学习笔记
Solr简介 采用Java5开发,基于Lucene的全文搜索服务器.同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置.可扩展并对查询性能进行了优化,并且提供了一个完善的功能 ...
- Python之json库
JSON简介 JSON (JavaScript Object Notation) 是一种用于表示结构化数据的流行数据格式. 常用于服务器和Web应用程序之间传输和接收数据. 在Python中,JSON ...
- [NOI 2005]瑰丽华尔兹
Description 题库链接 给你一张 \(n\times m\) 的棋盘,棋盘上有一些障碍.一共 \(t\) 个时刻,被分为 \(k\) 段,在每一段中都有一个向上/下/左/右倾斜的趋势(持续时 ...