Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 1937 ms):
import sys, math, random; from heapq import heapify, heappop, heappush;
from collections import defaultdict, Counter, deque; from bisect import bisect_left, bisect_right
def IL(): return list(map(int, sys.stdin.readline().strip().split()))
def S(): return sys.stdin.readline().strip()

def solve():
    n = IL()[0]
    adj = [[] for _ in range(n + 1)]
    degree = [0] * (n + 1)
    ind = [0] * (n + 1)
    for _ in range(n - 1):
        x, y = IL()
        adj[x].append(y)
        adj[y].append(x)
        degree[x] += 1
        degree[y] += 1
        ind[x] += 1
        ind[y] += 1

    que = deque()
    for node in range(1, n + 1):
        if degree[node] == 1:
            que.append(node)
    val = [-1] * (n + 1)
    level = 0
    while que:
        for _ in range(len(que)):
            node = que.popleft()
            val[node] = level
            for nei in adj[node]:
                ind[nei] -= 1
                if ind[nei] == 1:
                    que.append(nei)
            level += 1
    
    arr = []
    for node in range(1, n + 1):
        arr.append((degree[node], -val[node], node))
    arr.sort()

    ans = 1
    deg, _, first = arr.pop()
    ans += deg - 1
    for nei in adj[first]:
        degree[nei] -= 1
    arr = []
    for node in range(1, n + 1):
        if node != first:
            arr.append((degree[node], -val[node], node))
    arr.sort()
    deg, _, _ = arr.pop()
    ans += deg - 1
    print(ans)

    


    

tt = 1
tt = IL()[0]
for _ in range(tt):
    solve()

Solution 2 (runtime: 811 ms):
import sys,math,bisect,heapq
input=sys.stdin.readline
#remember file names are input.txt and output.txt 
#remember to pass only one argument to print
#add this lines before your program and after defining fast io(or that will over write testing)
#after testing done set flag =0
flag=0
if flag:
    class out:
        def __init__(self):
            self.fo=open("cp_with_python/cp/output.txt","w")
        def inu(self,*x):
            if str(type(x))!="<class 'str'>":
                x=str(x)
            self.fo.write(x)
            self.fo.write("\n")
    class inp:
        def __init__(self):
            self.fi=open("cp_with_python/cp/input.txt","r")
        def inu(self):
            return self.fi.readline()
    fy=out()
    print=fy.inu
    fx=inp()
    input=fx.inu

for _ in range(int(input())):
    n=int(input())
    #n,s1,s2=map(int,input().split())
    #a=list(map(int,input().split()))
    graph=[set() for i in range(n)]
    inc=[[i,0] for i in range(n)]
    for i in range(n-1):
        x,y=map(int,input().split())
        graph[x-1].add(y-1)
        graph[y-1].add(x-1)
        inc[x-1][1]+=1
        inc[y-1][1]+=1
    inc.sort(key=lambda x:x[1],reverse=True)
    ans=0
    if n==2:
        print(0)
    else:
        if inc[0][1]==inc[1][1]:
            if inc[0][1]==inc[2][1]:
                print(2*inc[0][1]-1)
            else:
                if inc[1][0] in graph[inc[0][0]]:
                    print(2*inc[0][1]-2)
                else:
                    print(2*inc[0][1]-1)
        else:
            for i in range(1,n):
                if inc[i][1]==inc[1][1]:
                    if inc[i][0] not in graph[inc[0][0]]:
                        print(inc[1][1]+inc[0][1]-1)
                        break
                else:
                    print(inc[0][1]+inc[1][1]-2)
                    break
            else:
                print(inc[0][1]+inc[1][1]-2)