[并查集] POJ 1703 Find them, Catch them
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 43132 | Accepted: 13257 |
Description
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.
2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Input
Output
Sample Input
1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4
Sample Output
Not sure yet.
In different gangs.
In the same gang.
Source
#include<stdio.h>
int father[150500],relation[150050];
void init(int n)
{
int i;
for(i=1;i<=n;++i) father[i]=i;
for(i=1;i<=n;++i) relation[i]=0;
}
int find(int x)
{
int t;
if(x==father[x]) return (x);
t=father[x];
father[x]=find(t);
relation[x]=(relation[x]+relation[t])%2;
return (father[x]);
}
void merge(int x,int y)
{
int find_x=find(x);
int find_y=find(y);
if(find_x!=find_y)
{
relation[find_x]=(relation[y]+1-relation[x]+2)%2;
father[find_x]=find_y;
}
}
int main()
{
int T,n,m,num=0;char c;int x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init(n);
while(m--)
{
getchar();
scanf("%c%d%d",&c,&x,&y);
if(c=='D')
{
merge(x,y);
}else
if(c=='A')
{
if(find(x)!=find(y))
{
printf("Not sure yet.\n");
continue;
}else
if(relation[x]==relation[y])
{
printf("In the same gang.\n");
continue;
}else
if(relation[x]!=relation[y])
printf("In different gangs.\n");
}
}
}
return 0;
}
[并查集] POJ 1703 Find them, Catch them的更多相关文章
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- POJ 1703 Find them, Catch them (数据结构-并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31102 Accepted: ...
- poj 1703 Find them, Catch them 【并查集 新写法的思路】
题目地址:http://poj.org/problem?id=1703 Sample Input 1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4 Sample Output N ...
- 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(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- (中等) 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 ...
随机推荐
- 模板——Tarjan
#include <cstdio> #include <cstring> #include <iostream> #include <vector> u ...
- 【前端】iterable类型的 forEach方法
看三个例子: a = [ age: 1 , age: 2 ] a.forEach (item) -> item.age = 0 # 有效 console.log a # [ # age: 0 # ...
- ie不兼容的几个js问题及解决办法
1.table问题 在动态新增tr或者td时,createElement()一般用appendChild();都不生效,解决办法是用新增tbody,如 var table=document.creat ...
- finish()在dialog中的作用
finish()在dialog中销毁的是dialog所在的活动:
- js遍历table中的每一个元素
function GetInfoFromTable(tableid) { var tableInfo = ""; var tableObj = document.getElemen ...
- Django中的QuerySet查询优化之select_related
在数据库有外键的时候,使用 select_related() 和 prefetch_related() 可以很好的减少数据库请求的次数,从而提高性能.本文通过一个简单的例子详解这两个函数的作用.虽然Q ...
- 一篇讲Java指令重排和内存可见性的好文
在这里: http://tech.meituan.com/java-memory-reordering.html 指令重排和内存可见性(缓存不一致)是两个不同的问题. volatile关键字太强,即阻 ...
- CSS3:clip-path
旧的clip 旧的css也提供了一个clip属性,但这个属性只能用于裁剪一个矩形,其本质是根据overflow:hidden隐藏掉了裁剪外的区域,使用: clip:rect(<top>,& ...
- input type="number"
情景: 移动端 input控件,调数字键盘,在输入小数点时光标会自动跳到小数点前面.安卓6.0.1的版本会这样. question: 1.调原生数字键盘的方法,必须用 type="numbe ...
- 关于STM32的抢占式优先级说明。——Arvin
关于STM32的中断设置.--Arvin 中断 STM32 很多人在配置STM32中断时对固件库中的这个函数NVIC_PriorityGroupConfig()配置优先级分组方式,会很不理解,尤其是看 ...