Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 30702   Accepted: 9447

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.

题意:

城市里面有2个犯罪团伙,罪犯都属于这两个团伙。如今给你2个罪犯,推断他们是不是一个犯罪团伙。假设当前关系不确定,就输出not sure yet.

并查集的应用,非常A bug's life基本一样。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath> using namespace std; const int maxn = 100005;
int cas, n, m;
int parent[maxn], relation[maxn];
char str[2];
int a, b; int find(int x)
{
if (x == parent[x])
return x;
int px = find(parent[x]);
relation[x] = ((relation[x] + relation[parent[x]])%2);
return parent[x] = px;
} void init()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
parent[i] = i;
relation[i] = 0;
}
} void kruskal(int a, int b)
{
if(n==2 && str[0]=='A')
//题意是说仅仅有两个人时,应该是属于不同的团伙。。可是数据没有这个。。就算不加也A了、、
printf("In different gangs.\n");
else
{
int pa = find(a);
int pb = find(b);
if (pa != pb)
{
if (str[0] == 'D')
{
parent[pa] = pb;
if (relation[b] == 0)
relation[pa] = 1 - relation[a];//表示不同
else
relation[pa] = relation[a];//同样
}
else printf("Not sure yet.\n");
}
else
{
if (str[0] == 'A')
{
if (relation[a] != relation[b])
printf("In different gangs.\n");
else
printf("In the same gang.\n");
}
}
}
} int main()
{
scanf("%d", &cas);
while (cas--)
{
init();
while (m--)
{
scanf("%s%d%d", str, &a, &b);
kruskal(a, b);
}
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

POJ 1703:Find them, Catch them(并用正确的设置检查)的更多相关文章

  1. HDU 3172 Virtual Friends(并用正确的设置检查)

    职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...

  2. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  3. poj.1703.Find them, Catch them(并查集)

    Find them, Catch them Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

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

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

  5. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  6. [并查集] POJ 1703 Find them, Catch them

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

  7. poj 1703 Find them, Catch them(并查集)

    题目:http://poj.org/problem?id=1703 题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派, A后询问 两个人是否属于 同一个帮派. ...

  8. POJ 1703 Find them, Catch them (数据结构-并查集)

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

  9. POJ 1703 Find them, Catch them(确定元素归属集合的并查集)

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

随机推荐

  1. QModelIndex有internalPointer()函数,可以存任何数据,另有QAbstractItemModel::createIndex来创造节点

    整个model的节点数据,都靠它来记录了. 另有一个创造节点的函数(自带函数): QModelIndex QAbstractItemModel::createIndex(int arow, int a ...

  2. Swift - 字符串(String)用法详解

    下面对String常用的属性和方法做个总结 1,判断是否为空:isEmpty 1 2 3 var str:String if str.isEmpty{ } 2,获取字符数量:countElements ...

  3. 发掘ListBox的潜力(一):自动调整横向滚动条宽度

    <自绘ListBox的两种效果>一文帖出之后,从反馈信息来看,大家对这种小技巧还是很认同.接下来我将继续围绕ListBox写一系列的文章,进一步发掘ListBox的潜力,其中包括:自动调整 ...

  4. nyoj 55 懒省事的小明 优先队列 multiset 还有暴力

    懒省事的小明 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 3   描述       小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的果子打了下来,而且按果子的 ...

  5. ExtJs4 笔记(12) Ext.toolbar.Toolbar 工具栏、Ext.toolbar.Paging 分页栏、Ext.ux.statusbar.StatusBar 状态栏

    本篇讲解三个工具栏控件.其中Ext.toolbar.Toolbar可以用来放置一些工具类操控按钮和菜单,Ext.toolbar.Paging专门用来控制数据集的分页展示,Ext.ux.statusba ...

  6. Swift - 数字格式化转成字符串(保留两位小数)

    1,下面是一个浮点类型的数字转成String字符串的例子 1 2 var f = 123.32342342 var s:String = "\(f)" //123.32342342 ...

  7. Prime Path (poj 3126 bfs)

    Language: Default Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Ac ...

  8. Network Panel说明

    Chrome Developer Tools:Network Panel说明   官方资料:Chrome Developer Tools: Network Panel 一.chrome Develop ...

  9. mybatis 的简单使用

    须要用到的包:(这里仅仅是当中一个版本号.其它的百度) mysql-connector-java-5.1.6-bin mybatis-3.2.2 先看项目文件夹: 配置文件mybatisconfig. ...

  10. GCC编译优化指南【作者:金步国】

    GCC编译优化指南[作者:金步国] GCC编译优化指南 作者:金步国 版权声明 本文作者是一位自由软件爱好者,所以本文虽然不是软件,但是本着 GPL 的精神发布.任何人都可以自由使用.转载.复制和再分 ...