[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/joney000/Java-Competitive-Programming/master/Algorithms/DSU.java [Back]  [Original]

/*
 * Author    : joney_000[developer.jaswant@gmail.com]
 * Algorithm : Disjoint Set Union O(log n) + path optimization
 * Platform  : Codeforces/Leetcode. eg. problem: https://leetcode.com/problems/satisfiability-of-equality-equations/
 */
class DSU {
    private int[] parentOf;
    private int[] depth;
    private int size;

    public DSU(int size) {
        this.size = size;
        this.depth = new int[size + 1];
        this.parentOf = new int[size + 1];
        clear(size);
    }
    
    // reset 
    public void clear(int range) {
        this.size = range;
        for (int pos = 1; pos = depth[rootB]) {
            depth[rootA] = Math.max(depth[rootA], 1 + depth[rootB]);
            parentOf[rootB] = rootA;
        } else {
            depth[rootB] = Math.max(depth[rootB], 1 + depth[rootA]);
            parentOf[rootA] = rootB;
        }
    }

    int getNoOfTrees() {
        int uniqueRoots = 0;
        for (int pos = 1; pos 

Web Proxy Viewer  |  New URL  |  Original Page