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 5 testcases wrong. Is it only because of the precision or something else?
importjava.io.*;importjava.util.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;publicclassgraph{intn,e;HashMap<Integer,ArrayList<Integer>>list=newHashMap();booleanv[];staticintdistance[];//Constructor takes inputgraph(){n=i();//no of nodes e=n-1;//no of egdesfor(inti=1;i<=n;i++)list.put(i,newArrayList());for(inti=0;i<e;i++)//creating adjacency list{inta=i();intb=i();list.get(a).add(b);list.get(b).add(a);}v=newboolean[n+1];cl=newint[n+1];distance=newint[n+1];distance[0]=0;}publicintdfs(introot){v[root]=true;ArrayList<Integer>ar=list.get(root);intc=1;for(inti=0;i<ar.size();i++){intnode=ar.get(i);if(!v[node])// if element is not visited{distance[node]=distance[root]+1;c+=dfs(node);}}cl[root]=c;returnc;}staticintcl[];publicstaticvoidmain(String[]args){PrintWriterout=newPrintWriter(System.out);graphg=newgraph();introot=1;g.dfs(root);doubledd=0.0d;doubletotaldist=0.0d;for(inti=2;i<=g.n;i++){//System.out.println(i+" "+g.cl[i]+" "+distance[i]);dd+=(g.n-g.cl[i])*distance[i];totaldist+=distance[i];}// out.printf("%.14f\n", dd/totaldist);System.out.print(dd/totaldist);// System.out.print(dd/(g.n-1));}staticScannersc=newScanner(System.in);staticlongl(){returnsc.nextLong();}staticinti(){returnsc.nextInt();}staticStrings(){returnsc.nextLine();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Wire Removal
You are viewing a single comment's thread. Return to all comments →
Can anyone help me with this ?
I am getting 5 testcases wrong. Is it only because of the precision or something else?