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.
There is another working solution a little different from the suggested one. The main idea is that if we choose an arbitrary root node and calculate how many parent nodes were guessed correctly, then we can choose new root as the neighbour node of the current root and the only parent-child relation which can change is between these two nodes (old and new root). Algorithm contains several steps:
1. build a graph from the input data and assign guessed parents to the nodes
3. choose an arbitrary node as a root and perform DFS, chocking how many parents were guessed correctly
4. using the same root, perform another DFS. While processing a node we check how the number of guessed parents changes if the root of the tree was one of its child nodes
classResult{/* * Complete the 'storyOfATree' function below. * * The function is expected to return a STRING. * The function accepts following parameters: * 1. INTEGER n * 2. 2D_INTEGER_ARRAY edges * 3. INTEGER k * 4. 2D_INTEGER_ARRAY guesses */publicstaticclassGraph{publicList<Node>nodes=newArrayList<>();publicList<List<Node>>adj=newArrayList<>();}publicstaticclassNode{publicintid;publicNodeparent;publicSet<Node>guessedParents=newHashSet<>();publicbooleanvisited;}publicstaticStringstoryOfATree(intn,List<List<Integer>>edges,intk,List<List<Integer>>guesses){intalicesWins=0;Graphgraph=createGraph(n,edges,k,guesses);Noderoot=graph.nodes.get(1);intcorrectParents=dfsFillParents(graph,root);if(correctParents>=k)alicesWins++;clearVisited(graph);alicesWins+=dfsCheckWins(graph,root,k,correctParents);intgcd=gcd(n,alicesWins);returnalicesWins/gcd+"/"+n/gcd;}publicstaticintdfsFillParents(Graphgraph,Nodenode){intcorrectParents=0;node.visited=true;for(Nodeadj:graph.adj.get(node.id)){if(!adj.visited){adj.parent=node;if(adj.guessedParents.contains(adj.parent)){correctParents++;}correctParents+=dfsFillParents(graph,adj);}}returncorrectParents;}publicstaticintdfsCheckWins(Graphgraph,Nodenode,intk,intcorrectParents){intwins=0;node.visited=true;for(Nodeadj:graph.adj.get(node.id)){if(!adj.visited){intcorrection=0;if(adj.guessedParents.contains(adj.parent)){correction-=1;}if(node.guessedParents.contains(adj)){correction+=1;}correctParents+=correction;if(correctParents>=k){wins++;}wins+=dfsCheckWins(graph,adj,k,correctParents);correctParents-=correction;}}returnwins;}publicstaticGraphcreateGraph(intn,List<List<Integer>>edges,intk,List<List<Integer>>guesses){Graphgraph=newGraph();// nodesfor(inti=0;i<=n;++i){Nodenode=newNode();node.id=i;graph.nodes.add(node);graph.adj.add(newArrayList<>());}// edgesfor(List<Integer>edge:edges){intn1=edge.get(0);intn2=edge.get(1);graph.adj.get(n1).add(graph.nodes.get(n2));graph.adj.get(n2).add(graph.nodes.get(n1));}// guessesfor(List<Integer>guess:guesses){intu=guess.get(0);intv=guess.get(1);graph.nodes.get(v).guessedParents.add(graph.nodes.get(u));}returngraph;}publicstaticvoidclearVisited(Graphgraph){graph.nodes.forEach(n->n.visited=false);}publicstaticintgcd(intmajor,intminor){if(minor==0)returnmajor;returngcd(minor,major%minor);}publicstaticvoidprint(Strings){System.out.println(s);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Story of a Tree
You are viewing a single comment's thread. Return to all comments →
There is another working solution a little different from the suggested one. The main idea is that if we choose an arbitrary root node and calculate how many parent nodes were guessed correctly, then we can choose new root as the neighbour node of the current root and the only parent-child relation which can change is between these two nodes (old and new root). Algorithm contains several steps: 1. build a graph from the input data and assign guessed parents to the nodes 3. choose an arbitrary node as a root and perform DFS, chocking how many parents were guessed correctly 4. using the same root, perform another DFS. While processing a node we check how the number of guessed parents changes if the root of the tree was one of its child nodes