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 used 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 Treevector<vector<int> > graph;Â
// To check if there// is path or notint found = 0, n;Â
// Path and temporary vectorvector<int> path, temp;Â
// Count of A and Bint c_A = 0, c_B = 0;Â
// Function to find the path connecting 1 to nvoid 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 nodesvoid 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 unvisited 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 playersvoid 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 unvisited 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 codeint 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;} |
Java
// Java implementation of the // above approachimport java.util.*;class GFG{Â
// Vector to store Treestatic Vector<Integer> []graph;Â
// To check if there// is path or notstatic int found = 0, n;Â
// Path and temporary vectorstatic Vector<Integer> path =Â Â Â Â Â Â Â new Vector<>();static Vector<Integer> temp = Â Â Â Â Â Â Â new Vector<>();Â
// Count of A and Bstatic int c_A = 0, c_B = 0;Â
// Function to find the path // connecting 1 to nstatic void find(int i,                  int prev){  // Push the ith node  temp.add(i);Â
  // If we reached our   // destination  if (i == n)   {    path = (temp);    return;  }  for (int j = 0;            j < graph[i].size(); j++)    if (graph[i].get(j) != prev)     {      // Dfs for its children      find(graph[i].get(j), i);    }Â
  // Remove the node  temp.remove(temp.size() - 1);}Â
// Function to mark all the // adjacent unvisited nodesstatic void mark(int i,                  int visited[],                  int c){  if (visited[i] > 0)   {    // 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 unvisited   // adjacent nodes  for (int j = 0;           j < graph[i].size(); j++)    if (visited[graph[i].get(j)] > 0)      mark(graph[i].get(j),            visited, c);}Â
// Function to find the winner // among the playersstatic void findWinner(){  // Finds the path  find(1, -1);Â
  int visited[] = new int[n + 1];Â
  for (int i = 0;            i < path.size(); i++)   {    // Mark nodes visited by    // A as 1 and B as 2    if (i < Math.ceil(path.size() / 2.0))     {      visited[path.get(i)] = 1;      c_A++;    }    else    {      visited[path.get(i)] = 2;      c_B++;    }  }Â
  // Mark all the adjacent   // unvisited nodes  for (int i = 0;            i < path.size(); i++)    mark(path.get(i),         visited,         visited[path.get(i)]);Â
  if (c_A > c_B)    System.out.print("A wins");  else if (c_A < c_B)    System.out.print("B wins");  else    System.out.print("Draw");}Â
// Driver code@SuppressWarnings("unchecked")public static void main(String[] args){  n = 7;  graph = new Vector[n + 1];  for (int i = 0;            i < graph.length; i++)    graph[i] = new Vector<Integer>();     // Graph  graph[6].add(4);  graph[4].add(6);  graph[6].add(5);  graph[5].add(6);  graph[5].add(7);  graph[7].add(5);  graph[5].add(2);  graph[2].add(5);  graph[3].add(4);  graph[4].add(3);  graph[1].add(4);  graph[4].add(1);Â
  findWinner();}}Â
// This code is contributed by Amit Katiyar |
Python3
# Python3 implementation of the above approachfrom math import ceil, floorÂ
# Vector to store Treegraph = [[] for i in range(1000)]Â
# To check if there# is path or notfound = 0n = 0Â
# Path and temporary vectorpath = []temp = []Â
# Count of A and Bc_A = 0c_B = 0Â
# Function to find the path connecting 1 to ndef 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 nodesdef 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 unvisited adjacent nodes    for j in graph[i]:        if (visited[j] == 0):            mark(j, visited, c)Â
# Function to find the winner among the playersdef 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 unvisited 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 coden = 7Â
# Graphgraph[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 |
C#
// C# implementation of the // above approachusing System;using System.Collections.Generic;class GFG{Â
// List to store Treestatic List<int> []graph;Â
// To check if there// is path or notstatic int found = 0, n;Â
// Path and temporary vectorstatic List<int> path =Â Â Â Â Â Â Â new List<int>();static List<int> temp = Â Â Â Â Â Â Â new List<int>();Â
// Count of A and Bstatic int c_A = 0, c_B = 0;Â
// Function to find the path // connecting 1 to nstatic void find(int i,                  int prev){  // Push the ith node  temp.Add(i);Â
  // If we reached our   // destination  if (i == n)   {    path = (temp);    return;  }  for (int j = 0;            j < graph[i].Count; j++)    if (graph[i][j] != prev)     {      // Dfs for its children      find(graph[i][j], i);    }Â
  // Remove the node  temp.Remove(temp.Count - 1);}Â
