Infected Tree (Codeforces 1689C)
Tags: DP, trees

Byteland is a beautiful land known because of its beautiful trees.Misha has found a binary tree with $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. A binary tree is an acyclic connected bidirectional graph containing $$$n$$$ vertices and $$$n - 1$$$ edges. Each vertex has a degree at most $$$3$$$, whereas the root is the vertex with the number $$$1$$$ and it has a degree at most $$$2$$$.Unfortunately, the root got infected.The following process happens $$$n$$$ 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).

Input: There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1\leq t\leq 5000$$$) — the number of test cases. This is followed by the test cases description.The first line of each test case contains one integer $$$n$$$ ($$$2\leq n\leq 3\cdot 10^5$$$) — the number of vertices of the tree. The $$$i$$$-th of the following $$$n-1$$$ lines in the test case contains two positive integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n$$$), meaning that there exists an edge between them in the graph. It is guaranteed that the graph is a binary tree rooted at $$$1$$$. It is also guaranteed that the sum of $$$n$$$ over all test cases won't exceed $$$3\cdot 10^5$$$.

Output: For each test case, output the maximum number of vertices Misha can save.

Note: In the first test case, the only possible action is to delete vertex $$$2$$$, after which we save $$$0$$$ vertices in total.In the second test case, if we delete vertex $$$2$$$, we can save vertices $$$3$$$ and $$$4$$$.  

Sample Inputs:
4
2
1 2
4
1 2
2 3
2 4
7
1 2
1 5
2 3
2 4
5 6
5 7
15
1 2
2 3
3 4
4 5
4 6
3 7
2 8
1 9
9 10
9 11
10 12
10 13
11 14
11 15

Sample Outputs:
0
2
2
10

Solution (Python):
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n = int(input())
    g = [[] for _ in range(n)]
    for i in range(n - 1):
        (u, v) = map(int, input().split())
        g[u - 1].append(v - 1)
        g[v - 1].append(u - 1)
    q = [(0, -1)]
    h = 0
    c = 2
    while q:
        new_q = []
        for (i, j) in q:
            x = [child for child in g[i] if child != j]
            c = min(c, len(x))
            for k in x:
                new_q.append([k, i])
        if c < 2:
            print(n - 1 - c - 2 * h)
            break
        h += 1
        q = new_q

Solution (Python):
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n = int(input())
    g = [[] for _ in range(n)]
    for i in range(n - 1):
        (u, v) = map(int, input().split())
        g[u - 1].append(v - 1)
        g[v - 1].append(u - 1)
    q = [(0, -1)]
    h = 0
    c = 2
    while q:
        new_q = []
        for (i, j) in q:
            x = [child for child in g[i] if child != j]
            c = min(c, len(x))
            for k in x:
                new_q.append([k, i])
        if c < 2:
            print(n - 1 - c - 2 * h)
            break
        h += 1
        q = new_q

Solution (Python):
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n = int(input())
    g = [[] for _ in range(n)]
    for i in range(n - 1):
        (u, v) = map(int, input().split())
        g[u - 1].append(v - 1)
        g[v - 1].append(u - 1)
    q = [(0, -1)]
    h = 0
    c = 2
    while q:
        new_q = []
        for (i, j) in q:
            x = [child for child in g[i] if child != j]
            c = min(c, len(x))
            for k in x:
                new_q.append([k, i])
        if c < 2:
            print(n - 1 - c - 2 * h)
            break
        h += 1
        q = new_q