POJ 1703:Find them, Catch them(带权的并查集)

Find them, Catch them

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;
}
时间: 2024-11-06 05:37:34

POJ 1703:Find them, Catch them(带权的并查集)的相关文章

Poj 1182种类(带权)并查集

题目链接 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44316 Accepted: 12934 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是

带权值并查集(转)

[POJ 1988] Cube Stacking 我们需要新增两种属性cnt[i]cnt[i]与s[i]s[i],分别表示ii之下的块数和ii所在堆的数量.在路径压缩时,cnt[i] += cnt[f[i]] ,另外在连接操作时,需要动态更新cnt[find(u)]和s[find(v)]的信息. 1 #include <iostream> 2 #define lson l,m,rt<<1 3 #define rson m+1,r,rt<<1|1 4 #define cl

带权值得并查集

Building Block Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 151   Accepted Submission(s) : 57 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description John are playing with

POJ 1984 Navigation Nightmare 二维带权并查集

题目来源:POJ 1984 Navigation Nightmare 题意:给你一颗树 k次询问 求2点之间的曼哈顿距离 并且要在只有开始k条边的情况下 思路:按照方向 我是以左上角为根 左上角为原点 dx[i]为i点距离根的x坐标 dy[]是y坐标 这两个可以通过路径压缩求出 只不过是二维而已 #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int m

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

POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y均能C通信,则x和y可以通信.现在给出若干个操作, O p 代表修复编号为p的电脑 S p q代表询问p和q是不是能通信. 思路: 并查集即可.. 如果修复了一台电脑,则把与它相连距离不超过d的且修复了的放在一个集合里面. #include<cstdio> #include<cstring&

POJ 2492 A Bug&#39;s Life (带权并查集 &amp;&amp; 向量偏移)

题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配关系. 分析 : 本题雷同POJ 1182 食物链, 如果会了那一题, 那现在这题便简单多了, 建议先了解食物链的偏移向量做法.这里也是使用向量的思考方式来进行relation的变化, 这里我令 relation = 0为同性, relation = 1为异性, 接下来的步骤就和食物链的雷同了. 优

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

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 b

poj 1182 食物链 (带关系的并查集)

  食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是同类.

UVA - 11987 Almost Union-Find(带删除的并查集)

Almost Union-Find Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem A Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to implement something simil