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.
staticIntegerans;// Complete the evenForest function below.staticintevenForest(intt_nodes,intt_edges,List<Integer>t_from,List<Integer>t_to){ArrayList<ArrayList<Integer>>g=newArrayList<>();for(inti=0;i<=t_nodes;i++){g.add(newArrayList<>());}for(inti=0;i<t_edges;i++){intsrc=t_from.get(i);intdst=t_to.get(i);g.get(dst).add(src);}ans=0;dfs(1,g);returnans;}staticintdfs(intsrc,ArrayList<ArrayList<Integer>>g){intc=1;if(g.get(src).size()==0)returnc;for(intnbr:g.get(src)){c+=dfs(nbr,g);}if(src!=1&&c%2==0){ans++;return0;}returnc;}publicstaticvoidmain(String[]args)throwsIOException{BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbufferedWriter=newBufferedWriter(newFileWriter(System.getenv("OUTPUT_PATH")));String[]tNodesEdges=bufferedReader.readLine().replaceAll("\\s+$","").split(" ");inttNodes=Integer.parseInt(tNodesEdges[0]);inttEdges=Integer.parseInt(tNodesEdges[1]);List<Integer>tFrom=newArrayList<>();List<Integer>tTo=newArrayList<>();IntStream.range(0,tEdges).forEach(i->{try{String[]tFromTo=bufferedReader.readLine().replaceAll("\\s+$","").split(" ");tFrom.add(Integer.parseInt(tFromTo[0]));tTo.add(Integer.parseInt(tFromTo[1]));}catch(IOExceptionex){thrownewRuntimeException(ex);}});intres=evenForest(tNodes,tEdges,tFrom,tTo);bufferedWriter.write(String.valueOf(res));bufferedWriter.newLine();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
Even Tree
You are viewing a single comment's thread. Return to all comments →