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.
#include<cmath>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>usingnamespacestd;intfindset(intx,vector<int>&r){if(r[x]==x)return(x);elsereturn(findset(r[x],r));}intmain(){intn;cin>>n;// initvector<int>root(2*n+1);vector<int>count(2*n+1);for(inti=1;i<=2*n;i++){root[i]=i;count[i]=1;}for(inti=0;i<n;i++){intg,b;cin>>g>>b;// find root of g & bintrg=findset(g,root);intrb=findset(b,root);if(rg==rb)continue;// union without rankingroot[rb]=rg;count[rg]+=count[rb];count[rb]=0;}// find min and maxintmin=2*n+1;intmax=0;for(inti=1;i<=2*n;i++){if(count[i]>1&&count[i]<min)min=count[i];if(count[i]>max)max=count[i];}cout<<min<<" "<<max;return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Components in a graph
You are viewing a single comment's thread. Return to all comments →
C++ solution, passed all tests: