• (中等) 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
  • 相关阅读:
    [NHibernate]条件查询Criteria Query
    [JQuery]用InsertAfter实现图片走马灯展示效果
    [NHibernate]HQL查询
    [NHibernate]基本配置与测试
    [HTML/CSS]margin属性用法
    [HTML/CSS]盒子模型,块级元素和行内元素
    [Asp.net MVC]Asp.net MVC5系列——布局视图
    [c#基础]值类型和引用类型的Equals,==的区别
    用中间件实现读负载均衡的数据库群集
    论数据库连接池对中间件性能的重要性
  • 原文地址:https://www.cnblogs.com/whywhy/p/4655638.html
Copyright © 2020-2023  润新知