U
class U{ int n; int cal; int[] ancinents; int[] bros; public U(U u){ this.n=u.n; this.cal=u.cal; this.ancinents=new int[n]; this.bros=new int[n]; for(int i=0;i<n;i++){ this.ancinents[i]=u.ancinents[i]; this.bros[i]=u.bros[i]; } } public U(int n){ this.n=n; this.cal=n; this.ancinents=new int[n]; this.bros=new int[n]; for(int i=0;i<n;i++){ ancinents[i]=i; } Arrays.fill(bros,1); } public int find(int i){ while(i!=ancinents[i]){ ancinents[i]=ancinents[ancinents[i]]; i=ancinents[i]; //i=ancinents[ancinents[i]]; } return i; } public void union(int i,int j){ int a=find(i); int b=find(j); if(a!=b){ bros[a]+=bros[b]; ancinents[b]=a; cal--; } } }