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

思路:赤裸裸的种类并查集,0代表ab属于同一集团,1代表不同,要是不在同个树里面,就是不确定.......

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define M 100005
int father[M],rank[M];
int find(int x)
{
if(x==father[x])
return x;
int tmp=father[x];
father[x]=find(tmp);
rank[x]=(rank[x]+rank[tmp])%2;
return father[x];
}
void liantong(int x,int y)
{
int tmp=find(x);
int tmp1=find(y);
if(tmp!=tmp1)
{
father[tmp1]=tmp;
rank[tmp1]=(2-1+2-rank[y]+rank[x])%2;
} }
int main()
{
int text;
scanf("%d",&text);
while(text--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)
{
father[i]=i;
rank[i]=0;
}
while(m--)
{
char ch[10];
int tmp,tmp1;
scanf("%s%d%d",ch,&tmp,&tmp1);
if(ch[0]=='D')
liantong(tmp,tmp1);
else
{
int x=find(tmp);
int y=find(tmp1);
if(x==y)
{
int r=(2-rank[tmp]+rank[tmp1])%2;
if(r==0)
printf("In the same gang.\n");
else
printf("In different gangs.\n");
}
else
printf("Not sure yet.\n");
}
}
}
return 0;
}

poj1703(种类并查集)的更多相关文章

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

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

  2. poj1703 Find them, Catch them(种类并查集

    题目地址:http://poj.org/problem?id=1703 题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮.输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定), ...

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

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

  4. NOIP2010关押罪犯[并查集|二分答案+二分图染色 | 种类并查集]

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示 ...

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

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

  6. NOI2001|POJ1182食物链[种类并查集 向量]

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65430   Accepted: 19283 Description ...

  7. poj1417(种类并查集+dp)

    题目:http://poj.org/problem?id=1417 题意:输入三个数m, p, q 分别表示接下来的输入行数,天使数目,恶魔数目: 接下来m行输入形如x, y, ch,ch为yes表示 ...

  8. poj1733(种类并查集+离散化)

    题目链接: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第 ...

  9. poj 1182:食物链(种类并查集,食物链问题)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44168   Accepted: 12878 Description ...

随机推荐

  1. 谈谈选用技术的原则,技术学习方法技巧,阅读代码的技巧及其它 MSF的一点心得

    谈谈技术原则,技术学习方法,代码阅读及其它(正文) 这篇文章是前一阵在水木BBS上和别人讨论中偶自己发言的摘编,是偶这几年开发过程完全经验式的总结.完全个人经验,供批判. 一.选用技术的原则 比较规范 ...

  2. Redis学习之路(003)- hiredis安装及测试

    一. hiredis下载地址及C API  github下载:https://github.com/redis/hiredis 安装脚本: #!/bin/zsh git clone https://g ...

  3. Unix And Linux

    摘抄与于:http://www.cnblogs.com/awpatp/category/200255.html vi命令速查图 摘要: Lesson 1 Lesson 2 Lesson 3 Lesso ...

  4. SQL Server 2008中SQL之WaitFor

    SQL Server 2008中SQL应用系列--目录索引 在SQL Server 2005以上版本中,在一个增强的WaitFor命令,其作用可以和一个job相当.但使用更加简捷. 看MSDN: ht ...

  5. 初步了解pandas(学习笔记)

    1 pandas简介 pandas 是一种列存数据分析 API.它是用于处理和分析输入数据的强大工具,很多机器学习框架都支持将 pandas 数据结构作为输入. 虽然全方位介绍 pandas API ...

  6. Oracle 12C -- native left outer join的加强

    在11g中SQL> select count(*)  2    from emp a, dept b, bonus c  3   where a.deptno(+) = b.deptno  4  ...

  7. STVD中将现有工程重命名为另一个工程

    http://blog.csdn.net/sy_lixiang/article/details/47273649 例子:把工程名为Template的工程改为color,把左边红圈部分重命名为右面的名字 ...

  8. unity, particle system Emit from Edge

  9. Tomcat7启动报Error listenerStart错误

    问题 Tomcat7在启动时报错,详细信息如下: 十一月 23, 2013 7:21:58 下午 org.apache.catalina.core.StandardContext startInter ...

  10. Android-Cannot merge new index 66195 into a non-jumbo instruction的解决办法

    转载请注明来源:http://blog.csdn.net/goldenfish1919/article/details/33729679 用eclispe打包的时候报错: [2014-06-23 13 ...