Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**------------------------
- Author : NikolaP
- Compiler : C++
- -------------------------*/
- //{ Setup
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long int ll;
- typedef long double ld;
- typedef vector<int> vi;
- const int inf = INT_MAX;
- const bool debug = 0;
- //}
- class UFDS /// Union find disjoint sets - OOP
- {
- private:
- vi parent, rank;
- public:
- UFDS(){ /// Default constructor
- parent.assign(1,0);
- rank.assign(1,0);
- };
- ~UFDS(); /// Deconstructor
- UFDS(int n){ /// Constructor
- parent.assign(n,0);
- rank.assign(n,0);
- for (int i = 0; i < n; ++i)
- parent[i] = i;
- }
- /// Find the root of a set + Path compression
- int FindSet(int i) { return (parent[i] == i) ? i : ( parent[i] = FindSet(parent[i]) ); }
- /// Check if two elements are in the same set
- bool IsSameSet(int i, int j) { return FindSet(i) == FindSet(j); }
- /// Unite two sets - Union by rank
- void Union(int i, int j){
- if(!IsSameSet(i,j)){
- int x = FindSet(i), y = FindSet(j);
- if(rank[x] > rank[y]) parent[y] = x;
- else{
- parent[x] = y;
- if(rank[x] == rank[y]) ++rank[y];
- }
- }
- }
- };
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(0);
- cout.precision(8);
- //cout << fixed;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment