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 TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)
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:
D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.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:
The 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
For 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.
题意:
给你N个英雄,这些英雄术语两个阵营。D a b表示知道a和b在对立阵营,A a b表示询问a和b是否属于同一阵营或者不确定关系。
分析:
敌人的敌人就是朋友,用一个数组记录关系:vis[a] = b表示a的敌人是b,然后并查集把对立的人和对立的人放到一个集合里,具体看代码实现。
#include<cstdio>
using namespace std;
const int maxn = 1e5+5;
int f[maxn],vis[maxn];
int t,n,m,a,b;
int get(int x){
return f[x] == x?x : f[x] = get(f[x]);
}
void merge(int x,int y){
f[get(x)] = get(y);
}
void init(){
for (int i = 1; i <= n; i++){
f[i] = i;
vis[i] = 0;
}
}
int main(){
char ch;
scanf("%d",&t);
while (t--){
scanf("%d%d",&n,&m);
init();
for (int i = 1; i <= m; i++){
getchar();
scanf("%c",&ch);
if (ch == 'D'){
scanf("%d%d",&a,&b);
if (vis[a]) merge(vis[a],b);
if (vis[b]) merge(vis[b],a);
vis[a] = b;vis[b] = a;
}
else{
scanf("%d%d",&a,&b);
if (get(a) == get(b)) printf("In the same gang.\n");
else if (get(vis[a]) == get(b)) printf("In different gangs.\n");
else printf("Not sure yet.\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 ...
- 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 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- POJ 1703 Find them, Catch them 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
- POJ 1703 Find them, Catch them 并查集,还是有点不理解
题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派.于是需要一个数组r[]来判断父亲节点和子节点的关系.具体思路可参考http://blog.csdn.net/freezhanacmor ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
- [并查集] POJ 1703 Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: ...
- POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
随机推荐
- 一百一十、SAP的OO-ALV之四,定义屏幕相关变量和逻辑流
一.代码如下,定义相关变量 二.来带屏幕页面,双击STATUS_9000和USER_COMMAND_9000,自动生成相应代码 三.点击是 四.会自动生产关联的Includ文件 五.我们自己创建一个M ...
- 通过html5 touch事件封装手势识别组件
html5移动端新增了touchstart,touchmove,touchend事件,利用这3个事件,判断手指的点击和划动轨迹,我们可以封装各种手势的识别功能, 这3个事件和pc端的mousedown ...
- Java的包装类
一.概述 因为基本数据类型的变量身上没有任何的方法和属性,所以针对基本数据类型提供了对应的类形式--包装类. 利用这个类产生对象,调用对象身上的方法来操作这个数据. 二.分类 包装类分为以下几种: 基 ...
- linux命令之strace简单使用
strace是什么 strace是一个可用于诊断.调试和教学的Linux用户空间跟踪器.我们用它来监控用户空间进程和内核的交互,比如系统调用.信号传递.进程状态变更等. 使用方式 strace 使用帮 ...
- c++程序—三目运算符
#include<iostream> using namespace std; #include<string> int main() { //三目运算符 ; ; ; c = ...
- P 1023 组个最小数
转跳点:
- 大数据高可用集群环境安装与配置(04)——安装JAVA运行环境
Hadoop运行在java环境,所以在安装Hadoop之前,需要安装好jdk 提前下载好jdk安装包(jdk-8u161-linux-x64.tar.gz),将它上传到指定的安装目录当中,然后运行安装 ...
- 大二暑假第二周总结--开始学习Hadoop基础(一)
一.简单视频学习Hadoop的处理架构 二.简单视频学习分布式文件系统HDFS并进行简单的实践操作 简单操作教程:http://dblab.xmu.edu.cn/blog/290-2/ 注意:在建立H ...
- part9 公用图片画廊组件拆分
1.src中创建 common 再创建 gallery.然后gallery.vue 2.build 中webpack.base.conf 中配置更短路径 module.exports {}中 reso ...
- Git--rebase合并提交
参考 https://blog.csdn.net/hj7jay/article/details/78809547 https://blog.csdn.net/yangcs2009/article/de ...