It seems my code is correct - Click Here
But I am getting WA in it, I have also checked the corner case (didn’t consider 1 as the special city).
I ran a BFS and calculated the total energy to reach every node from node 1 and stored it, then took all leaf nodes energies and greedily checked with energies available.
Can anyone point out the mistake. It’ll be really helpful.
Can this problem be solved using Dijkstra algorithm? I mean at first I have to calculated the minimum cost from the capital city to all cities using Dijlsktra… And then find the number of special city then the answer using multiset…
In theory yes, but we use a special characteristic of trees. We can simply do the dfs traversal of a graph because there’s only a single path between every two vertices.
As for the multiset, every leaf occurs only once so you can get away with a simple set or just an array sorted in increasing order and greedily match pilgrims with lowest energy to leaves of smallest distance.
I had a doubt. Feel free and please do reply. This is my soln. I got WA and I didn’t have any Run time Errors.
I used dfs in the question and didn’t use bool visited array. Is it one of the correct way to solve? (Note while inserting I inserted unidirectional directional link betn cities).
If possible can someone tell me what was wrong with my code ?
You should add bidirectional edges in your graph, since input can be given as:
1 3
2 3
In your case you won’t consider 3 - 2 as an edge, meaning you’ll consider 3 as a leaf rather than 2.
This introduces a new issue in your code which is dfs without visited array. You still don’t need visited array, but need a way to tell whether the vertex u is parent of v. This can be solved by introducing parent variable in the dfs defintion.
Also in your later while loop you’ll need a way to break out of the loop once j reaches its maximum value, not only i, because there could be fewer leaves than pilgrims.
Hey anyone here please check my code which getting WA but i think my approach is right and also i considered all the corner cases CodeChef: Practical coding for everyone
hey aadiupadhyay please check my code which getting WA but i think my approach is right and also i considered all the corner cases Solution: 45071706 | CodeChef
Woohooo !!! Thanks a lot man this clear lots of my doubts… Actually I used this method of unidirectional and no visited array CSES trees section few problems. But after seeing Karik baiyas soln on youtube, I came to know about this parent thing. But I was unable to tell what was wrong with my method.
Hey! can you please explain just one thing… how did you realize that there exists only a single path between two vertices? is it because there are N-1 edges?
There must exist at least one path between every pair of vertices (u, v) according to the statement of the problem.
Now suppose there exists more than one path between vertices u and v. This means there are k paths between them, such that k \geq 2.
Since k \geq 2, there are 2 different paths we can shoose p and q. If this were true there would exist a cycle (formed by unique vertices of p and q), which by the definition of tree is impossible. So we derive a contradiction, this by combining the first and second statement we conclude there must be one and only one path between two vertices of a tree.
I don’t get what I’m doing wrong can someone help ? CodeChef: Practical coding for everyone Here’s the solution.
Also, I’m quite new to graph problems any good resources that you guys would recommend?
Your approach is correct, but TLE is coming because you are storing the tree in a 2D matrix, which is taking much space(10^5 * 10^5) and time(10^5 * 10^5).In the DFS function, you are iterating all other nodes for each node (Line:7), so time is (10^5 * 10^5).For more information, you can visit this website, where they have discussed the Time complexity of DFS in detail - Click Here
If you store the tree in an adjacency list, then your time complexity will reduce to O(V+E) = O(10^5 + (10^5-1)) which is O(10^5 + 10^5)
You can visit the above-mentioned line, it will give more clarity.
Thanks alot, i understood its better to make adjacency list. I used to avoid it as a common query could be, whether there’s an edge from A to B ?, So we could answer it in O(1) in matrix, instead of list.