ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 1171 ms):
def solution():
    n = int(input())
    u_v = [list(map(lambda x: int(x)-1, input().split())) for _ in range(n-1)]
    adj = [set() for _ in range(n)]
    for u, v in u_v:
        adj[u].add(v)
        adj[v].add(u)
    _, _, i = max((len(adj[u]), sum(int(len(adj[u]) > len(adj[v])) for v in adj[u]), u) for u in range(n))
    for v in adj[i]:
        adj[v].remove(i)
    j = max((j for j in range(n) if j != i), key=lambda x: len(adj[x]))
    return len(adj[i])+len(adj[j])-1

for _ in range(int(input())):
    print(solution())

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))
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 687 ms):
import io
import os
import sys


inf = 10 ** 18
mod = 10 ** 9 + 7

ONLINE_JUDGE = __debug__

if ONLINE_JUDGE:
    R = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline


    def _input() -> str:
        return R().decode()
else:
    R = sys.stdin.readline


    def _input() -> str:
        return R()


def get_int() -> int:
    return int(_input())


def get_str() -> str:
    return _input().strip()


def get_int_list() -> list[int]:
    return list(map(int, _input().strip().split()))


def init_matrix(n_, m_, value_=0) -> list[list]:
    return [[value_] * m_ for _ in range(n_)]


def solve() -> None:
    n = get_int()

    g = init_matrix(n, 0)
    cnt = [0] * n
    pairs = set()
    for i in range(n - 1):
        u, v = get_int_list()
        u -= 1
        v -= 1
        g[v].append(u)
        g[u].append(v)
        cnt[u] += 1
        cnt[v] += 1
        pairs.add((u, v))
        pairs.add((v, u))

    order = sorted(
        range(len(cnt)),
        key=lambda x: cnt[x],
        reverse=True,
    )
    u, v = order[:2]
    h = []
    for i in range(n):
        if cnt[i] == cnt[v]:
            h.append(i)

    if cnt[u] != cnt[v]:
        for w in h:
            if (u, w) not in pairs:
                print(cnt[u] + cnt[v] - 1)
                return

        print(cnt[u] + cnt[v] - 2)
    else:
        for i in range(len(h)):
            for j in range(i + 1, len(h)):
                if (h[i], h[j]) not in pairs:
                    print(cnt[u] + cnt[v] - 1)
                    return

        print(cnt[u] + cnt[v] - 2)


for _ in range(get_int()):
    solve()


Solution 2 (runtime: 1593 ms):
def solve():
    n = int(input())
    d = [{'id': _ + 1, 'v': []} for _ in range(n)]
    for i in range(n - 1):
        u, v = map(int, input().split())
        d[u - 1]['v'].append(v)
        d[v - 1]['v'].append(u)
    if n == 2:
        print(0)
    else:
        for i in range(n):
            s = 0
            for j in d[i]['v']:
                s += len(d[j - 1]['v'])
            d[i]['s'] = s
        d.sort(key=lambda x: (-len(x['v']), x['s']))
        cnt = 1
        cnt += len(d[0]['v']) - 1
        for item in d:
            if d[0]['id'] in item['v']:
                item['v'].remove(d[0]['id'])
        d.remove(d[0])
        d.sort(key=lambda x: (-len(x['v']), x['s']))
        cnt += len(d[0]['v']) - 1
        print(cnt)


for _ in range(int(input())):
    solve()
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 687 ms):
import io
import os
import sys


inf = 10 ** 18
mod = 10 ** 9 + 7

ONLINE_JUDGE = __debug__

if ONLINE_JUDGE:
    R = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline


    def _input() -> str:
        return R().decode()
else:
    R = sys.stdin.readline


    def _input() -> str:
        return R()


def get_int() -> int:
    return int(_input())


def get_str() -> str:
    return _input().strip()


def get_int_list() -> list[int]:
    return list(map(int, _input().strip().split()))


def init_matrix(n_, m_, value_=0) -> list[list]:
    return [[value_] * m_ for _ in range(n_)]


