zoukankan      html  css  js  c++  java
  • POJ1703Find them, Catch them[种类并查集]

    Find them, Catch them
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 42416   Accepted: 13045

    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


    题意:给出不在同一集合关系
    罪犯只可能属于两个团伙中的一个,现在给出M个条件(D a b表示a和b不在同一团伙),
    对于每一个询问(A a b)确定a,b是不是属于同一团伙或者不能确定。

     
    种类并查集类似于带权并查集v[i]=0表示i与父相同,v[i]=1表示不同
    路径压缩和合并时推导一下
    &1相当于%2
    //
    //  main.cpp
    //  poj1703
    //
    //  Created by Candy on 30/10/2016.
    //  Copyright © 2016 Candy. All rights reserved.
    //
    
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    using namespace std;
    const int N=1e5+5;
    typedef long long ll;
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    int T,n,m,a,b;
    char c[5];
    int fa[N],v[N];
    inline int find(int x){
        if(x==fa[x]) return x;
        int root=find(fa[x]);
        v[x]=(v[x]+v[fa[x]])&1;
        return fa[x]=root;
    }
    inline void unn(int x,int y){
        int f1=find(x),f2=find(y);
        if(f1!=f2){
            fa[f1]=f2;
            v[f1]=(v[x]+v[y]+1)&1;
        }
    }
    int main(int argc, const char * argv[]) {
        T=read();
        while(T--){
            n=read();m=read();
            for(int i=1;i<=n;i++) fa[i]=i,v[i]=0;
            for(int i=1;i<=m;i++){
                scanf("%s",c);a=read();b=read();
                if(c[0]=='D') unn(a,b);
                else{
                    int f1=find(a),f2=find(b);
                    if(f1!=f2) puts("Not sure yet.");
                    else if(v[a]==v[b]) puts("In the same gang.");
                    else puts("In different gangs.");
                }
            }
        }
        
        return 0;
    }
  • 相关阅读:
    无法添加数据库未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc, Version=10.0.0.0, Culture=neutral,PublicKeyToken=89845dcd8080c
    转载:自己制作Visual Studio项目模板(以原有项目为模版) VS—项目模板丢失的解决方案
    设计一个高效的缓存管理服务 C#
    Visual Studio 30个快捷键2009年05月22日
    Everything 中文绿色版
    Visual studio 打包
    远程桌面连接超出最大连接数的3种解决办法
    [Cache 学习] Cache.Insert 与 Cache.Add 区别
    三层架构之我见 —— 不同于您见过的三层架构。
    基于IIS发布你的WCF Service。
  • 原文地址:https://www.cnblogs.com/candy99/p/6015491.html
Copyright © 2011-2022 走看看