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.
The Story of a Tree
The Story of a Tree
Sort by
recency
|
67 Discussions
|
Please Login in order to post a comment
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
My JavaScript Solution
There are errors in the main function.Took a long time to debug this not realising there was an error in the way the inputs are taken. Replace the line:vector> guesses(q); with vector> guesses(g);. This should fix the error
Hint:
The first thing to do is tranversing the tree to imagine how the tree is. Next, we notice that in the tree we've created above:Guys, write a custom main function. The one given have some issues. as pointed out in earlier answer, the loop to read in guesses has a logical error. Personally this didn't fix my problem much so I simply wrote a new main function and it works.
In c++,
Iam starting to hate this website. This took a lot of my time!!