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("%d",&test);
    while (test--)
    {
        scanf("%d%d",&n,&m);
        init(n);
        for (int i=1;i<=m;i++)
        {
            scanf("%s%d%d",c,&a,&b);
            if (c[0]==‘A‘)
            {
                if ( find(a)!=find(b) && find(a)!=find(b+n) )
                    printf("Not sure yet.\n");
                else if  ( find(a)==find(b) ) printf("In the same gang.\n");
                else  printf("In different gangs.\n");
            }
            else  //‘D  a  b‘
            {
                if ( find(a)!=find(b+n) )
                {
                    p[find(a)]=find(b+n); //不在同一集合
                    p[find(b)]=find(a+n); //不在同一集合
                }
            }
        }
    }
    return  0;
}

**********************************************************************

芯馨(396842689)  17:
25:
53
#include<iostream>
#include<cstdio>
using namespace std;
#define N 500001
int ei[N],rank[N];

void init(int n)
{
    for (int i=1;i<=n;i++)
        ei[i]=i,rank[i]=0;
}

int find(int x)
{
    if (ei[x]==x) return x;
    int tt=ei[x];
    ei[x]=find(ei[x]);
    rank[x]=(rank[x]+rank[tt])%2;
    return ei[x];
}

void set(int x,int y)
{
    int xx=find(x),yy=find(y);
    if (xx!=yy)
    {
        ei[yy]=xx;
        rank[yy]=(rank[x]+rank[y]+1)%2;
    }
}
int main()
{
    char ch;
    int i,x,y;
    int n,m;
    int t;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d%d",&n,&m);
        init(n);
        for (i=1;i<=m;i++)
        {
            scanf("\n%c %d %d",&ch,&x,&y);
            if (ch==‘D‘)
                set(x,y);
            else
            {
                int xx=find(x),yy=find(y);
                if (xx!=yy)
                    printf("Not sure yet.\n");
                else
                {
                    if (rank[x]==rank[y])
                        printf("In the same gang.\n");
                    else
                        printf("In different gangs.\n");
                }
            }
        }
    }
    return 0;
}

poj 1703,布布扣,bubuko.com

时间: 2024-08-04 12:41:01

poj 1703的相关文章

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

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

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(加权并查集) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接: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 <=

种类并查集,Poj(1703)

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

【POJ 1703】 Find them,Catch them

[题目链接] http://poj.org/problem?id=1703 [算法] 并查集 + 拆点 [代码] #include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #i

poj 1703 Find them, Catch them

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