competitive-programming-java-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub shun0923/competitive-programming-java-library

:heavy_check_mark: library/UnionFind.java

Depends on

Required by

Verified with

Code

package library;

import java.util.*;
import library.FastIO;
import library.AbstractGraph;

class UnionFind { // N=numNode
	public final int n;
	private final int nodes[]; // non neg: parent idx, neg: - size
	public int numGroups;

	public UnionFind(final int n) { // O(N)
		FastIO.nonNegativeCheck(n);
		this.n = n;
		nodes = new int[n];
		numGroups = n;
		Arrays.fill(nodes, -1);
	}

	public final void uniteAll(final Collection<? extends AbstractEdge> edges) { for(AbstractEdge e : edges) unite(e); } // O(a(N)M)
	public final void uniteAll(final AbstractEdge[] edges) { for(AbstractEdge e : edges) unite(e); } // O(a(N)M)
	public final boolean unite(final AbstractEdge e) { return unite(e.source, e.target); }
	public final boolean unite(final int x, final int y) { // O(a(N))
		FastIO.rangeCheck(x, n);
		FastIO.rangeCheck(y, n);
		int xRoot = root(x);
		int yRoot = root(y);
		if(xRoot == yRoot) return true;
		numGroups --;
		if(nodes[yRoot] < nodes[xRoot]) {
			nodes[yRoot] += nodes[xRoot];
			nodes[xRoot] = yRoot;
		}else {
			nodes[xRoot] += nodes[yRoot];
			nodes[yRoot] = xRoot;
		}
		return false;
	}

	public final int size(final int i) { FastIO.rangeCheck(i, n); return - nodes[root(i)]; } // O(a(N))

	public final int root(final int i) { // O(a(N))
		FastIO.rangeCheck(i, n);
		if(nodes[i] < 0) return i;
		return nodes[i] = root(nodes[i]);
	}

	public final boolean same(final int x, final int y) { FastIO.rangeCheck(x, n); FastIO.rangeCheck(y, n); return root(x) == root(y); } // O(a(N))

	public final HashMap<Integer, HashSet<Integer>> groups() { // O(N)
		HashMap<Integer, HashSet<Integer>> groups = new HashMap<>();
		for(int i = 0; i < n; i ++) {
			int root = root(i);
			groups.putIfAbsent(root, new HashSet<>());
			groups.get(root).add(i);
		}
		return groups;
	}
}
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/onlinejudge_verify/documentation/build.py", line 71, in _render_source_code_stat
    bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/onlinejudge_verify/languages/user_defined.py", line 71, in bundle
    return subprocess.check_output(shlex.split(command))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/subprocess.py", line 466, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['false']' returned non-zero exit status 1.
Back to top page