zoukankan      html  css  js  c++  java
  • (中等) POJ 1703 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. 

      题目就是给了一些点不在一个集合内,问某两个点是否在一个集合内。

      带权并查集的经典题,要注意两个集合合并是判断是否是一个,然后就是权相加的时候要注意。

      不过感觉这个题目有些bug,题目说每个集合至少一个,这样的话就应该判断N=2的情况,但是没判断也过了。。。

    代码如下:

    // ━━━━━━神兽出没━━━━━━
    //      ┏┓       ┏┓
    //     ┏┛┻━━━━━━━┛┻┓
    //     ┃           ┃
    //     ┃     ━     ┃
    //     ████━████   ┃
    //     ┃           ┃
    //     ┃    ┻      ┃
    //     ┃           ┃
    //     ┗━┓       ┏━┛
    //       ┃       ┃
    //       ┃       ┃
    //       ┃       ┗━━━┓
    //       ┃           ┣┓
    //       ┃           ┏┛
    //       ┗┓┓┏━━━━━┳┓┏┛
    //        ┃┫┫     ┃┫┫
    //        ┗┻┛     ┗┻┛
    //
    // ━━━━━━感觉萌萌哒━━━━━━
    
    // Author        : WhyWhy
    // Created Time  : 2015年07月17日 星期五 20时24分40秒
    // File Name     : 1703_1.cpp
    
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    const int MaxN=100005;
    
    int N;
    int fa[MaxN],val[MaxN];
    
    int find(int x,int &num)
    {
        if(fa[x]==-1)
            return x;
    
        int tn=0;
        int t=find(fa[x],tn);
    
        fa[x]=t;
        num=tn+val[x];
        val[x]=num;
    
        return fa[x];
    }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        
        int T,Q;
        int a,b;
        int ba,bb;
        int x,y;
        char s[10];
    
        scanf("%d",&T);
    
        while(T--)
        {
            scanf("%d %d",&N,&Q);
    
            memset(fa,-1,sizeof(fa));
            
            while(Q--)
            {
                scanf("%s %d %d",s,&a,&b);
    
                if(s[0]=='D')
                {
                    x=y=0;
                    ba=find(a,x);
                    bb=find(b,y);
        
                    if(ba!=bb)
                    {
                        fa[ba]=bb;
                        val[ba]=x+y+1;
                    }
                }
                else
                {
                    x=y=0;
                    ba=find(a,x);
                    bb=find(b,y);
    
                    if(ba!=bb)
                        puts("Not sure yet.");
                    else if((y-x)%2)
                        puts("In different gangs.");
                    else
                        puts("In the same gang.");
                }
            }
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    [Linux] XShell 远程 Ubuntu 云主机,图形化界面打开Chrome
    [UI] 工具 & 框架
    你不知道的<input type="file">的小秘密
    vue3逻辑分离和页面快速展示数据
    vue中props参数的使用
    vue3.0中reactive的正确使用姿势
    CF986B Petr and Permutations(逆序对)
    洛谷P1972 [SDOI2009]HH的项链(莫队)44分做法
    2021牛客暑期多校训练营5 B. Boxes(概率期望)
    2021牛客暑期多校训练营5 K. King of Range(单调队列)详细题解
  • 原文地址:https://www.cnblogs.com/whywhy/p/4655638.html
Copyright © 2011-2022 走看看