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.
importjava.math.*;importjava.security.*;importjava.text.*;importjava.util.*;importjava.util.Comparator;importjava.util.concurrent.*;importjava.util.function.*;importjava.util.regex.*;importjava.util.stream.*;importstaticjava.util.stream.Collectors.joining;importstaticjava.util.stream.Collectors.toList;classResult{/* * Complete the 'kruskals' function below. * * The function is expected to return an INTEGER. * The function accepts WEIGHTED_INTEGER_GRAPH g as parameter. *//* * For the weighted graph, <name>: * * 1. The number of nodes is <name>Nodes. * 2. The number of edges is <name>Edges. * 3. An edge exists between <name>From[i] and <name>To[i]. The weight of the edge is <name>Weight[i]. * */publicstaticintkruskals(intgNodes,List<Integer>gFrom,List<Integer>gTo,List<Integer>gWeight){ArrayList<Tree>l=newArrayList<>();for(inti=0;i<gFrom.size();i++){l.add(newTree(gFrom.get(i),gTo.get(i),gWeight.get(i)));}Collections.sort(l);intr=doKruskal(l,gNodes,gFrom.size());returnr;}publicstaticintdoKruskal(ArrayList<Tree>a,intv,inte){Subset[]s=newSubset[v+1];intresult=0;for(inti=1;i<v+1;i++){s[i]=newSubset(i,0);}for(intj=0;j<e;j++){intx=findParent(s,a.get(j).s);inty=findParent(s,a.get(j).d);if(x!=y){doUnion(s,x,y);result+=a.get(j).w;}}returnresult;}publicstaticvoiddoUnion(Subset[]p,intx,inty){inta=findParent(p,x);intb=findParent(p,y);if(p[a].r<=p[b].r){p[a].p=b;}else{p[b].p=a;}}publicstaticintfindParent(Subset[]p,inti){if(p[i].p==i){returnp[i].p;}returnfindParent(p,p[i].p);}}classSubset{intp;intr;Subset(intx,inty){p=x;r=y;}}classTreeimplementsComparable<Tree>{ints;intd;intw;Tree(intx,inty,intz){s=x;d=y;w=z;}@OverridepublicintcompareTo(Treet){if(this.w>t.w){return1;}elseif(this.w<t.w){return-1;}else{if((this.s+this.d+this.w)<(t.s+t.d+t.w)){return-1;}else{return0;}}}@OverridepublicStringtoString(){returnthis.s+" "+this.d+" "+this.w;}}publicclassSolution{publicstaticvoidmain(String[]args)throwsIOException{BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbufferedWriter=newBufferedWriter(newFileWriter(System.getenv("OUTPUT_PATH")));String[]gNodesEdges=bufferedReader.readLine().replaceAll("\\s+$","").split(" ");intgNodes=Integer.parseInt(gNodesEdges[0]);intgEdges=Integer.parseInt(gNodesEdges[1]);List<Integer>gFrom=newArrayList<>();List<Integer>gTo=newArrayList<>();List<Integer>gWeight=newArrayList<>();IntStream.range(0,gEdges).forEach(i->{try{String[]gFromToWeight=bufferedReader.readLine().replaceAll("\\s+$","").split(" ");gFrom.add(Integer.parseInt(gFromToWeight[0]));gTo.add(Integer.parseInt(gFromToWeight[1]));gWeight.add(Integer.parseInt(gFromToWeight[2]));}catch(IOExceptionex){thrownewRuntimeException(ex);}});intres=Result.kruskals(gNodes,gFrom,gTo,gWeight);// Write your code here.bufferedWriter.write(String.valueOf(res));bufferedReader.close();bufferedWriter.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Kruskal (MST): Really Special Subtree
You are viewing a single comment's thread. Return to all comments →
JAVA solution-->