// Function to mark all the // adjacent unvisited nodesstatic void mark(int i,                  int []visited,                  int c){  if (visited[i] > 0)   {    // 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 unvisited   // adjacent nodes  for (int j = 0;           j < graph[i].Count; j++)    if (visited[graph[i][j]] > 0)      mark(graph[i][j],            visited, c);}Â
// Function to find the winner // among the playersstatic void findWinner(){  // Finds the path  find(1, -1);Â
  int []visited = new int[n + 1];Â
  for (int i = 0;            i < path.Count; i++)   {    // Mark nodes visited by    // A as 1 and B as 2    if (i < Math.Ceiling(path.Count / 2.0))     {      visited[path[i]] = 1;      c_A++;    }    else    {      visited[path[i]] = 2;      c_B++;    }  }Â
  // Mark all the adjacent   // unvisited nodes  for (int i = 0;            i < path.Count; i++)    mark(path[i],         visited,         visited[path[i]]);Â
  if (c_A > c_B)    Console.Write("A wins");  else if (c_A < c_B)    Console.Write("B wins");  else    Console.Write("Draw");}Â
// Driver codeÂ
public static void Main(String[] args){  n = 7;  graph = new List<int>[n + 1];     for (int i = 0;            i < graph.Length; i++)    graph[i] = new List<int>();     // Graph  graph[6].Add(4);  graph[4].Add(6);  graph[6].Add(5);  graph[5].Add(6);  graph[5].Add(7);  graph[7].Add(5);  graph[5].Add(2);  graph[2].Add(5);  graph[3].Add(4);  graph[4].Add(3);  graph[1].Add(4);  graph[4].Add(1);Â
  findWinner();}}Â
// This code is contributed by shikhasingrajput |
Javascript
<script>Â
// Javascript implementation of the// above approachÂ
// Vector to store Treelet graph;Â
// To check if there// is path or notlet found = 0, n;Â
// Path and temporary vectorlet path = [];let temp = [];Â
// Count of A and Blet c_A = 0, c_B = 0;Â
// Function to find the path// connecting 1 to nfunction find(i, prev){         // Push the ith node    temp.push(i);      // If we reached our    // destination    if (i == n)    {        path = (temp);        return;    }    for(let j = 0;            j < graph[i].length; j++)    {        if (graph[i][j] != prev)        {            // Dfs for its children            find(graph[i][j], i);        }    }    // Remove the node    temp.pop();}Â
// Function to mark all the// adjacent unvisited nodesfunction mark(i, visited, c){    if (visited[i] > 0)    {                 // 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 unvisited    // adjacent nodes    for(let j = 0;            j < graph[i].length; j++)        if (visited[graph[i][j]] > 0)            mark(graph[i][j], visited, c);}Â
// Function to find the winner// among the playersfunction findWinner(){         // Finds the path    find(1, -1);         let visited = new Array(n + 1);         for(let i = 0;            i < path.length; i++)    {                 // Mark nodes visited by        // A as 1 and B as 2        if (i < Math.ceil(path.length / 2.0))        {            visited[path[i]] = 1;            c_A++;        }        else        {            visited[path[i]] = 2;            c_B++;        }    }         // Mark all the adjacent    // unvisited nodes    for(let i = 0;            i < path.length; i++)        mark(path[i], visited,             visited[path[i]]);         if (c_A > c_B)        document.write("A wins");    else if (c_A < c_B)        document.write("B wins");    else        document.write("Draw");}Â
// Driver coden = 7;graph = new Array(n + 1);for(let i = 0;Â Â Â Â Â Â Â Â i < graph.length; i++)Â Â Â Â graph[i] = [];Â
// Graphgraph[6].push(4);graph[4].push(6);graph[6].push(5);graph[5].push(6);graph[5].push(7);graph[7].push(5);graph[5].push(2);graph[2].push(5);graph[3].push(4);graph[4].push(3);graph[1].push(4);graph[4].push(1);Â
findWinner();Â
// This code is contributed by patel2127Â
</script> |
A wins
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

