zoukankan      html  css  js  c++  java
  • POJ 1703 Find them, Catch them(并查集)

    简单的并查集类似 食物链那道题。 简单一下

    设a-x, a-y 表示 a属于龙帮和a属于蛇帮,以此来判断从属关系

    题目:

    Find them, Catch them
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 27729   Accepted: 8453

    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.
    

    Source

     
     
    代码:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <queue>
     5 #include <cmath>
     6 using namespace std;
     7 
     8 int N,M;
     9 int f[2*100000+10];
    10 
    11 void init()
    12 {
    13     for(int i=1;i<=2*N;i++)
    14     {
    15         f[i] = i;
    16     }
    17 }
    18 
    19 int find( int x)
    20 {
    21     return f[x] == x? x: f[x] = find(f[x]);
    22 }
    23 
    24 int a,b;
    25 char c;
    26 void readin()
    27 {
    28     getchar();
    29     c = getchar();scanf("%d%d",&a,&b);
    30 }
    31 bool same ( int a, int b)
    32 {
    33     int fa  = find(a) , fb =find( b);
    34     if( fa == fb)return true;
    35     return false;
    36 }
    37 
    38 void uni(int a,int b)
    39 {
    40     int fa = find(a), fb = find(b);
    41     if( fa != fb)
    42         f[fb] = fa;
    43 }
    44 
    45 int main()
    46 {
    47     int T;
    48     cin>>T;
    49     while(T--)
    50     {
    51         cin>>N>>M;
    52         init();
    53         for(int i=0;i<M;i++)
    54         {
    55             readin();
    56 
    57             if(c == 'A')
    58             {
    59                 if (same ( a, b ) || same( a+N , b+N))
    60                     cout<<"In the same gang."<<endl;
    61                 else if( same( a, b+N) || same(b, a+N))
    62                     cout<<"In different gangs."<<endl;
    63                 else
    64                     cout<<"Not sure yet."<<endl;
    65             }
    66             if( c== 'D')
    67             {
    68                 uni ( a, b+N  );
    69                 uni ( b, a+N);
    70             }
    71 
    72         }
    73     }
    74 
    75 
    76     return 0;
    77 }
  • 相关阅读:
    Oracle Database 11g : SQL 基础
    Idea-Java接入银联支付的Demo
    Linux文件系统挂载管理
    Linux文件系统
    使用fdisk进行磁盘管理
    Vim文本编辑器
    Linux系统常用命令
    Linux系统目录架构
    Linux文件基本操作管理
    Linux文件系统的基本结构
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3539097.html
Copyright © 2011-2022 走看看