链接:

http://poj.org/problem?id=1703

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 36768   Accepted: 11294

Description

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:

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

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.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <queue>
#include <algorithm>
using namespace std;#define N 100005
#define INF 0x3f3f3f3fint f[N], r[N], vis[N];int Find(int x)
{int k = f[x];if(x!=f[x]){f[x] = Find(f[x]);r[x] = (r[x]+r[k])%2;}return f[x];
}int main()
{int t;scanf("%d", &t);while(t--){int n, m, i, a, b;char s[10];scanf("%d%d", &n, &m);memset(r, 0, sizeof(r));memset(vis, 0, sizeof(vis));for(i=0; i<=n; i++)f[i] = i;for(i=1; i<=m; i++){scanf("%s%d%d", s, &a, &b);if(s[0]=='D'){int fa = Find(a);int fb = Find(b);if(fa!=fb){f[fa]=fb;r[fa] = (r[a]-r[b]+3) % 2;}vis[a] = vis[b] = 1;}else{if(vis[a]==0 || vis[b]==0)printf("Not sure yet.\n");else{int fa = Find(a);int fb = Find(b);if(fa!=fb)printf("Not sure yet.\n");else{if(r[a]==r[b])printf("In the same gang.\n");elseprintf("In different gangs.\n");}}}}}return 0;
}

转载于:https://www.cnblogs.com/YY56/p/4789802.html

(并查集 带关系)Find them, Catch them -- poj -- 1703相关推荐

  1. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

  2. 树形结构 —— 并查集 —— 带权并查集

    [概述] 定义:带权并查集即是结点存有权值信息的并查集. 适用:当两个元素之间的关系可以量化,并且关系可以合并时,可以使用带权并查集来维护元素之间的关系. 权值:带权并查集每个元素的权通常描述其与并查 ...

  3. Find them, Catch them POJ - 1703(种类并查集)

    题意: 在这个城市里有两个黑帮团伙,现在给出N个人,问任意两个人他们是否在同一个团伙 1.输入D x y代表x于y不在一个团伙里 2.输入A x y要输出x与y是否在同一团伙或者不确定他们在同一个团伙 ...

  4. 【NOI2002】【Luogu1196】银河英雄传说(并查集带边权)

    problem 有30000条队列,初始每条队列一条战舰,编号为1-30000. 给出T条指令,分为合并和询问, 1.合并指令为: M i j 含义为第i号战舰所在的整个战舰队列,作为一个整体(头在前 ...

  5. 并查集一般高级应用的理解

    并查集高级应用一般是带权并查集,首先肯定要维护每个元素之间相应的关系,一个简单的数组要实现这个功能,则要将数组开大,将数组分为多个段,使每一段数组 代表不同的含义,而对于段与段之间的联系,就实现了对并 ...

  6. 我知道并查集的可爱之处

    请开始您的并查集之旅 迈入门槛 文字概念 故事配图辅助理解 浅尝辄止 畅通工程 程序自动分析 supermarket 慢慢深入 银河英雄传说 parity game 1.带权并查集 2.扩展域并查集 ...

  7. 并查集kuangbin专题

    把内容迁到这里来了,作为一部分补充 初始化: for(int i = 1;i <= n; ++i)f[i] = i; 路径压缩: int find(int t) {if(f[t] == t) r ...

  8. 图论500题 ---- 并查集求路径上最大值最小不超过K的点对数 HDU Portal

    题目链接 题目大意: 就给你一个图,qqq次询问,问你这个图上有多少对点之间的所以路径上的最大值的最小值不超过kkk? 解题思路: 首先我们知道这本质上就是求两个点联通的路径上的最大值最小是多少? 那 ...

  9. ACM入门之【并查集】

    并查集是一种树形的数据结构,顾名思义,它用于处理一些不交集的 合并 及 查询 问题. 它支持两种操作: 查找:确定某个元素处于哪个子集. 合并:将两个子集合并成一个集合. 基本模板: const in ...

最新文章

  1. 一周焦点 | 陆奇“入驻” YC;TensorFlow 2.0 即将发布
  2. C# 窗体实例化一次
  3. python list 去重_Python中对列表list去重
  4. 性能测试的概念及其分类
  5. OpenCV 中的图像处理 004_平滑图像
  6. mac版本查看日志命令
  7. android 元素点击位置,appium自动化操作之元素定位点击事件全家桶(find_element_by、find_elements_by)...
  8. 神奇的编码C语言,神奇的fans (C语言代码)
  9. Linux-Shell脚本编程-学习-4-Shell编程-操作数字-加减乘除计算
  10. CTF中一点进制转换脚本记录
  11. c语言万年历节日算法,万年历星期的算法(C语言)
  12. STM32L051低功耗STOP模式下RTC唤醒
  13. unity手机端发布
  14. red hat linux 9下载地址,Red Hat Linux 9.0 ISO下载 (转)
  15. Windows Mobile 6.1模拟器发布,欢迎下载!
  16. VSCode的一些小操作
  17. M3800的故事——Ipad mini2·电源1
  18. ADI Blackfin DSP处理器-BF533的开发详解59:DSP控制ADXL345三轴加速度传感器的应用2(含源码)
  19. 黑马程序员————高新技术————eclipse及IDE开发工具介绍
  20. python爬取qq好友_Python3实现QQ机器人自动爬取百度文库的搜索结果并发送给好友(主要是爬虫)...

热门文章

  1. 使用java搭建直播平台,我就不信你还听不明白了!
  2. 字节跳动学习笔记:java多线程实现原理
  3. MLIR(Multi-Level Intermediate Representation)概述
  4. 一文弄懂SSD目标检测算法
  5. 是啥意思_属猴人:十猴九苦是啥意思 十猴九不全什么意思 为什么
  6. mybatis 创建session, 缓存, 执行SQL
  7. java读取pdf_Java 读取PDF中的文本和图片的方法
  8. 软件开发计划_敏捷软件开发实践:估算与计划读书笔记113第11章 确定渴望度优先级...
  9. java核心api_java核心API
  10. linux密码修改时间,linux 查看、修改用户及密码过期时间(示例代码)