1703

Find them, Catch them

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 32225   Accepted: 9947

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.题目大意:N 表示共有 N 个帮派;下面有 M 条个询问        有两个帮派,现在告诉你一些互相敌对的点对,然后让你判断某两个点之间的关系首先判两个点关系是否确定;        只要两个点在一个集合里就可以了,直接用并查集然后判断同一个集合的关系:想了很久,发现只要转换到根,看看两个点和根的关系就可以了。        只要在结点间加上个权,1表示敌对,0相反。路径压缩和合并集合的时候考虑下权做下变换就可以了。加了个秩快了一点。AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
// 0 代表木有关系,1代表同类,2代表不同类
const int N = 1e5+10;
int m;
int f[N],c[N];
int init()
{
    for(int i=0; i<=m; i++)
    {
        f[i] = i;
        c[i] = 0;
    }
}
int xfind(int x)
{
    if(f[x] == x)
       return x;
       int t = f[x];
       f[x] =xfind(f[x]);
       c[x] = (c[x]+c[t])%2;
       return f[x];
}
int solve(char ch,int x,int y )
{
    int fx = xfind(x);
    int fy = xfind(y);
    if(fx != fy)
    {
        if(ch == ‘A‘) return 0; //返回值
        f[fx] =fy;
        if((c[x]+c[y])%2 ==0)
            c[fx] = 1;
    }
    else
    {
        if(ch == ‘A‘)
           {
               if(c[x] != c[y]) return 1;
                if(c[x] == c[y]) return 2;
           }
    }
    if(ch == ‘D‘) return 3;
}
int main( )
{
    int T,n,x,y;
    char s;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&m,&n);
        init();
        for(int i=0; i<n; i++)
        {
            scanf(" %c %d %d",&s,&x,&y);
            int flag  = solve(s,x,y);
            if(flag == 0) printf("Not sure yet.\n");
            else if(flag == 1) printf("In different gangs.\n");
            else if(flag == 2) printf("In the same gang.\n");
        }
    }
    return 0;
}
时间: 2024-11-06 18:09:45

1703的相关文章

Bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 传递闭包,bitset

1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 323  Solved: 238[Submit][Status][Discuss] Description 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序.    约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他发现,他还需要再做一

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 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

(带关系)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 cit

TC Hangs when using quick search extended on win10 (1703)

https://ghisler.ch/board/viewtopic.php?t=47682 I recently updated windows 10 to the latest released on March 2017 (1703 15067.13). I did not a clean install, because it requires much time to do and i just wanted to try this new version of windows. Ve

Poj(1703),种类并查集

题目链接:http://poj.org/problem?id=1703 已经不是第一次接触种类并查集了,直到今天才搞懂. 感谢红黑联盟,感谢杰哥!!! 每个节点只要关系确定,不管是不是同一个集合里面,都把他们放到一个集合里面,用一个rank[]数组记录他们与根节点的关系,比较神奇的地方有两处: 1.find函数里面,因为find是递归写的,不断往上找,不断更新rank[x](与根节点的关系),这个%k,也是很牛逼的,2种类型,标号只有01: 2.Union函数里面,更新rank[fy],rank

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 1703 - Find them, Catch them(加权并查集) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=1703 题目大意: N个人分属两个黑帮.给出M条信息.(M,N <= 10^5) D x y表示已知x和y不在同一个黑帮中. A x y表示询问x和y是否在一个黑帮中.如果是,输出In different gangs. 如果不是,输出In the same gang.如果未知,输出Not sure yet. 输入一个数字t表示数据组数(1 <= T <=

联想拯救者15-ISK 升级 windows10 1703

摘要:联想拯救者预装的win10版本是1511,强迫症的我希望能用上windows10 1703,抱着试试的态度拿自己的笔记本做实验. 方法: 1.把自己的系统还原到最纯净状态,这样做的原因是我C盘装软件太多,已经少于20个G了,所以还原以腾出空间,也减少因软件问题意外的发生 1)捅菊花 2)选第三个 3) 还原 4) 二十分钟后还原完成 5) 简单的设置后进入系统,用时大约二十分钟 2 下载微软官方的易升工具,百度就有,自己下载,微软官方也有,安装,然后点开(过程大约四个小时,看个人网速) 这

种类并查集,Poj(1703)

题目链接:http://poj.org/problem?id=1703 第一次做种类并查集,有的地方还不是很清楚,想了一上午,有点明白了,这里记录一下. 这里我参考的红黑联盟的题解. 关键:种类并查集与带权并查集实质上的差别并不大, 关键的区别就是种类并查集只是带权并查集再弄个%取余操作而已,然后余数就表示他属于哪个种类. rank数组表示节点和父节点的关系(也可以理解为他的种类). find中,找到x的父节点后,那么更新他与父节点的关系,rank[x] = (rank[x]+rank[fa])