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: 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)