- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ program to find winner of cell coloring game

Suppose we have two arrays A and B both are with N elements each. Consider Amal and Bimal are playing a game on a board whose cell numbers are numbered from 1 to N. And N-1 roads. Roads are connecting two cells. So ith road is connecting A[i] to B[i]. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. Initially the cell 1 is marked as black color, and cell N is white. Other cells are not colored. Amal plays first, and they are playing alternatively. Amal selects an uncolored cells that is adjacent to black cell and paints black. Bimal selects an uncolored cell that is adjacent to a white cell and paints white. When a player cannot paint a cell, he looses. We have to find the winner.

So, if the input is like A = [3, 1, 3, 7, 5, 1]; B = [6, 2, 1, 4, 7, 4], then the output will be Amal, because if Amal first paints cell 2 black, he will win regardless of Bimal's moves.

## Steps

To solve this, we will follow these steps −

N := 99999 Define one adjacency list adjList Define three large arrays p, d, and ssz Define a function dfs(), this will take nd, par, dep, p[nd] := par d[nd] := dep ssz[nd] := 1 for each node i in adjList[nd], do if i XOR par is non-zero, then: dfs(i, nd, dep + 1) ssz[nd] := ssz[nd] + ssz[i] From the main method, do the following: n := size of A for initialize i := 1, when i < n, update (increase i by 1), do: u := A[i - 1], v := B[i - 1] insert v at the end of adjList[u] insert u at the end of adjList[v] dfs(1, 1, 0) nd := n for initialize i := 0, when i < (d[n] - 1) / 2, update (increase i by 1), do: nd := p[nd] return if 2 * ssz[nd] >= n, then "Bimal", otherwise "Amal"

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; int N = 99999; vector<vector<int>> adjList(N); vector<int> p(N), d(N), ssz(N); void dfs(int nd, int par, int dep){ p[nd] = par; d[nd] = dep; ssz[nd] = 1; for (int i : adjList[nd]){ if (i ^ par){ dfs(i, nd, dep + 1); ssz[nd] += ssz[i]; } } } string solve(vector<int> A, vector<int> B){ int n = A.size(); for (int i = 1; i < n; i++){ int u = A[i - 1], v = B[i - 1]; adjList[u].push_back(v); adjList[v].push_back(u); } dfs(1, 1, 0); int nd = n; for (int i = 0; i < (d[n] - 1) / 2; i++) nd = p[nd]; return (2 * ssz[nd] >= n ? "Bimal" : "Amal"); } int main(){ vector<int> A = { 3, 1, 3, 7, 5, 1 }; vector<int> B = { 6, 2, 1, 4, 7, 4 }; cout << solve(A, B) << endl; }

## Input

{ 3, 1, 3, 7, 5, 1 }, { 6, 2, 1, 4, 7, 4 }

## Output

Amal

- Related Articles
- C++ program to find winner of card game
- C++ program to find winner of ball removal game
- C++ Program to find winner of unique bidding game
- C++ Program to find winner name of stick crossing game
- C++ program to find winner of typing game after delay timing
- Program to find winner of stone game in Python
- Program to find winner of array removal game in Python
- Program to find winner of number reducing game in Python
- Program to find the winner of an array game using Python
- Program to find winner of a rower reducing game in Python
- Program to find winner of a rower breaking game in Python
- Python program to find score and name of winner of minion game
- Program to find winner of a set element removal game in Python
- Binary Tree Coloring Game in Python
- Predict the winner in Coin Game in C++