Given a tree with N nodes. Two players A and B start from node 1 and node N respectively. A can visit all the adjacent nodes to the nodes already visited by A but can not visit any nodes which is already visited by B and similarly for B also.
The player who visits more cities win. Find the player who wins if they both play optimally.
Input: Output: A wins
Approach: The optimal solution is that both the Player starts visiting the nodes which lie in the path connecting node 1 and node N. This is because one player cannot visit the nodes already visited by another player so each player will try to limit the number of nodes that can be visited by another player. Then it will be easy to count the number of nodes that can be visited by each player and find out the winner.
The DFS will be use to find out the path between two nodes and mark them one by one like 1 or 2, 1 for A and 2 for B and then mark all the adjacent unvisited nodes with the respective value and then calculate the count of nodes of A and B.
Below is the implementation of the above approach:
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Print levels with odd number of nodes and even number of nodes
- Check if a Binary Tree contains duplicate subtrees of size 2 or more
- Maximize number of nodes which are not part of any edge in a Graph
- Maximum number of nodes which can be reached from each node in a graph.
- Count nodes within K-distance from all nodes in a set
- Sum of all odd nodes in the path connecting two given nodes
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Count of all prime weight nodes between given nodes in the given Tree
- Maximize count of nodes disconnected from all other nodes in a Graph
- Remove all nodes which don't lie in any path with sum>= k
- Count of nodes which are at a distance X from root and leaves
- Count of Nodes which has Prime Digit sum weight in a Tree
- Count pairs of leaf nodes in a Binary Tree which are at most K distance apart
- Find if there is a path of more than k length from a source
- Largest subset of Graph vertices with edges of 2 or more colors
- Remove minimum elements from either side such that 2*min becomes more than max | Set 2
- Check sum of Covered and Uncovered nodes of Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.