Black and white tree

Company: Bolt.earth

Difficulty: medium

Problem Statement

Black and white tree Problem Description You are given a tree with N vertices numbered 1 to N . The tree is an undirected connected graph with N - 1 edges, where edge i connects vertices a_i and b_i . The tree is rooted at vertex 1 . Every vertex is colored either white (written as the character 0 ) or black (written as the character 1 ). The colors are given as a string Color of length N , where Color[i] (0-indexed) is the color of vertex i + 1 . The subtree of a vertex i is the connected subgraph made of i together with all of its descendants (with respect to the root, vertex 1). The beauty of a vertex i is the number of paths inside its subtree whose two end vertices have opposite colors (one white and one black). Find the beauty of all N vertices. Complete the function solve(N, Color, Edges) , where Edges is the list of the N - 1 edges, each given as a pair [a_i, b_i] . It returns the list of the N beauties, the i -th value being the beauty of vertex i . Input Format The first line