Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (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)

Solution 2 (runtime: 765 ms):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve646():
    n = int(input())
    e = []
    for _ in range(n-1):
        u, v = map(int, input().split())
        u, v = u-1, v-1
        e.append([u, v])
    final_res = 0
    for _ in range(2):
        adj = [[] for _ in range(n)]
        for u, v in e:
            adj[u].append(v)
            adj[v].append(u)
        rm = [False] * n
        d = [0] * n
        for i in range(n):
            d[i] = len(adj[i])
        for _ in range(2):
            mx = u = -1
            for i in range(n):
                if not rm[i] and mx < d[i]:
                    mx = d[i]
                    u = i
            rm[u] = True
            d[u] = 0
            for v in adj[u]:
                d[v] -= 1
        res = 0
        visited = rm[:]
        def dfs(u):
            visited[u] = True
            q = [u]
            while q:
                u = q.pop()
                for v in adj[u]:
                    if not visited[v]:
                        visited[v] = True
                        q.append(v)
        for i in range(n):
            if not visited[i]:
                dfs(i)
                res += 1
        final_res = max(final_res, res)
        for i, edge in enumerate(e):
            u, v = edge
            e[i] = [n-1-u, n-1-v]
    return final_res

res_test = []
for _ in range(int(input())):
    res_test.append(solve646())
    
sys.stdout.write("\n".join(map(str, res_test))) #output all test