def solve() -> None:
    n = get_int()

    g = init_matrix(n, 0)
    cnt = [0] * n
    pairs = set()
    for i in range(n - 1):
        u, v = get_int_list()
        u -= 1
        v -= 1
        g[v].append(u)
        g[u].append(v)
        cnt[u] += 1
        cnt[v] += 1
        pairs.add((u, v))
        pairs.add((v, u))

    order = sorted(
        range(len(cnt)),
        key=lambda x: cnt[x],
        reverse=True,
    )
    u, v = order[:2]
    h = []
    for i in range(n):
        if cnt[i] == cnt[v]:
            h.append(i)

    if cnt[u] != cnt[v]:
        for w in h:
            if (u, w) not in pairs:
                print(cnt[u] + cnt[v] - 1)
                return

        print(cnt[u] + cnt[v] - 2)
    else:
        for i in range(len(h)):
            for j in range(i + 1, len(h)):
                if (h[i], h[j]) not in pairs:
                    print(cnt[u] + cnt[v] - 1)
                    return

        print(cnt[u] + cnt[v] - 2)


for _ in range(get_int()):
    solve()


Solution 2 (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)
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 687 ms):
import io
import os
import sys


inf = 10 ** 18
mod = 10 ** 9 + 7

ONLINE_JUDGE = __debug__

if ONLINE_JUDGE:
    R = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline


    def _input() -> str:
        return R().decode()
else:
    R = sys.stdin.readline


    def _input() -> str:
        return R()


def get_int() -> int:
    return int(_input())


def get_str() -> str:
    return _input().strip()


def get_int_list() -> list[int]:
    return list(map(int, _input().strip().split()))


def init_matrix(n_, m_, value_=0) -> list[list]:
    return [[value_] * m_ for _ in range(n_)]


def solve() -> None:
    n = get_int()

    g = init_matrix(n, 0)
    cnt = [0] * n
    pairs = set()
    for i in range(n - 1):
        u, v = get_int_list()
        u -= 1
        v -= 1
        g[v].append(u)
        g[u].append(v)
        cnt[u] += 1
        cnt[v] += 1
        pairs.add((u, v))
        pairs.add((v, u))

    order = sorted(
        range(len(cnt)),
        key=lambda x: cnt[x],
        reverse=True,
    )
    u, v = order[:2]
    h = []
    for i in range(n):
        if cnt[i] == cnt[v]:
            h.append(i)

    if cnt[u] != cnt[v]:
        for w in h:
            if (u, w) not in pairs:
                print(cnt[u] + cnt[v] - 1)
                return

        print(cnt[u] + cnt[v] - 2)
    else:
        for i in range(len(h)):
            for j in range(i + 1, len(h)):
                if (h[i], h[j]) not in pairs:
                    print(cnt[u] + cnt[v] - 1)
                    return

        print(cnt[u] + cnt[v] - 2)


for _ in range(get_int()):
    solve()


Solution 2 (runtime: 890 ms):
import heapq




tc = int(input())
for _ in range(0,tc):
  n = int(input())
  
  graph = [[] for i in range(0,n)]
  for i in range(0,n-1):
    inp_arr = [int(i) for i in input().split()]
    v1 = inp_arr[0]-1
    v2 = inp_arr[1]-1
    graph[v1].append(v2)
    graph[v2].append(v1)
  
  edge_count = []
  for i in range(0,n):
    heapq.heappush(edge_count,(-len(graph[i]),i))
  if(n==2):
    print(0)
  else:  
    v1 = heapq.heappop(edge_count)
    v2 = heapq.heappop(edge_count)
    v3 = heapq.heappop(edge_count)
    
    if(v1[0] == v2[0]==v3[0]):
      print(2*-v1[0]-1)
    else:
      
      v1_connections = set()
      for i in range(0,len(graph[v1[1]])):
        v1_connections.add(graph[v1[1]][i])
      
      if(v2[0]!=v3[0]):
        add = -1 if v2[1] in v1_connections else 0
        print(-v1[0]-v2[0]-1+add)
      else: 
        same_cons = [v2[1],v3[1]]
        l = v2[0]
        while(len(edge_count) > 0):
          v = heapq.heappop(edge_count)
          if(v[0] == l):
            same_cons.append(v[1])
          else:
            break
        
        add = -1  
        for v in same_cons:
          if(v not in v1_connections):
            add = 0
            break
        print(-v1[0]-v2[0]-1+add)
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 687 ms):
import io
import os
import sys


inf = 10 ** 18
mod = 10 ** 9 + 7

ONLINE_JUDGE = __debug__

if ONLINE_JUDGE:
    R = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline


    def _input() -> str:
        return R().decode()
