Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //DSU
- struct dsu
- {
- vector<int> parent;
- vector<int> sz;
- dsu(int n)
- {
- parent=vector<int>(n);
- for(int i=0;i<n;i++)parent[i]=i;
- sz=vector<int>(n,1);
- }
- int find_(int x)
- {
- int root=x;
- while(root!=parent[root])
- root=parent[root];
- //Path compression
- while(parent[x]!=root)
- {
- int p=parent[x];
- parent[x]=root;
- x=p;
- }
- return root;
- }
- bool union_(int x,int y)
- {
- int X=find_(x);
- int Y=find_(y);
- // x and y are already in the same set
- if(X==Y)
- return false;
- // x and y are not in same set, so we merge them
- if(sz[X]<sz[Y])
- swap(X, Y);
- // merge yRoot into xRoot
- parent[Y]=X;
- sz[X]+=sz[Y];
- return true;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement