zoukankan      html  css  js  c++  java
  • 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 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."

    题目大意:有n个人,D a b表示a b位于不同的集合,A a b则代表问a b是否属于同一个集合(在线提问)。

    思路:并查集拓展的简单应用,不多讲。就用rela[x]表示x与当前父节点(不一点是根节点)是否相同。

    代码(313MS):

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int MAXN = 100010;
     8 
     9 int fa[MAXN];
    10 bool rela[MAXN];
    11 
    12 int get_set(int x) {
    13     if(x == fa[x]) return x;
    14     else {
    15         int ret = get_set(fa[x]);
    16         rela[x] ^= rela[fa[x]];
    17         return fa[x] = ret;
    18     }
    19 }
    20 
    21 void unionSet(int x, int y) {
    22     int fx = get_set(x);
    23     int fy = get_set(y);
    24     fa[fy] = fx;
    25     rela[fy] = 1 ^ (rela[x] ^ rela[y]);
    26 }
    27 
    28 void query(int x, int y) {
    29     int fx = get_set(x);
    30     int fy = get_set(y);
    31     if(fx != fy) puts("Not sure yet.");
    32     else if(rela[x] == rela[y]) puts("In the same gang.");
    33     else puts("In different gangs.");
    34 }
    35 
    36 int main() {
    37     int T, n, m, x, y;
    38     char c;
    39     scanf("%d", &T);
    40     while(T--) {
    41         scanf("%d%d", &n, &m);
    42         for(int i = 1; i <= n; ++i)
    43             fa[i] = i, rela[i] = 0;
    44         while(m--) {
    45             scanf(" %c%d%d", &c, &x, &y);
    46             if(c == 'D') unionSet(x, y);
    47             else query(x, y);
    48         }
    49     }
    50 }
    View Code
  • 相关阅读:
    谷歌大规模机器学习:模型训练、特征工程和算法选择 (32PPT下载)
    (转) 深度强化学习综述:从AlphaGo背后的力量到学习资源分享(附论文)
    (转) Supercharging Style Transfer
    Summary on deep learning framework --- TensorFlow
    (转) How a Kalman filter works, in pictures
    Torch 两个矩形框重叠面积的计算 (IoU between tow bounding box)
    C、C++基础和编程风格 (转)
    Linux && shell
    求最短路径的条数
    一个链表中包含环,请找出该链表的环的入口结点。
  • 原文地址:https://www.cnblogs.com/oyking/p/3296237.html
Copyright © 2011-2022 走看看