We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I am getting wrong answer on test case 1,3,5.
Below is my code, I used path compression too
#include<cmath>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>#include<stdlib.h>usingnamespacestd;structnode{intdata;structnode*next=NULL;ints=1;};structnode*fin(structnode*root){structnode*temp=root;while(temp->next!=NULL){temp=temp->next;}structnode*x;if(temp!=root){x=root;while(root->next->next!=NULL){root=root->next;x->next=temp;x=root;}returntemp;}elsereturnroot;*/}intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */intn;cin>>n;vector<structnode*>v(n);for(inti=0;i<n;i++){v[i]=newnode;v[i]->data=i;}intq;cin>>q;while(q--){chart;cin>>t;if(t=='M'){inti,j;cin>>i>>j;i--;j--;structnode*x,*y;x=fin(v[i]);y=fin(v[j]);if(x!=y){if(x->s>=y->s){v[j]->next=x;x->s++;}else{v[i]->next=y;y->s++;}}}else{intg;cin>>g;g--;structnode*r=fin(v[g]);cout<<r->s<<endl;}}return0;}
Cookie support is required to access HackerRank
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 →
I am getting wrong answer on test case 1,3,5. Below is my code, I used path compression too