链接:

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 0x3f3f3f3f int 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])%;
}
return f[x];
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m, i, a, b;
char s[]; scanf("%d%d", &n, &m); memset(r, , sizeof(r));
memset(vis, , sizeof(vis));
for(i=; i<=n; i++)
f[i] = i; for(i=; i<=m; i++)
{
scanf("%s%d%d", s, &a, &b); if(s[]=='D')
{
int fa = Find(a);
int fb = Find(b); if(fa!=fb)
{
f[fa]=fb;
r[fa] = (r[a]-r[b]+) % ;
}
vis[a] = vis[b] = ;
}
else
{
if(vis[a]== || vis[b]==)
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");
else
printf("In different gangs.\n");
}
}
}
} }
return ;
}

(并查集 带关系)Find them, Catch them -- poj -- 1703的更多相关文章

  1. K - Find them, Catch them POJ - 1703 (带权并查集)

    题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...

  2. 浅谈并查集&种类并查集&带权并查集

    并查集&种类并查集&带权并查集 前言: 因为是学习记录,所以知识讲解+例题推荐+练习题解都是放在一起的qvq 目录 并查集基础知识 并查集基础题目 种类并查集知识 种类并查集题目 并查 ...

  3. Find them, Catch them(POJ 1703 关系并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38668   Accepted: ...

  4. 种类并查集——带权并查集——POJ1182;HDU3038

    POJ1182 HDU3038 这两个题比较像(一类题目),属于带权(种类)并查集 poj1182描绘得三种动物种类的关系,按照他一开始给你的关系,优化你的种类关系网络,最后看看再优化的过程中有几处矛 ...

  5. Poj1182 食物链(并查集/带权并查集)

    题面 Poj 题解 这里采用并查集的补集. \(x\)表示同类集合,\(x+n\)表示敌人集合,\(x+n\times2\)表示敌人的敌人集合. 如果当前给出的是一对同类关系,就判断\(x\)是否吃\ ...

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

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

  7. 【并查集&&带权并查集】BZOJ3296&&POJ1182

    bzoj1529[POI2005]ska Piggy banks [题目大意] n头奶牛m种语言,每种奶牛分别掌握一些语言.问至少再让奶牛多学多少种语言,才能使得它们能够直接或间接交流? [思路] ( ...

  8. HDU 3038 How Many Answers Are Wrong 并查集带权路径压缩

    思路跟 LA 6187 完全一样. 我是乍一看没反应过来这是个并查集,知道之后就好做了. d[i]代表节点 i 到根节点的距离,即每次的sum. #include <cstdio> #in ...

  9. 食物链--poj1182(并查集含有关系)

    http://poj.org/problem?id=1182   题意应该就不用说了  再次回到食物链这道题,自己写了一遍,一直wa...原因竟然是不能用多实例,我也是醉了,但是我真的彻底的理解了,那 ...

随机推荐

  1. LINQ to SQL 系列 如何使用LINQ to SQL插入、修改、删除数据 (转)

    http://www.cnblogs.com/yukaizhao/archive/2010/05/13/linq_to_sql_1.html LINQ和 LINQ to SQL 都已经不是一个新事物了 ...

  2. python 简明教程

    第一个源程序 #!/usr/bin/python# Filename : helloworld.pyprint 'Hello world' 执行: $ python helloworld.py 或者  ...

  3. emacs之配置5,窗口位置和大小

    emacsConfig/window-setting.el ;设置窗口位置 ( ) ;设置宽和高 () (if (eq system-type 'darwin) ()) (if (eq system- ...

  4. java中的变量和常量

    也可以先声明后赋值  自动类型转换 1.  目标类型能与源类型兼容,如 double 型兼容 int 型,但是 char 型不能兼容 int 型 2.  目标类型大于源类型,如 double 类型长度 ...

  5. (转)为C# Windows服务添加安装程序

    本文转载自:http://kamiff.iteye.com/blog/507129 最近一直在搞Windows服务,也有了不少经验,感觉权限方面确定比一般程序要受限很多,但方便性也很多.像后台运行不阻 ...

  6. Oracle Database PSU/CPU

    1. 什么是PSU/CPU?CPU: Critical Patch UpdateOracle对于其产品每个季度发行一次的安全补丁包,通常是为了修复产品中的安全隐患. PSU: Patch Set Up ...

  7. PG自动化测试

    安装软件包 yum groupinstall "Development Tools" yum install zlib-devel tcl-devel readline-devel ...

  8. python入门第3篇 pycharm安装及使用

    内容: 1. python开发工具的介绍及安装 2.pycharm的设置及技巧 一.python开发工具的介绍及安装 python下载后就自带了一个官方的IDE,官方的IDE我个人觉得不是很好用,所以 ...

  9. 学习了django对于sqlite3进行了了解,谈谈看法

    学习了django对于sqlite3进行了了解,谈谈看法 由于django默认使用的是sqlite3,写了几个建表语句, 然后数据做下迁移,其实就是建表语句的执行. 一直对sqlite3没有一个直观的 ...

  10. ASPxGridView删除、添加、修改成功后,弹出提示对话框的方法

    分为几步: 1.在aspx文件中添加 function EndCallBack(s, e) {    if (s.cpAlertMsg != "" && s.cpA ...