else:
    R = sys.stdin.readline


    def _input() -> str:
        return R()


def get_int() -> int:
    return int(_input())


def get_str() -> str:
    return _input().strip()


def get_int_list() -> list[int]:
    return list(map(int, _input().strip().split()))


def init_matrix(n_, m_, value_=0) -> list[list]:
    return [[value_] * m_ for _ in range(n_)]


def solve() -> None:
    n = get_int()

    g = init_matrix(n, 0)
    cnt = [0] * n
    pairs = set()
    for i in range(n - 1):
        u, v = get_int_list()
        u -= 1
        v -= 1
        g[v].append(u)
        g[u].append(v)
        cnt[u] += 1
        cnt[v] += 1
        pairs.add((u, v))
        pairs.add((v, u))

    order = sorted(
        range(len(cnt)),
        key=lambda x: cnt[x],
        reverse=True,
    )
    u, v = order[:2]
    h = []
    for i in range(n):
        if cnt[i] == cnt[v]:
            h.append(i)

    if cnt[u] != cnt[v]:
        for w in h:
            if (u, w) not in pairs:
                print(cnt[u] + cnt[v] - 1)
                return

        print(cnt[u] + cnt[v] - 2)
    else:
        for i in range(len(h)):
            for j in range(i + 1, len(h)):
                if (h[i], h[j]) not in pairs:
                    print(cnt[u] + cnt[v] - 1)
                    return

        print(cnt[u] + cnt[v] - 2)


for _ in range(get_int()):
    solve()


Solution 2 (runtime: 764 ms):
def li():return [int(i)for i in input().split()]
     
for _ in range(int(input())):
    n,=li()
    nv=[0]*(n+1)
    vv=[[]for i in range(n+1)]
    for i in range(n-1):
    	x,y=li()
    	nv[x]+=1
    	vv[x]+=[y]
    	nv[y]+=1
    	vv[y]+=[x]
    m=max(nv)
    if nv.count(m)>=3:print(2*m-1);continue
    node=nv.index(m)
    ans=m
    nv[node]=0
    for e in vv[node]:nv[e]-=1
    ans+=max(nv)-1
    print(ans)
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 687 ms):
import io
import os
import sys


inf = 10 ** 18
mod = 10 ** 9 + 7

ONLINE_JUDGE = __debug__

if ONLINE_JUDGE:
    R = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline


    def _input() -> str:
        return R().decode()
else:
    R = sys.stdin.readline


    def _input() -> str:
        return R()


def get_int() -> int:
    return int(_input())


def get_str() -> str:
    return _input().strip()


def get_int_list() -> list[int]:
    return list(map(int, _input().strip().split()))


def init_matrix(n_, m_, value_=0) -> list[list]:
    return [[value_] * m_ for _ in range(n_)]


def solve() -> None:
    n = get_int()

    g = init_matrix(n, 0)
    cnt = [0] * n
    pairs = set()
    for i in range(n - 1):
        u, v = get_int_list()
        u -= 1
        v -= 1
        g[v].append(u)
        g[u].append(v)
        cnt[u] += 1
        cnt[v] += 1
        pairs.add((u, v))
        pairs.add((v, u))

    order = sorted(
        range(len(cnt)),
        key=lambda x: cnt[x],
        reverse=True,
    )
    u, v = order[:2]
    h = []
    for i in range(n):
        if cnt[i] == cnt[v]:
            h.append(i)

    if cnt[u] != cnt[v]:
        for w in h:
            if (u, w) not in pairs:
                print(cnt[u] + cnt[v] - 1)
                return

        print(cnt[u] + cnt[v] - 2)
    else:
        for i in range(len(h)):
            for j in range(i + 1, len(h)):
                if (h[i], h[j]) not in pairs:
                    print(cnt[u] + cnt[v] - 1)
                    return

        print(cnt[u] + cnt[v] - 2)


for _ in range(get_int()):
    solve()


Solution 2 (runtime: 671 ms):
from sys import stdin
input = stdin.readline

INF = float('inf')
MOD = 1_000_000_007
MOD1 = 998_244_353

from heapq import heappush,heappop
from bisect import bisect_left
from collections import deque

