zoukankan      html  css  js  c++  java
  • Find them, Catch them 并查集

    Problem 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.
    *****************************************************************************************************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<queue>
     6 using namespace std;
     7 int p[101010];
     8 int r[100100];
     9 int n,m;
    10 int find(int x)
    11 {
    12    if(p[x]==x)return x;
    13    int t=p[x];
    14    p[x]=find(p[x]);
    15    r[x]=(r[x]+r[t])%2;
    16    return p[x];
    17 }
    18 void  Unon(int x,int y)
    19 {
    20     int r1=find(x);
    21     int r2=find(y);
    22     p[r1]=r2;
    23     r[r1]=(r[x]+1+r[y])%2;//记住因为此时两队,可用两数相加
    24 
    25 }
    26 
    27 void  set1()
    28 {
    29    for(int i=1;i<=n;i++)
    30    {
    31       p[i]=i;
    32       r[i]=0;
    33    }
    34 }
    35 
    36 int main()
    37 {
    38   int Cas;
    39   char str1;
    40   int a,b;
    41   scanf("%d",&Cas);
    42   while(Cas--)
    43   {
    44     scanf("%d%d%*c",&n,&m);
    45     set1();
    46     for(int i=1;i<=m;i++)
    47     {
    48        scanf("%c%d%d%*c",&str1,&a,&b);
    49        if(str1=='A')
    50        {
    51          if(find(a)==find(b))
    52          {
    53            if(r[a]!=r[b])
    54             printf("In different gangs.
    ");
    55            else
    56             printf("In the same gang.
    ");
    57          }
    58          else
    59           {
    60             printf("Not sure yet.
    ");
    61           }
    62        }
    63        else
    64          if(str1=='D')
    65           Unon(a,b);
    66     }
    67   }
    68 }
    View Code
  • 相关阅读:
    day03—JavaScript中DOM的Event事件方法
    day02-Javascript之document.write()方法
    day01-JavaScript中"Uncaught TypeError: Cannot set property 'innerHTML' of null"错误
    Linux安装Tomcat8
    CentOS7安装jdk8及环境变量配置
    Linux命令之lsof
    java如何停止一个运行的线程?
    大数据技术之Hadoop(HDFS)
    大数据技术之Hadoop入门
    用word2013 把word 文档发送到博客园
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3488442.html
Copyright © 2011-2022 走看看