[Solution] Infected Tree Codeforces Solution | Codeforces Problem Solution 2022
Byteland is a beautiful land known because of its beautiful trees.
Misha has found a binary tree with vertices, numbered from to . A binary tree is an acyclic connected bidirectional graph containing vertices and edges. Each vertex has a degree at most , whereas the root is the vertex with the number and it has a degree at most .
Unfortunately, the root got infected.
The following process happens times:
- Misha either chooses a non-infected (and not deleted) vertex and deletes it with all edges which have an end in this vertex or just does nothing.
- Then, the infection spreads to each vertex that is connected by an edge to an already infected vertex (all already infected vertices remain infected).
As Misha does not have much time to think, please tell him what is the maximum number of vertices he can save from the infection (note that deleted vertices are not counted as saved).
There are several test cases in the input data. The first line contains a single integer () — the number of test cases. This is followed by the test cases description.
The first line of each test case contains one integer () — the number of vertices of the tree.
The -th of the following lines in the test case contains two positive integers and (), meaning that there exists an edge between them in the graph.
It is guaranteed that the graph is a binary tree rooted at . It is also guaranteed that the sum of over all test cases won't exceed .
For each test case, output the maximum number of vertices Misha can save.
No comments:
Post a Comment