zoukankan      html  css  js  c++  java
  • hihocoder 1638:多级并查集

    题目链接
    并查集可以用于聚类。

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    class Main {
    int N = (int) (1e4 + 7);
    int father[] = new int[N << 1];
    
    class FatherDis {
        int father;
        int dis;
    
        FatherDis(int father, int dis) {
            this.father = father;
            this.dis = dis;
        }
    
        @Override
        public String toString() {
            return String.format("(father=%d,dis=%d)", father, dis);
        }
    }
    
    FatherDis find(int x) {
        if (father[x] == x) {
            return new FatherDis(x, 0);
        }
        FatherDis f = find(father[x]);
        f.dis++;
        if ((f.dis & 1) == 0) {
            father[x] = f.father + N;
        } else {
            father[x] = f.father;
        }
        return f;
    }
    
    Main() {
        Scanner cin = new Scanner(System.in); 
        int t = cin.nextInt();
        while (t-- > 0) {
            int n = cin.nextInt(), m = cin.nextInt();
            for (int i = 1; i <= n; i++) father[i] = father[i + N] = i;
            int fail = -1;
            for (int i = 0; i < m; i++) {
                int x = cin.nextInt(), u = cin.nextInt(), v = cin.nextInt();
                if (fail != -1) continue;//读完数据再说
                FatherDis fu = find(u), fv = find(v);
                boolean sameLevel = (fu.dis & 1) == (fv.dis & 1);
                if (x == 0) {
                    if (fu.father != fv.father) {
                        if (sameLevel) {
                            father[fu.father] = fv.father + N;
                        } else {
                            father[fu.father] = fv.father;
                        }
                    } else {
                        if (!sameLevel) {//不同类别
                            fail = i;
                        }
                    }
                } else {
                    if (fu.father != fv.father) {
                        if (sameLevel) {
                            father[fu.father] = fv.father;
                        } else {
                            father[fu.father] = fv.father + N;
                        }
                    } else {
                        if (sameLevel) {//同一类别
                            fail = i;
                        }
                    }
                }
            }
            if (fail == -1) {
                System.out.println("great");
            } else {
                System.out.println("sad");
                System.out.println(fail + 1);
            }
        }
    }
    
    public static void main(String[] args) {
        new Main();
    }
    }
    
  • 相关阅读:
    HDU 1232 畅通工程(并查集分析)
    NYOJ 2 括号配对问题
    HDU 1205 吃糖果
    HDU 1201 18岁生日
    [ACM] hdu Find a way
    [ACM] hdu Ignatius and the Princess I

    pongo(英雄会)编程挑战: 人人code,整数取反
    [ACM] POJ 1852 Ants
    波司登杯2013微软office应用创意大赛烟台大学校园赛参赛历程
  • 原文地址:https://www.cnblogs.com/weiyinfu/p/9098299.html
Copyright © 2011-2022 走看看