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 ...
随机推荐
- 《Java程序员面试笔试宝典》之为什么Java中有些接口没有任何方法
由于Java不支持多重继承,即一个类只能有一个父类,为了克服单继承的缺点,Java语言引入了接口这一概念.接口是抽象方法定义的集合(接口中也可以定义一些常量值),是一种特殊的抽象类.接口中只包含方法的 ...
- Unity四种路径总结
四种路径的权限: Application.dataPath 包含游戏数据文件夹的路径(只读) Applicatio ...
- Java并发框架——AQS堵塞队列管理(一)——自旋锁
我们知道一个线程在尝试获取锁失败后将被堵塞并增加等待队列中,它是一个如何的队列?又是如何管理此队列?这节聊聊CHL Node FIFO队列. 在谈到CHL Node FIFO队列之前,我们先分析这样 ...
- JSP九大内置对象和四种属性范围解读
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文首先主要解说了JSP中四种属性范围的概念.用法与实例. 然后在这个基础之上又引入了九 ...
- JavaScript获取某年某月的最后一天
JavaScript获取某年某月的最后一天 1.实现源代码 <!DOCTYPE html> <!-- To change this license header, choose Li ...
- Juqery 中使用 ajax
从 test.js 载入 JSON 数据,附加参数,显示 JSON 数据中一个 name 字段数据. jQuery 代码: $.getJSON("test.js", { name: ...
- sql server 删除所有表和存储过程
1.删除外键约束 DECLARE c1 cursor for select 'alter table ['+ object_name(parent_obj) + '] drop constraint ...
- Android画一个随意拖动的圆形
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactor ...
- Linq 标准查询操作符三
本文介绍了LINQ标准查询操作符.没有这些操作符,LINQ就不会存在.本文为理解这些操作符的功能提供了很好的基础.了解它们将会很有帮助,因为LINQ的各种Provider都是基于这些操作符来完成各自丰 ...
- Sql server 数据库 单用户切换为多用户
使用master 下的sysprocesses 查询 db正在使用的spid 如 select spid from sysprocesseswhere dbid=DB_ID('DbName') 然后执 ...