题目地址: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(种类并查集的更多相关文章

  1. [poj1703]Find them, Catch them(种类并查集)

    题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...

  2. POJ1703Find them, Catch them[种类并查集]

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42416   Accepted: ...

  3. poj1703 Find them,Catch them 【并查集】

    做过一些的带权并查集,再来做所谓的"种类并查集",发现好像就顿悟了. 种类并查集与带权并查集实质上的区别并不大. 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已.然后 ...

  4. poj1703 Find them, Catch them(并查集)

    https://vjudge.net/problem/POJ-1703 9ms多,卡着时间过了.上次一道并查集也是这样,总觉得要学一波并查集的优化.. 续:好像是可以只做一层存放敌人即可. #incl ...

  5. poj1703 Find them, Catch them(并查集的应用)

    Find them, Catch them   Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32225   Accepte ...

  6. POJ 1703 Find them,Catch them ----种类并查集(经典)

    http://blog.csdn.net/freezhanacmore/article/details/8774033?reload  这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...

  7. POJ 1703 Find them, Catch them(种类并查集)

    题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...

  8. POJ1703 Find them Catch them 关于分集合操作的正确性证明 种类并查集

    题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...

  9. poj1703(种类并查集)

    题意:有两个犯罪集团,现在有两种操作,D [a] [b]表示a和b是属于不同犯罪集团的,A [a] [b] 是询问你a和b的关系,如果ab属于同一个犯罪集团,输出In the same gang.   ...

随机推荐

  1. ➡️➡️➡️IELTS Listening

    目录 src numbers and letters src https://ielts-simon.com/ielts-help-and-english-pr/ielts-listening/ nu ...

  2. Python中的代码块及其缓存机制、深浅copy

    一.代码块及其缓存机制 代码块 一个模块.一个函数.一个类.一个文件等都是一个代码块:交互式命令下,一行就是一个代码块. 同一个代码块内的缓存机制(字符串驻留机制) 机制内容:Python在执行同一个 ...

  3. HtmlUnit-API的使用就介绍

    转自:https://www.cnblogs.com/luotinghao/p/3800054.html 网络爬虫第一个要面临的问题,就是如何抓取网页,抓取其实很容易,没你想的那么复杂,一个开源Htm ...

  4. pandas help

    1. read_csv read_csv方法定义: pandas.read_csv(filepath_or_buffer, sep=', ', delimiter=None, header='infe ...

  5. CURL_模拟登录

    <?php $curl = curl_init(); $url = "http://www.imooc.com/user/login"; //$url = "htt ...

  6. laravel 排除csrf验证

    中(*排除所有路由)

  7. GO常量/枚举

    常量中的数据类型只可以是布尔型.数字型(整数型.浮点型和复数)和字符串型. 常量的定义格式: const identifier [type] = value 你可以省略类型说明符 [type],因为编 ...

  8. 使用gcc编译c语言解码ascii码

    vi test.c 输入代码: #include<stdio.h> int main(void) { char *p = (char *)"\xE6\x8A\xB1\xE6\xA ...

  9. 用C/C++创建windows服务程序

    转载:https://blog.csdn.net/chenyujing1234/article/details/8023816 一.演示过程下方代码演示了如何使用vs(C/C++)创建windows服 ...

  10. 微擎系统BUG漏洞解决方法汇总

    微擎微赞系统BUG漏洞解决方法汇总 弄了微擎系统来玩玩,发觉这个系统BUG还不少,阿里云的提醒都一大堆,主要是没有针对SQL注入做预防,处理的办法基本都是用转义函数. 汇总: 1. 漏洞名称: 微擎任 ...