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: 468 ms):
def max1(t) :
    indice=0
    nb=t[0]
    n=len(t)
    for i in range(1,n) :
        if t[i]>nb :
            indice=i
            nb=t[i]
    return nb,indice

def max2(t,j) :
    
    if j==0 :
        indice=1
        nb=t[1]
    
    else:
        nb=t[0]
        indice=0
    n=len(t)
    for i in range(1,n) :
        if i!=j :
            if t[i]>nb :
                indice=i
                nb=t[i]
    return nb,indice

for _ in range(int(input())):
    n=int(input())
    if n<=2 :
        for i in range(n-1):
            u,v=map(int,input().split())
        print(0)
    else:
        t=[0]*n
        t2=[0]*n
        l=[]
        for i in range(n-1):
            u,v=map(int,input().split())
            l.append((u,v))
            t[u-1]+=1
            t[v-1]+=1
            t2[u-1]+=1
            t2[v-1]+=1
        nb1,indice1=max1(t)
        nb3,indice3=max2(t2,indice1)
        t[indice1]=0
        t2[indice3]=0
        for i in l :
            if i[0]-1==indice1 :
                t[i[1]-1]-=1
            elif i[1]-1==indice1 :
                t[i[0]-1]-=1
        for i in l :
            if i[0]-1==indice3 :
                t2[i[1]-1]-=1
            elif i[1]-1==indice3 :
                t2[i[0]-1]-=1
        nb2,indice2=max1(t)
        nb4,indice4=max1(t2)
 
  
        print(max(nb1+nb2-1,nb3+nb4-1))