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

    Find them, Catch them
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 33780   Accepted: 10432

    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.并查集

    2.

    逻辑判断1:a与fa的关系与fa与f[a]的关系相同,也就是对于fa,a和f[a]等价,所以 a和f[a]一定在同一组织;a与fa的关系与fa与f[a]的关系不同(相反),也就是对于fa,a和f[a]不等价,所以a和f[a]一定不在同一组织。

    逻辑判断2:a、b必然不在同一组织中,4种情况

     1 //pojFind them, Catch them  1703
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<string>
     6 #include<algorithm>
     7 #include<iostream>
     8 #include<stack>
     9 #include<set>
    10 #include<queue>
    11 using namespace std;
    12 int f[100005];
    13 bool c[100005];
    14 void Init(int n){
    15     int i;
    16     for(i=0;i<=n;i++){
    17         f[i]=i;
    18         c[i]=true;//true表示与父节点在同一组织内
    19     }
    20 }
    21 int find_set(int a){
    22     int fa=f[a];
    23     if(f[a]!=a){
    24         f[a]=find_set(f[a]);
    25     }
    26     c[a]=(c[fa]==c[a])?true:false;//逻辑判断1
    27     return f[a];
    28 }
    29 void Union(int a,int b,int fa,int fb){
    30     f[fa]=fb;
    31     c[fa]=(c[a]==c[b])?false:true;//逻辑判断2
    32 }
    33 int main(){
    34     //freopen("D:\INPUT.txt","r", stdin);
    35     int T,n,m,i,a,b;
    36     char ch;
    37     cin>>T;
    38     while(T--){
    39         scanf("%d %d
    ",&n,&m);//cin>>n>>m;
    40         Init(n);
    41         for(i=0;i<m;i++){
    42             // cin 会导致TLE
    43             scanf("%c %d %d
    ",&ch,&a,&b);
    44             //cin>>ch;
    45             //cout<<ch<<endl;
    46             //cin>>a>>b;//scanf("%c %d %d
    ",&ch,&a,&b);//cin>>ch>>a>>b;
    47             int fa,fb;
    48             fa=find_set(a);
    49             fb=find_set(b);
    50             if(ch=='A'){
    51                 if(fa==fb){
    52                     if(c[a]==c[b]){
    53                         cout<<"In the same gang."<<endl;
    54                     }
    55                     else{
    56                         cout<<"In different gangs."<<endl;
    57                     }
    58                 }
    59                 else{
    60                     cout<<"Not sure yet."<<endl;
    61                 }
    62             }
    63             else{
    64                 Union(a,b,fa,fb);//if(fa!=fb)
    65             }
    66         }
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    ABS
    Windows Internals 6th Security
    Windows Internals 6th chap4 services
    Windows Internals 6th chap5 Thread
    Python 腾讯云短信,发送手机验证码
    Python 阿里大于发送手机验证码
    爬虫新手学习2-爬虫进阶(urllib和urllib2 的区别、url转码、爬虫GET提交实例、批量爬取贴吧数据、fidder软件安装、有道翻译POST实例、豆瓣ajax数据获取)
    爬虫新手学习1-爬虫基础
    Python Django CMDB项目实战之-3创建form表单,并在前端页面上展示
    Python Django CMDB项目实战之-2创建APP、建模(models.py)、数据库同步、高级URL、前端页面展示数据库中数据
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4373496.html
Copyright © 2011-2022 走看看