def solve(I):
    n, = map(int,input().strip().split())
    adj = [set() for _ in range(n+1)]
    for _ in range(n-1):
        u,v = map(int,input().strip().split())
        adj[u].add(v)
        adj[v].add(u)

    if n == 2:
        print(0)
        return

    dfs = [(1,0)]
    for v,p in dfs:
        for u in adj[v]:
            if u == p:
                continue
            dfs.append((u,v))

    mx1 = 1
    for v,_ in dfs:
        if len(adj[v]) >= len(adj[mx1]):
            mx1 = v
    
    for v in adj[mx1]:
        adj[v].remove(mx1)
    
    mx2 = 1 if mx1 != 1 else 2
    for v,_ in dfs:
        if v == mx1: continue
        if len(adj[v]) >= len(adj[mx2]):
            mx2 = v

    print(len(adj[mx1]) + len(adj[mx2]) - 1)


        
q = 1
q = int(input().strip())
for i in range(q):
    solve(i+1)
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 687 ms):
import io
import os
import sys


inf = 10 ** 18
mod = 10 ** 9 + 7

ONLINE_JUDGE = __debug__

if ONLINE_JUDGE:
    R = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline


    def _input() -> str:
        return R().decode()
else:
    R = sys.stdin.readline


    def _input() -> str:
        return R()


def get_int() -> int:
    return int(_input())


def get_str() -> str:
    return _input().strip()


def get_int_list() -> list[int]:
    return list(map(int, _input().strip().split()))


def init_matrix(n_, m_, value_=0) -> list[list]:
    return [[value_] * m_ for _ in range(n_)]


def solve() -> None:
    n = get_int()

    g = init_matrix(n, 0)
    cnt = [0] * n
    pairs = set()
    for i in range(n - 1):
        u, v = get_int_list()
        u -= 1
        v -= 1
        g[v].append(u)
        g[u].append(v)
        cnt[u] += 1
        cnt[v] += 1
        pairs.add((u, v))
        pairs.add((v, u))

    order = sorted(
        range(len(cnt)),
        key=lambda x: cnt[x],
        reverse=True,
    )
    u, v = order[:2]
    h = []
    for i in range(n):
        if cnt[i] == cnt[v]:
            h.append(i)

    if cnt[u] != cnt[v]:
        for w in h:
            if (u, w) not in pairs:
                print(cnt[u] + cnt[v] - 1)
                return

        print(cnt[u] + cnt[v] - 2)
    else:
        for i in range(len(h)):
            for j in range(i + 1, len(h)):
                if (h[i], h[j]) not in pairs:
                    print(cnt[u] + cnt[v] - 1)
                    return

        print(cnt[u] + cnt[v] - 2)


for _ in range(get_int()):
    solve()


Solution 2 (runtime: 718 ms):
import sys, math, collections, bisect, heapq, functools, itertools
sys.setrecursionlimit(10 ** 5)
inp = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 20
MX = 10 ** 5 + 10
orda = ord('a')
fmax = lambda x, y: (x if x > y else y)
fmin = lambda x, y: (x if x < y else y)

def solve():
    N = int(inp())
    degree = [0 for _ in range(N + 1)]
    adj = [[] for _ in range(N + 1)]

    for _ in range(N - 1):
        u, v = list(map(int, inp().split()))
        adj[u].append(v)
        adj[v].append(u)
        degree[u] += 1
        degree[v] += 1

    tmp = []

    for node in range(1, N + 1):
        tmp.append((degree[node], node))

    tmp.sort(reverse=True)
    mx = tmp[0][0]
    best = i = 0

    def get_mx_branch(a, d, node):
        a = a[:]
        d = d[:]
        current, current_mx = d[node], 0

        d[node] = 0
        for u in a[node]:
            d[u] -= 1

        for i in range(1, N + 1):
            current_mx = fmax(current_mx, d[i])

        return current + current_mx - 1

    while i < N and tmp[i][0] == mx:
        if i > 1: break
        best = fmax(best, get_mx_branch(adj, degree, tmp[i][1]))
        i += 1

    return best
    
def main():
    for _ in range(int(inp())):
        print(solve())

if __name__ == '__main__':
    main()
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 687 ms):
import io
import os
import sys


inf = 10 ** 18
mod = 10 ** 9 + 7

ONLINE_JUDGE = __debug__

if ONLINE_JUDGE:
    R = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline


    def _input() -> str:
        return R().decode()
