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 figure out what is wrong with my code? It fails at 7/11 cases. Thanks
#include<bits/stdc++.h>usingnamespacestd;vector<string>split_string(string);intparents[100001],counts[100001];structWNode{intpoint1,point2;longlongweight;};structWeightPaths{longlongweight,paths;};structcomp{booloperator()(WNodea,WNodeb){returna.weight<=b.weight;}};intfind(intx,intparents[]){while(x!=parents[x]){x=parents[parents[x]];}returnx;}longlongsearch(vector<WeightPaths>&weightpath,intn){intleft=0,right=weightpath.size()-1;longlongresult=0;while(true){if(right-left<=1)break;intmid=left+(right-left)/2;if(weightpath[mid].weight>n)right=mid;elseleft=mid;}if(weightpath[right].weight<=n)result=weightpath[right].paths;elseresult=weightpath[left].paths;returnresult;}// Complete the solve function below.vector<int>solve(intn,vector<vector<int>>tree,vector<vector<int>>queries){for(inti=1;i<=n;i++){parents[i]=i;counts[i]=1;}vector<WNode>wnode(n-1);for(inti=0;i<tree.size();i++){wnode[i].point1=tree[i][0];wnode[i].point2=tree[i][1];wnode[i].weight=tree[i][2];}sort(wnode.begin(),wnode.end(),comp());vector<WeightPaths>weightpath;WeightPathswp;wp.weight=0;wp.paths=0;weightpath.push_back(wp);intk=0;while(k<n-1){wp.weight=wnode[k].weight;while(k<n-1&&wnode[k].weight==wp.weight){intp1=wnode[k].point1;intp2=wnode[k].point2;intp=find(p1,parents);intq=find(p2,parents);if(counts[p]<counts[q]){wp.paths+=(longlong)counts[p]*counts[q];parents[p]=q;counts[q]+=counts[p];counts[p]=0;}else{wp.paths+=(longlong)counts[p]*counts[q];parents[q]=p;counts[p]+=counts[q];counts[q]=0;}k++;}weightpath.push_back(wp);}vector<int>results;for(inti=0;i<queries.size();i++){intl=queries[i][0],r=queries[i][1];longlongln,rn,p;ln=search(weightpath,l-1);rn=search(weightpath,r);p=rn-ln;results.push_back(p);}returnresults;}intmain(){ofstreamfout(getenv("OUTPUT_PATH"));stringnq_temp;getline(cin,nq_temp);vector<string>nq=split_string(nq_temp);intn=stoi(nq[0]);intq=stoi(nq[1]);vector<vector<int>>tree(n-1);for(inttree_row_itr=0;tree_row_itr<n-1;tree_row_itr++){tree[tree_row_itr].resize(3);for(inttree_column_itr=0;tree_column_itr<3;tree_column_itr++){cin>>tree[tree_row_itr][tree_column_itr];}cin.ignore(numeric_limits<streamsize>::max(),'\n');}vector<vector<int>>queries(q);for(intqueries_row_itr=0;queries_row_itr<q;queries_row_itr++){queries[queries_row_itr].resize(2);for(intqueries_column_itr=0;queries_column_itr<2;queries_column_itr++){cin>>queries[queries_row_itr][queries_column_itr];}cin.ignore(numeric_limits<streamsize>::max(),'\n');}vector<int>result=solve(n,tree,queries);for(intresult_itr=0;result_itr<result.size();result_itr++){fout<<result[result_itr];if(result_itr!=result.size()-1){fout<<"\n";}}fout<<"\n";fout.close();return0;}vector<string>split_string(stringinput_string){string::iteratornew_end=unique(input_string.begin(),input_string.end(),[](constchar&x,constchar&y){returnx==yandx==' ';});input_string.erase(new_end,input_string.end());while(input_string[input_string.length()-1]==' '){input_string.pop_back();}vector<string>splits;chardelimiter=' ';size_ti=0;size_tpos=input_string.find(delimiter);while(pos!=string::npos){splits.push_back(input_string.substr(i,pos-i));i=pos+1;pos=input_string.find(delimiter,i);}splits.push_back(input_string.substr(i,min(pos,input_string.length())-i+1));returnsplits;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Super Maximum Cost Queries
You are viewing a single comment's thread. Return to all comments →
Can anyone help me figure out what is wrong with my code? It fails at 7/11 cases. Thanks