library

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

View the Project on GitHub kisepichu/library

:heavy_check_mark: Union Find
(lib/data-structure/union-find.hpp)

Verified with

Code

#pragma once
class UnionFind{
public:
	vector<int>par,rank;
	UnionFind(int n){
		par.resize(n);rank.resize(n,0);
		for(int i=0;i<n;++i)par[i]=i;
	}
	int root(int x){
		return x==par[x]?x:par[x]=root(par[x]);
	}
	bool same(int x,int y){
		return root(x)==root(y);
	}
	void unite(int x,int y){
		x=root(x);
		y=root(y);
		if(x==y)return;
		if(rank[x]<rank[y])par[x]=y;
		else{
			par[y]=x;
			if(rank[x]==rank[y])++rank[x];
		}
	}
};

/**
* @title Union Find
*/
#line 2 "lib/data-structure/union-find.hpp"
class UnionFind{
public:
	vector<int>par,rank;
	UnionFind(int n){
		par.resize(n);rank.resize(n,0);
		for(int i=0;i<n;++i)par[i]=i;
	}
	int root(int x){
		return x==par[x]?x:par[x]=root(par[x]);
	}
	bool same(int x,int y){
		return root(x)==root(y);
	}
	void unite(int x,int y){
		x=root(x);
		y=root(y);
		if(x==y)return;
		if(rank[x]<rank[y])par[x]=y;
		else{
			par[y]=x;
			if(rank[x]==rank[y])++rank[x];
		}
	}
};

/**
* @title Union Find
*/
Back to top page