Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 687 ms):
import io
import os
import sys


inf = 10 ** 18
mod = 10 ** 9 + 7

ONLINE_JUDGE = __debug__

if ONLINE_JUDGE:
    R = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline


    def _input() -> str:
        return R().decode()
else:
    R = sys.stdin.readline


    def _input() -> str:
        return R()


def get_int() -> int:
    return int(_input())


def get_str() -> str:
    return _input().strip()


def get_int_list() -> list[int]:
    return list(map(int, _input().strip().split()))


def init_matrix(n_, m_, value_=0) -> list[list]:
    return [[value_] * m_ for _ in range(n_)]


def solve() -> None:
    n = get_int()

    g = init_matrix(n, 0)
    cnt = [0] * n
    pairs = set()
    for i in range(n - 1):
        u, v = get_int_list()
        u -= 1
        v -= 1
        g[v].append(u)
        g[u].append(v)
        cnt[u] += 1
        cnt[v] += 1
        pairs.add((u, v))
        pairs.add((v, u))

    order = sorted(
        range(len(cnt)),
        key=lambda x: cnt[x],
        reverse=True,
    )
    u, v = order[:2]
    h = []
    for i in range(n):
        if cnt[i] == cnt[v]:
            h.append(i)

    if cnt[u] != cnt[v]:
        for w in h:
            if (u, w) not in pairs:
                print(cnt[u] + cnt[v] - 1)
                return

        print(cnt[u] + cnt[v] - 2)
    else:
        for i in range(len(h)):
            for j in range(i + 1, len(h)):
                if (h[i], h[j]) not in pairs:
                    print(cnt[u] + cnt[v] - 1)
                    return

        print(cnt[u] + cnt[v] - 2)


for _ in range(get_int()):
    solve()


Solution 2 (runtime: 781 ms):
t = int(input())

for x in range(t):
    n = int(input())
    edges = []
    adj_list = []

    for y in range(n+1):
        adj_list.append([])

    for e in range(n-1):
        a,b = list(map(int, input().split(" ")))
        
        adj_list[a].append(b)
        adj_list[b].append(a)


    for i in range(2):
        cnt_max_edges = 0
        max_edges = []
        for node in range(1, n+1):
            if adj_list[node] != None:
                if len(adj_list[node]) > cnt_max_edges:
                    cnt_max_edges = len(adj_list[node])
                    max_edges = [node]
                elif len(adj_list[node]) == cnt_max_edges:
                    max_edges.append(node)


        if len(max_edges) <= 2 or i == 1:
            max_edges = [max_edges[0]]
        else:
            f = False
            for idx1 in range(0, len(max_edges)):
                if f: break
                for idx2 in range(idx1+1, len(max_edges)):
                    if not max_edges[idx2] in adj_list[max_edges[idx1]]:
                        max_edges = [max_edges[idx1], max_edges[idx2]]
                        f = True
                        break


        for most_edges_node in max_edges:
            for node in adj_list[most_edges_node]:
                adj_list[node].remove(most_edges_node)

            adj_list[most_edges_node] = None

        if len(max_edges) >= 2:
            break

    count_cc = 0
    connected = {}
    v = 1
    for node in range(1, n+1):
        if adj_list[node] != None and not node in connected:
            queue = [node]

            while len(queue) > 0:
                curr = queue.pop()
                connected[curr] = count_cc
                for adj_node in adj_list[curr]:
                    if not adj_node in connected:
                        queue.append(adj_node)
                        connected[adj_node] = count_cc

            count_cc += 1

    print(count_cc)