
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

import java.util.Scanner;
class QuickUnionUF { int id[];
QuickUnionUF(int n) { id=new int[n]; for(int i=0;i<n;i++) id[i]=i; }
private int root (int i) { while(i!=id[i]) i=id[i]; return i; }
public boolean connected(int a, int b) { return root(a)==root(b); }
public void union(int a,int b) { id[root(a)]=root(b); // Note: The roots have the same id as their index number
}