else:
    R = sys.stdin.readline


    def _input() -> str:
        return R()


def get_int() -> int:
    return int(_input())


def get_str() -> str:
    return _input().strip()


def get_int_list() -> list[int]:
    return list(map(int, _input().strip().split()))


def init_matrix(n_, m_, value_=0) -> list[list]:
    return [[value_] * m_ for _ in range(n_)]


def solve() -> None:
    n = get_int()

    g = init_matrix(n, 0)
    cnt = [0] * n
    pairs = set()
    for i in range(n - 1):
        u, v = get_int_list()
        u -= 1
        v -= 1
        g[v].append(u)
        g[u].append(v)
        cnt[u] += 1
        cnt[v] += 1
        pairs.add((u, v))
        pairs.add((v, u))

    order = sorted(
        range(len(cnt)),
        key=lambda x: cnt[x],
        reverse=True,
    )
    u, v = order[:2]
    h = []
    for i in range(n):
        if cnt[i] == cnt[v]:
            h.append(i)

    if cnt[u] != cnt[v]:
        for w in h:
            if (u, w) not in pairs:
                print(cnt[u] + cnt[v] - 1)
                return

        print(cnt[u] + cnt[v] - 2)
    else:
        for i in range(len(h)):
            for j in range(i + 1, len(h)):
                if (h[i], h[j]) not in pairs:
                    print(cnt[u] + cnt[v] - 1)
                    return

        print(cnt[u] + cnt[v] - 2)


for _ in range(get_int()):
    solve()


Solution 2 (runtime: 1374 ms):
from collections import defaultdict

t = int(input())

def find_max_edges(graph):
    max_us = []
    max_n = -1
    for u in graph:
        if len(graph[u]) > max_n:
            max_us, max_n = [u], len(graph[u])
        elif len(graph[u]) == max_n:
            max_us.append(u)
    return max_us, max_n

for _ in range(t):
    g = defaultdict(list)
    g_lengths = defaultdict(int)
    n = int(input())
    for i in range(n - 1):
        u, v = input().split(' ')
        g[u].append(v)
        g[v].append(u)
        g_lengths[u] += 1
        g_lengths[v] += 1

    us, n = find_max_edges(g)
    max_2nd_edges = 0
    for u in us:
        u_adj = set(g[u])
        for node in g_lengths:
            if node == u:
                continue
            max_2nd_edges = max(g_lengths[node] - (1 if node in u_adj else 0), max_2nd_edges)
        if max_2nd_edges == n:
            break

    components = 1
    components += n - 1
    components += max_2nd_edges - 1
    print(components)
Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 687 ms):
import io
import os
import sys


inf = 10 ** 18
mod = 10 ** 9 + 7

ONLINE_JUDGE = __debug__

if ONLINE_JUDGE:
    R = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline


    def _input() -> str:
        return R().decode()
else:
    R = sys.stdin.readline


    def _input() -> str:
        return R()


def get_int() -> int:
    return int(_input())


def get_str() -> str:
    return _input().strip()


def get_int_list() -> list[int]:
    return list(map(int, _input().strip().split()))


def init_matrix(n_, m_, value_=0) -> list[list]:
    return [[value_] * m_ for _ in range(n_)]


def solve() -> None:
    n = get_int()

    g = init_matrix(n, 0)
    cnt = [0] * n
    pairs = set()
    for i in range(n - 1):
        u, v = get_int_list()
        u -= 1
        v -= 1
        g[v].append(u)
        g[u].append(v)
        cnt[u] += 1
        cnt[v] += 1
        pairs.add((u, v))
        pairs.add((v, u))

    order = sorted(
        range(len(cnt)),
        key=lambda x: cnt[x],
        reverse=True,
    )
    u, v = order[:2]
    h = []
    for i in range(n):
        if cnt[i] == cnt[v]:
            h.append(i)

    if cnt[u] != cnt[v]:
        for w in h:
            if (u, w) not in pairs:
                print(cnt[u] + cnt[v] - 1)
                return

        print(cnt[u] + cnt[v] - 2)
    else:
        for i in range(len(h)):
            for j in range(i + 1, len(h)):
                if (h[i], h[j]) not in pairs:
                    print(cnt[u] + cnt[v] - 1)
                    return

        print(cnt[u] + cnt[v] - 2)


for _ in range(get_int()):
    solve()


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