Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 577 ms):
import sys
import math
import collections
import heapq
input=sys.stdin.readline
t=int(input())
for w in range(t):
    n=int(input())
    d={}
    deg=[0 for i in range(n)]
    for i in range(n):
        d[i]=[]
    for i in range(n-1):
        u,v=(int(i) for i in input().split())
        d[u-1].append(v-1)
        d[v-1].append(u-1)
        deg[u-1]+=1
        deg[v-1]+=1
    deg1=[deg[i] for i in range(n)]
    d1={}
    for i in range(n):
        d1[i]=d[i]
    ans=1
    ind,m=-1,-1
    for i in range(n):
        if(deg[i]>m):
            ind=i
            m=deg[i]
    ans+=m-1
    for i in d:
        for j in range(len(d[i])):
            if(d[i][j]==ind):
                del d[i][j]
                deg[i]-=1
                break
    del d[ind]
    del deg[ind]
    ans+=max(deg)-1
    if(deg1.count(m)>1):
        ans1=1
        ind1,m1=-1,-1
        for i in range(n):
            if(deg1[i]>m1 and i!=ind):
                ind1=i
                m1=deg1[i]
        ans1+=m1-1
        for i in d1:
            for j in range(len(d1[i])):
                if(d1[i][j]==ind1):
                    del d1[i][j]
                    deg1[i]-=1
                    break
        del d1[ind1]
        del deg1[ind1]
        ans1+=max(deg1)-1
        ans=max(ans,ans1)
    print(ans)

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)