Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 406 ms):
"""
For God so loved the world that he gave his one and only Son, 
that whoever believes in him shall not perish but have eternal life.
JOHN 3:16 
"""
import random
from math import *
from functools import lru_cache
from itertools import accumulate
from fractions import Fraction
from bisect import bisect_left as bl
from bisect import bisect_right as br
from operator import itemgetter
from collections import Counter,deque,defaultdict
from heapq import heappop as hpop, heappush as hpush
# constants
p = 10**9 + 7 
ANS = ['NO','YES']
import sys
input = sys.stdin.readline
output = sys.stdout.write
#sys.setrecursionlimit(110000)
# #For large input
# input = sys.stdin.read
# data = input().split()
# Shortcut for input()
def I(): return int(input())
def S(): return input().strip()
def F(): return float(input())
def MI(): return map(int, input().split())
def MS(): return map(str, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LS(): return list(map(str, input().split()))
def LF(): return list(map(float , input().split()))
# Shortcut for output()
res = []
def OI(res): output("\n".join(res))
def OL(res): output("\n".join(" ".join(i) for i in res))






t = I()
for _ in range(t):
    n = I()
    deg = [0]*(n+1)
    g = [[] for _ in range(n+1)]
    for _ in range(n-1):
        u,v = MI()
        deg[u] += 1
        deg[v]+=1
        g[u].append(v)
        g[v].append(u)
    m = max(deg)
    if deg.count(m) >= 3:
        print(2*m-1)
        continue
    ans = m
    node = deg.index(m)
    deg[node] = 0
    for child in g[node]:deg[child]-=1
    ans += (max(deg) - 1)
    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)