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.
can anyone help me what is the problem with this code it is paassing all test case but getting TLE for test case 10, how can i improve that code.
unordered_map<int,int>parent,ranker,totalSize;voidsetMap(intnode){if(parent[node]){return;}parent[node]=-1;ranker[node]=0;totalSize[node]=1;}intfindParent(intnode){while(parent[node]!=-1){node=parent[node];}returnnode;}intaddEdge(intparent1,intparent2){if(parent1==parent2){return0;}if(ranker[parent1]<=ranker[parent2]){parent[parent2]=parent1;ranker[parent2]++;totalSize[parent1]=totalSize[parent1]+totalSize[parent2];returntotalSize[parent1];}else{parent[parent1]=parent2;ranker[parent1]++;totalSize[parent2]=totalSize[parent1]+totalSize[parent2];returntotalSize[parent2];}}// Complete the maxCircle function below.vector<int>maxCircle(vector<vector<int>>queries){vector<int>maximum;intmaxVal=0;for(autoquery:queries){intu=query[0];intv=query[1];setMap(u);setMap(v);intparent1=findParent(u);intparent2=findParent(v);maxVal=max(maxVal,addEdge(parent1,parent2));maximum.push_back(maxVal);}returnmaximum;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Friend Circle Queries
You are viewing a single comment's thread. Return to all comments →
can anyone help me what is the problem with this code it is paassing all test case but getting TLE for test case 10, how can i improve that code.