(带关系)Find them, Catch them -- poj -- 1703

链接:

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])%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");
                        else
                            printf("In different gangs.\n");
                    }
                }
            }
        }

    }
    return 0;
}
时间: 2024-08-28 05:38:38

(带关系)Find them, Catch them -- poj -- 1703的相关文章

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 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 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是同类.

又见关系并查集 以POJ 1182 食物链为例

简单的关系并查集一般很容易根据给出的关系搞出一个有向的环,那么两者之间的关系就变成了两者之间的距离. 对于此题: 若u,v不在一个集合内,则显然此条语句会合法(暂且忽略后两条,下同). 那么将fu 变为 fv的儿子时需加一条权值为 w 的边,w 满足(w + ru)%3 = (rv+ (D == 1? 0 : 1))%3(ru,rv分别为u,v与fv的关系,即距离). 之所以在D == 2时加 1,是因为u吃v表明着u到fv的距离比v到fv的距离大1. 同理,D == 1时,表明两者到fv的距离

poj 1703

// hnldyhy(303882171) 17:07:57 // poj 1703 #include <stdio.h> int p[200005]; void init(int n) { for (int i=1;i<=2*n;i++) p[i]=i; } int find(int x) { if (x==p[x]) return x; return p[x]=find(p[x]); } int main() { int n,m,a,b,test; char c[10]; scanf

【原创】POJ 1703 &amp;&amp; RQNOJ 能量项链解题报告

唉 不想说什么了 poj 1703,从看完题到写完第一个版本的代码,只有15分钟 然后一直从晚上八点WA到第二天早上 最后终于发现了BUG,题目要求的“Not sure yet.”,我打成了“No sure yet.” 然后是RQNOJ的NOIP真题,经典的能量项链 从看完题到写完伪码用了30分钟,敲完全部代码用了10分钟 WA 了7次,次次只能过前三个点,后面全部超时. 不能够啊?我动态规划了都,怎么可能超时? 开始优化主函数. 发现通过一个比较,能把N * (N-1)的复杂度降到C(N,2)

poj 1703 Find them, Catch them(带权并查集和一种巧妙的方法)

Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36176   Accepted: 11090 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 Drago

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

传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accepted: 13065 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 D

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