zoukankan      html  css  js  c++  java
  • POJ-1703-Find them, Catch them

      

    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."

    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.
    题意:两个帮派斗争,A表示询问两个人是不是在一个帮派,D表示两个人不在一个帮派。
    题解:每D一次将两者加入并查集,用0,1记录节点和根节点的同异关系即可。日常卡cin,需要用scanf。
    AC代码:
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 using namespace std;
     5 const int maxn = 100007;
     6 int t,n,m,fa[maxn],c[maxn];
     7 int fi(int x)
     8 {
     9    if(fa[x]==x)return x;
    10    int t = fa[x];
    11    fa[x]=fi(fa[x]);
    12    c[x]=(c[x]+c[t])%2;
    13    return fa[x];
    14 }
    15 void make(char s,int x,int y)
    16 {
    17     int p = fi(x);
    18     int q = fi(y);
    19     if(p!=q)
    20     {
    21         if(s=='A')
    22         {
    23             cout<<"Not sure yet."<<endl;
    24             return;
    25         }
    26         fa[p]=q;
    27         if((c[x]+c[y])%2==0)c[p]=1;
    28     }
    29     else
    30     {
    31         if(s=='A')
    32         {
    33             if(c[x]!=c[y])
    34             {
    35                 cout<<"In different gangs."<<endl;
    36                 return ;
    37             }
    38             else
    39             {
    40                 cout<<"In the same gang."<<endl;
    41                 return ;
    42             }
    43         }
    44     }
    45     return ;
    46 }
    47 int main()
    48 {
    49     scanf("%d",&t);
    50     getchar();
    51     while(t--)
    52     {
    53         scanf("%d %d",&n,&m);
    54         getchar();
    55         for(int i=1;i<=n;i++)
    56         {
    57             fa[i]=i;
    58             c[i]=0;
    59         }
    60         for(int i=1;i<=m;i++)
    61         {
    62             char s;
    63             int a,b;
    64             scanf("%c%d%d",&s,&a,&b);
    65             getchar();
    66             make(s,a,b);
    67         }
    68     }
    69     return 0;
    70 }


  • 相关阅读:
    JS监听组合按键
    XSS初体验
    debuggap,移动端调试新方式
    简析分页逻辑
    【译】Javascript中的数据类型
    【译】typeof null的前世今生
    一个跨域请求的XSS漏洞再续
    H5页面音频自动播放问题
    一个跨域请求的XSS续
    成功自我管理之压力管理
  • 原文地址:https://www.cnblogs.com/sortmin/p/8081581.html
Copyright © 2011-2022 走看看