zoukankan      html  css  js  c++  java
  • ACM Find them, Catch them

    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 /*
     2     Name: Find them, Catch them 
     3     Copyright: 
     4     Author: 
     5     Date: 09/08/17 09:27
     6     Description: 给定两个集合,通过已给信息,判断两个元素是否同一集合。
     7                 如果是,则输出 "In the same gang."
     8                 如果不是,则输出"In different gangs."
     9                 如果不能从已给信息判断,则输出 "Not sure yet."
    10 */
    11 #include<iostream>
    12 #include<algorithm>
    13 #include<stdio.h>
    14 using namespace std;
    15 const int N = 500005;
    16 int parent[N],rank[N];
    17 
    18 void init() /*初始化*/
    19 {
    20     for(int i = 0; i < N; i++)
    21     {
    22         parent[i] = i;
    23         rank[i] = -1;
    24     }
    25             
    26 } 
    27 int find(int n)
    28 {
    29     if(n !=parent[n])
    30         parent[n] = find(parent[n]);
    31     return parent[n];
    32 }
    33 /*
    34 使用这个函数竟然给我TLE  =  =、
    35 int find(int n)
    36 {
    37     if(n == parent[n])
    38         return n;
    39     else
    40         return find(parent[n]);
    41 }
    42 */
    43 void merge(int a,int b)
    44 {
    45      a = find(a);
    46      b = find(b);
    47      if(a != b)
    48          parent[a] = b;    
    49 }
    50 int main()
    51 {
    52     int t,m,n,a,b;
    53     char ch;
    54     while(cin>>t)
    55     {
    56         while(t--)
    57         {
    58             int temp = 0;
    59             init();
    60             scanf("%d %d",&n,&m);
    61             for(int i = 0; i < m; i++)
    62             {
    63                 getchar();
    64                 scanf("%c %d %d",&ch,&a,&b);
    65                 //cin>>Node[i].ch>>Node[i].a>>Node[i].b;  在循环次数较多情况下使用scanf 使用cin容易TLE! 
    66                 if(ch == 'D')
    67                 {
    68                     if(rank[a] != -1)  merge(rank[a],b);
    69                     if(rank[b] != -1)  merge(rank[b],a);
    70                      rank[a] = b;
    71                      rank[b] = a;
    72                     
    73                 }else{
    74                     if( find(a) == find(b))
    75                         printf("In the same gang.
    ");
    76                     else if(find(rank[a]) == find(b))
    77                         printf("In different gangs.
    ");
    78                     else
    79                         printf("Not sure yet.
    ");
    80                 }
    81             }
    82         }
    83     }
    84     return 0;
    85 }
  • 相关阅读:
    mac creact-react-app 时包 gyp-error
    JAVA,JDBC报错Access denied for user '"root"'@'localhost
    JAVA,JDBC连接数据库报错:No suitable driver found for "jdbc:mysql://localhost:3306
    JAVA,反射使用demo,通过读取配置文件创建类,调用方法
    JAVA,反射获取class对象的3种方式
    python获取当前时间距离指定时间相差的秒值
    JAVA,字节流文件读写
    JAVA,反射常用方法
    JAVA,字符流读写,flush和close的区别
    JAVA,字符流文件读写
  • 原文地址:https://www.cnblogs.com/jj81/p/7325115.html
Copyright © 2011-2022 走看看