poj 1703(带权并查集)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 31840 | Accepted: 9807 |
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
first line of the input contains a single integer T (1 <= T <=
20), the number of test cases. Then T cases follow. Each test case
begins with a line with two integers N and M, followed by M lines each
containing one message as described above.
Output
each message "A [a] [b]" in each case, your program should give the
judgment based on the information got before. The answers might be one
of "In the same gang.", "In different gangs." and "Not sure yet."
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<iostream>
#include<cstdio> using namespace std; int parent[]; int Getparent(int x)
{
if(x!=parent[x])
parent[x] = Getparent(parent[x]);
return parent[x];
} void Union(int x,int y)
{
int a = Getparent(x),b = Getparent(y);
if(a!=b)
parent[b] = a;
} int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<n+n+;i++)
{
parent[i] = i;
}
for(int i=;i<m;i++)
{
char s;
int a,b;
getchar();
scanf("%c %d %d",&s,&a,&b);
if(s=='A')
{
if(Getparent(a) == Getparent(b)||Getparent(a+n)==Getparent(b+n))
printf("In the same gang.\n");
else if(Getparent(a) == Getparent(b+n)||Getparent(b) == Getparent(a+n))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
else
{
Union(a,b+n);
Union(a+n,b);
}
}
}
}
return ;
}
poj 1703(带权并查集)的更多相关文章
- POJ 1703 带权并查集
直接解释输入了: 第一行cases. 然后是n和m代表有n个人,m个操作 给你两个空的集合 每个操作后面跟着俩数 D操作是说这俩数不在一个集合里. A操作问这俩数什么关系 不能确定:输出Not sur ...
- 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; <& ...
- K - Find them, Catch them POJ - 1703 (带权并查集)
题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(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,带权并查集。
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两 ...
- 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 ...
随机推荐
- 腾讯课堂-草图大师 Sketchup 初级到精通视频讲座
腾讯课堂-草图大师 Sketchup 初级到精通视频讲座 草图大师 Sketchup 初级到精通视频讲座
- (转)linux下fork的运行机制
转载http://www.cnblogs.com/leoo2sk/archive/2009/12/11/talk-about-fork-in-linux.html 给出如下C程序,在linux下使用g ...
- Java连接Oracle数据库的示例代码
最基本的Oracle数据库连接代码(只针对Oracle11g): 1.右键项目->构建路径 ->配置构建路径,选择第三项“库”,然后点击“添加外部Jar”,选择 “D:\Oracle\ap ...
- PPT去掉图片白色背景
双击图片,点击菜单栏“删除背景”,用矩形框选中想要的区域,然后将鼠标焦点移到图片外,单击鼠标即可.
- 我在使用vector时候遇到的二逼问题
最近在练习使用STL中德各种容器,像vector,map,set之类的. 然后在使用vector的时候,无意间遇到了一个很二逼的问题. 主要是这样的,请看源码(C++): //错误的写法: #incl ...
- 根据list<Object>中的某个字段排序
compareTo必须是两个对象之间的比较(比如Long,Integer...),以下例子是升序排序 private void businessSort(List<WxDailyBusiness ...
- js中json的转换
//aa='{"id":0,"appId":"app***********Id","appSecret":"a ...
- C#/.NET笔试题
1.简述 private. protected. public. internal.protected internal 访问修饰符和访问权限 private : 私有成员, 在类的内部才可以访问. ...
- html5 音频
目前,web页面上没有标准的方式来播放音频文件,大多数的音频文件是使用插件来播放,而众多的浏览器使用了不同的插件.而html5的到来,给我们提供了一个标准的方式来播放web中音频文件,用户不再为浏览器 ...
- String类中几个简单的常用方法
这里我们就把 info 这个字符串 通过 “ ” 这个分隔符 分割成几部分 并吧没部分添加到 s 数组里面 注意:只有字符串才能分割 分隔符必须是 char 类型 而且是 字符串 里面存在的, 例如我 ...