并查集 POJ 1703 Find them, Catch them

题目传送门

题意:有两个黑帮集团,给出一些两个小弟属于不同的黑帮,询问两个小弟是否关系能确定

分析:首先直接弄两个集合是不好的,正确的做法是类似食物链的做法,关系已确定不属于同一个帮派的x 和 y 使得x 和 y + n属于同一个集合,y 和 x + n属于同一个集合,那么最后只要判断x 和 y 是否在同一个集合就可以了

收获:关系的连通问题

代码:

/************************************************
* Author        :Running_Time
* Created Time  :2015/9/9 星期三 15:17:58
* File Name     :O.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct UF   {
    int rt[N], rk[N];
    void init(void) {
        memset (rt, -1, sizeof (rt));
        memset (rk, 0, sizeof (rk));
    }
    int Find(int x) {
        return rt[x] == -1 ? x : rt[x] = Find (rt[x]);
    }
    void Union(int x, int y)    {
        x = Find (x), y = Find (y);
        if (x == y) return ;
        if (rk[x] > rk[y])  {
            rt[y] = x;  rk[x] += rk[y] + 1;
        }
        else    {
            rt[x] = y;  rk[y] += rk[x] + 1;
        }
    }
    bool same(int x, int y) {
        return Find (x) == Find (y);
    }
}uf;

int main(void)    {
    int T;  scanf ("%d", &T);
    while (T--) {
        int n, m;  scanf ("%d%d", &n, &m);
        uf.init ();
        char s[3];
        for (int x, y, i=1; i<=m; ++i)   {
            scanf ("%s %d %d", &s, &x, &y);
            if (s[0] == ‘D‘)    {
                uf.Union (x, y + n);
                uf.Union (x + n, y);
            }
            else if (s[0] == ‘A‘)   {
                if (uf.same (x, y)) puts ("In the same gang.");
                else if (uf.same (x, y + n))    puts ("In different gangs.");
                else    puts ("Not sure yet.");
            }
        }
    }

    return 0;
}

  

时间: 2024-12-07 09:36:04

并查集 POJ 1703 Find them, Catch them的相关文章

(并查集) poj 1703

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

Catenyms+欧拉回路/欧拉路+并查集+POJ

Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9617   Accepted: 2524 Description A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For e

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

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

POJ 1703 Find them, catch them (并查集)

题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且任意2个集合之间成员的敌我关系不明. 这里每个集合里面的成员关系要记录,他们在一个集合里,只是因为他们关系明确,敌人或者朋友. 千万不要简单的认为成朋友在一个集合,敌人在另外一个集合,分成2个集合.因为题目说只有2个匪帮,很容易进入这个误区. 我们只要记录一个成员和自己父亲的敌我关系和建树就可以了.

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: 32514   Accepted: 10036 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,带权并查集。

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