You are viewing a single comment's thread. Return to all comments →
Your algorithm was pretty much correct,
But you need to modify the union function
a bit and in main function , you need to print the size of the
root of that node because ,You are updating only the size of the
root in the union _ function
The modification is
New Union Function is :::
void union1(int a[],int size[],int r,int s) { int root_r=root(a,r); int root_s=root(a,s); if(root_r!=root_s) { if(size[root_r] < size[root_s]){ a[root_r]=a[root_s]; size[root_s]+=size[root_r]; } else { a[root_s]=a[root_r]; size[root_r]+=size[root_s]; } } } In Main Function Query Answer ::: cout<<size[root(a,p)]<<endl;
Seems like cookies are disabled on this browser, please enable them to open this website
Merging Communities
You are viewing a single comment's thread. Return to all comments →
Your algorithm was pretty much correct,
But you need to modify the union function
a bit and in main function , you need to print the size of the
root of that node because ,You are updating only the size of the
root in the union _ function
The modification is
New Union Function is :::