Potential Tree
Practice
5 (2 votes)
Depth first search
Algorithms
Graphs
Problem
48% Success 280 Attempts 50 Points 4s Time Limit 256MB Memory 1024 KB Max Code

You are given a tree consisting of \(N\) vertices. Its root is not fixed yet. The \(potential\) of a non-leaf vertex is the count of its children (distance of \(1\) from the vertex) with a non-zero \(potential\). The \(potential\) of a leaf vertex \(i\) is \(i \% 2\).

Your task is to calculate the \(potential\) of every possible root. Find an array \(A\) such that \(A[i]\) represents the \(potential\) of vertex \(i\) if the tree was rooted at \(i\).

Input Format:

  • The first line of input contains a single integer \(T\), denoting the number of test cases.
  • The first line of each test case contains a single integer, \(N\), denoting the number of vertices.
  • The following \(N-1\) lines contain two integers \(u,v\) denoting an edge between vertex \(u\) and vertex \(v\).

Output Format:

For each test case, print an array \(A\) such that \(A[i]\) represents the \(potential\) of vertex \(i\) if the tree was rooted at \(i\).

Constraints:

\(1 <= T <= 10\)

\(3 <= N <= 10^5\)

\(0 <= u, v < N\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
2 votes
Tags:
AlgorithmsApprovedDepth First SearchGrammar-VerifiedGraphsHardHiringReadyRecruit
Points:50
2 votes
Tags:
AlgorithmsApprovedGraphsHardOpen
Points:50
4 votes
Tags:
AlgorithmsTreesC++GraphsDepth First Search