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.

**Examples:**

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:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Vector to store Tree ` `vector<vector<` `int` `> > graph; ` ` ` `// To check if there ` `// is path or not ` `int` `found = 0, n; ` ` ` `// Path and temporary vector ` `vector<` `int` `> path, temp; ` ` ` `// Count of A and B ` `int` `c_A = 0, c_B = 0; ` ` ` `// Function to find the path connecting 1 to n ` `void` `find(` `int` `i, ` `int` `prev) ` `{ ` ` ` `// Push the ith node ` ` ` `temp.push_back(i); ` ` ` ` ` `// If we reached our destination ` ` ` `if` `(i == n) { ` ` ` `path = (temp); ` ` ` `return` `; ` ` ` `} ` ` ` `for` `(` `int` `j = 0; j < graph[i].size(); j++) ` ` ` `if` `(graph[i][j] != prev) { ` ` ` ` ` `// Dfs for its children ` ` ` `find(graph[i][j], i); ` ` ` `} ` ` ` ` ` `// Remove the node ` ` ` `temp.pop_back(); ` `} ` ` ` `// Function to mark all the adjacent ` `// unvisited nodes ` `void` `mark(` `int` `i, ` `int` `visited[], ` `int` `c) ` `{ ` ` ` `if` `(!visited[i]) { ` ` ` ` ` `// Increase the count ` ` ` `if` `(c == 1) ` ` ` `c_A++; ` ` ` `else` ` ` `c_B++; ` ` ` `} ` ` ` ` ` `visited[i] = c; ` ` ` ` ` `// Increase the count ` ` ` `if` `(c == 1) ` ` ` `c_A++; ` ` ` `else` ` ` `c_B++; ` ` ` ` ` `// Dfs for all its unvisted adjacent nodes ` ` ` `for` `(` `int` `j = 0; j < graph[i].size(); j++) ` ` ` `if` `(!visited[graph[i][j]]) ` ` ` `mark(graph[i][j], visited, c); ` `} ` ` ` `// Function to find the winner among the players ` `void` `findWinner() ` `{ ` ` ` `// Finds the path ` ` ` `find(1, -1); ` ` ` ` ` `int` `visited[n + 1] = { 0 }; ` ` ` ` ` `for` `(` `int` `i = 0; i < path.size(); i++) { ` ` ` ` ` `// Mark nodes visited by ` ` ` `// A as 1 and B as 2 ` ` ` `if` `(i < ` `ceil` `(path.size() / 2.0)) ` ` ` `visited[path[i]] = 1, c_A++; ` ` ` `else` ` ` `visited[path[i]] = 2, c_B++; ` ` ` `} ` ` ` ` ` `// Mark all the adjacent unvisted nodes ` ` ` `for` `(` `int` `i = 0; i < path.size(); i++) ` ` ` `mark(path[i], ` ` ` `visited, ` ` ` `visited[path[i]]); ` ` ` ` ` `if` `(c_A > c_B) ` ` ` `cout << ` `"A wins"` `; ` ` ` `else` `if` `(c_A < c_B) ` ` ` `cout << ` `"B wins"` `; ` ` ` `else` ` ` `cout << ` `"Draw"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `n = 7; ` ` ` `graph.clear(); ` ` ` `graph.resize(n + 1); ` ` ` ` ` `// Graph ` ` ` `graph[6].push_back(4); ` ` ` `graph[4].push_back(6); ` ` ` `graph[6].push_back(5); ` ` ` `graph[5].push_back(6); ` ` ` `graph[5].push_back(7); ` ` ` `graph[7].push_back(5); ` ` ` `graph[5].push_back(2); ` ` ` `graph[2].push_back(5); ` ` ` `graph[3].push_back(4); ` ` ` `graph[4].push_back(3); ` ` ` `graph[1].push_back(4); ` ` ` `graph[4].push_back(1); ` ` ` ` ` `findWinner(); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` `from` `math ` `import` `ceil, floor ` ` ` `# Vector to store Tree ` `graph ` `=` `[[] ` `for` `i ` `in` `range` `(` `1000` `)] ` ` ` `# To check if there ` `# is path or not ` `found ` `=` `0` `n ` `=` `0` ` ` `# Path and temporary vector ` `path ` `=` `[] ` `temp ` `=` `[] ` ` ` `# Count of A and B ` `c_A ` `=` `0` `c_B ` `=` `0` ` ` `# Function to find the path connecting 1 to n ` `def` `find(i, prev): ` ` ` `global` `c_A, c_B, path ` ` ` ` ` `# Push the ith node ` ` ` `temp.append(i) ` ` ` ` ` `# If we reached our destination ` ` ` `if` `(i ` `=` `=` `n): ` ` ` `path ` `=` `temp ` ` ` `return` ` ` ` ` `for` `j ` `in` `graph[i]: ` ` ` `if` `j !` `=` `prev: ` ` ` ` ` `# Dfs for its children ` ` ` `find(j, i) ` ` ` ` ` `# Remove the node ` ` ` `del` `temp[` `-` `1` `] ` ` ` `# Function to mark all the adjacent ` `# unvisited nodes ` `def` `mark(i, visited, c): ` ` ` `global` `c_B, c_A ` ` ` ` ` `if` `visited[i] ` `=` `=` `0` `: ` ` ` ` ` `# Increase the count ` ` ` `if` `(c ` `=` `=` `1` `): ` ` ` `c_A ` `+` `=` `1` ` ` `else` `: ` ` ` `c_B ` `+` `=` `1` ` ` ` ` `visited[i] ` `=` `c ` ` ` ` ` `# Increase the count ` ` ` `if` `(c ` `=` `=` `1` `): ` ` ` `c_A ` `+` `=` `1` ` ` `else` `: ` ` ` `c_B ` `+` `=` `1` ` ` ` ` `# Dfs for all its unvisted adjacent nodes ` ` ` `for` `j ` `in` `graph[i]: ` ` ` `if` `(visited[j] ` `=` `=` `0` `): ` ` ` `mark(j, visited, c) ` ` ` `# Function to find the winner among the players ` `def` `findWinner(): ` ` ` `global` `c_B, c_A, path ` ` ` ` ` `# Finds the path ` ` ` `find(` `1` `, ` `-` `1` `) ` ` ` ` ` `visited ` `=` `[` `0` `for` `i ` `in` `range` `(n ` `+` `1` `)] ` ` ` ` ` `for` `i ` `in` `range` `(` `len` `(path)): ` ` ` ` ` `# Mark nodes visited by ` ` ` `# A as 1 and B as 2 ` ` ` `if` `(i < ceil(` `len` `(path) ` `/` `2.0` `)): ` ` ` `visited[path[i]] ` `=` `1` ` ` `c_A ` `+` `=` `1` ` ` `else` `: ` ` ` `visited[path[i]] ` `=` `2` ` ` `c_B ` `+` `=` `1` ` ` ` ` `# Mark all the adjacent unvisted nodes ` ` ` `for` `i ` `in` `path: ` ` ` `mark(i, visited, visited[i]) ` ` ` ` ` `if` `(c_A > c_B): ` ` ` `print` `(` `"A wins"` `) ` ` ` `elif` `(c_A < c_B): ` ` ` `print` `(` `"B wins"` `) ` ` ` `else` `: ` ` ` `print` `(` `"Draw"` `) ` ` ` `# Driver code ` `n ` `=` `7` ` ` `# Graph ` `graph[` `6` `].append(` `4` `) ` `graph[` `4` `].append(` `6` `) ` `graph[` `6` `].append(` `5` `) ` `graph[` `5` `].append(` `6` `) ` `graph[` `5` `].append(` `7` `) ` `graph[` `7` `].append(` `5` `) ` `graph[` `5` `].append(` `2` `) ` `graph[` `2` `].append(` `5` `) ` `graph[` `3` `].append(` `4` `) ` `graph[` `4` `].append(` `3` `) ` `graph[` `1` `].append(` `4` `) ` `graph[` `4` `].append(` `1` `) ` ` ` `findWinner() ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

**Output:**

A wins

## Recommended Posts:

- 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 contribute@geeksforgeeks.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.