ACM题目————Find them, Catch them

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.

并查集的扩展。

//Asimple
#include <iostream>
#include <cstdio>

using namespace std;
const int maxn = 100010;
int fa[maxn];//存贮根    fa[a] 存 a 的根
int r[maxn];// 存贮 fa[a] 与 a 的关系
// 0 则不在一个 gang 里, 1 表示在一个 gang 里
int T, n, m, a, b;
char ch;

void make_set(int n)// 保存根
{
    for(int i=1; i<=n; i++)
    {
        fa[i] = i ;// i 的根是 fa[i]
        r[i] = 1 ;//  在同一个 gang 里
    }
}

int find_set(int a)//  找根节点
{
    if( a == fa[a] ) return a;
    else
    {
        int temp = fa[a] ;
        fa[a] = find_set(fa[a]);
        r[a] = (r[temp] + r[a] + 1 ) % 2 ;
    }
    return fa[a] ;
}

void union_set(int a, int b)
{
    int faa = find_set(a);//找根节点
    int fbb = find_set(b);
    if( faa != fbb )//两个根节点不同,就将其联合起来
    {
        fa[faa] = fbb ;
        r[faa] = ( r[a] + r[b] ) % 2 ;//更新状态
    }
}

int main()
{
    scanf("%d",&T);
    while( T -- )
    {
        scanf("%d%d",&n,&m);
        make_set(n);
        while( m-- )
        {
            getchar();
            scanf("%c%d%d",&ch,&a,&b);
            if( ch == ‘A‘ )
            {
                if( find_set(a) == find_set(b) )
                {
                    if((r[a]+r[b])%2==0) cout << "In the same gang." << endl ;
                    else cout << "In different gangs." << endl ;
                }
                else cout << "Not sure yet." << endl ;
            }
            else union_set(a,b);
        }
    }

    return 0;
}
时间: 2024-10-12 19:30:32

ACM题目————Find them, Catch them的相关文章

杭电ACM题目分类

杭电ACM题目分类 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028. 1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092. 1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196. 1197.1201.1202.1205.1219.1234.123

超强的ACM题目类型总结

转:初期: 一.基本算法:       (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.       (4)递推.       (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:       (1)图的深度优先遍历和广度优先遍历.       (2)最短路径算法(dijkstra,bellman-

我的ACM题目测试数据产生方法

到网上搜了下ACM题目产生方法,却没有搜索到有用的资料,那只能自己钻研一下了,下面把钻研出的结果给大家分享一下. 我们知道,ACM的题目对算法要求很高,OJ上而如何评判一个算法的复杂度靠的是测试数据.一般地,题目需要数据量比较大,不可能人工地去产生,只能借助计算机产生测试数据,而为了避免数据出现规律性,那应该尽量让数据随机.接下来,我们就研究最一般的问题,如何产生int,long long ,大整数,以及字符串? 我们知道,C语言提供了我们一个随机函数rand(),可惜的是它只能产生(1~2^1

ACM题目-Who&#39;s in the Middle

#这题主要用到了sort函数去简化排序的过程(需要声明#include <algorithm>) 描述 FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median' cow gives: half of the cows give as much or more than the median; half give as much or less. Give

HDOJ ACM题目分类

模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201 120

有一种acm题目叫做,奇葩!

本文全然没有技术含量,纯粹是娱乐. 我事实上想写点东西.可是近期好像做计算几何做得太多了,一种想说说不出东西的感觉,唯有写一下一些奇葩的题目了. HDU3337:Guess the number pid=3337"> http://acm.hdu.edu.cn/showproblem.php? pid=3337 题目意思: 猜数字!(32位整数范围内) 正确答案: <span style="font-size:18px;"> #include "s

写给一心刷ACM题目的学生

[来信] 老师你好,我大2013级大二的学生.大一时自学了数据结构,大二的时候参加了ACM. 现在寒假在家里刷题.可是我还想学习些东西,比如深入C 的学习,或者java.但是感觉除了刷题之外,就很迷茫,不知道以后出去工作是C 好还是java好.其实我更喜欢C ,但是如何去深入学习C 呢,我对C 的深入学习也是很迷茫的,因为对于C/C 除了用来刷题,我都不知道他们能够干什么? [回复] 你没有说你的专业,我猜想应该是计算机类的专业.下面就基于这个前提,谈谈我的想法. 很高兴你能找到一个提高编程能力

ACM 题目分类

转自: http://www.cnblogs.com/ltang/articles/1861284.html#top OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739 ,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期:一.基本算法:     (1)枚举. (poj1753,poj2965)    (2)贪心(poj1328,poj2109,poj2586)    (3)递归和分治法.     (4)递

推荐acm题目

杭电  http://acm.hdu.edu.cn/onlineuser.php. 浙大  http://acm.zju.edu.cn/onlinejudge/submit.do?problemId=1http://poj.org/status惟一的阿福 password:14420121 263273 14420121 学校原来网站      http://10.1.5.253:8080/acmhome/welcome.do?method=index