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 }
  • 相关阅读:
    WebRTC视频采集中的约束有哪些和具体的使用方法
    解决WebRTC中不同的浏览器之间适配的问题
    WebRTC如何获取音频视频设备
    用C#调用外部DLL
    null值与非null只比较大小时,只会返回false
    jsonp实现js跨域请求
    同一域名的ASP.NET网站实现Session共享
    machinekey相关信息
    从bbs.3dmgame.com与qq的登录解析oauth2.0协议
    asp.net使用wsdl文件调用接口,以及调用SSL接口报错“根据验证过程 远程证书无效”的处理
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3539097.html
Copyright © 2011-2022 走看看