Problem: 2063C - Remove Exactly Two
Language: java

Solution 1 (runtime: 1968 ms):
import java.util.*;

public class RemoveExactlyTwo
{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int t = in.nextInt();

        while (t-- > 0) {
            int n = in.nextInt();


            ArrayList<Integer>[] arr = new ArrayList[n];

            HashSet<Integer>[] s = new HashSet[n];

            for (int i = 0; i < n-1; i++) {
                int x = in.nextInt()-1;
                int y = in.nextInt()-1;

                if(arr[x] == null) {arr[x] = new ArrayList<>(); s[x] = new HashSet<>();}
                if(arr[y] == null) {arr[y] = new ArrayList<>(); s[y] = new HashSet<>();}

                arr[x].add(y); arr[y].add(x);
                s[x].add(y); s[y].add(x);
            }

            Comparator<int[]> comparator = (a, b) -> Integer.compare(b[0], a[0]);

            PriorityQueue<int[]> pq = new PriorityQueue<>(comparator);

//            pq.add(new int[]{n,0});

            HashSet<Integer> vis = new HashSet<>();

            int ans = dfs(pq, 0, vis, s, arr);

            System.out.println(ans);

        }



    }

    public static int dfs(PriorityQueue<int[]> pq, int m, HashSet<Integer> vis, HashSet<Integer>[] s, ArrayList<Integer>[] arr)
    {
        vis.add(m);
        int v = s[m].size();

        int max = 0;

        if(pq.isEmpty()){
            max = 0;
        }
        else if(s[m].contains(pq.peek()[1])) {
            int[] a = pq.poll();
            max = v + a[0] - 2;
            if(!pq.isEmpty()) max = Math.max( v + a[0] - 2, v + pq.peek()[0] - 1);
            pq.add(a);
        }
        else if(!s[m].contains(pq.peek()[1]))
        {
            max = Math.max(max, v + pq.peek()[0] - 1);
        }

        pq.add(new int[]{v, m});

        for (int i = 0; i < arr[m].size(); i++) {
            if(vis.contains(arr[m].get(i))) continue;
            max = Math.max(max, dfs(pq,arr[m].get(i), vis, s, arr));
        }

        return max;

    }



    /*          ─────────▄▄───────────────────▄▄──
                ──────────▀█───────────────────▀█─
                ──────────▄█───────────────────▄█─
                ──█████████▀───────────█████████▀─
                ───▄██████▄─────────────▄██████▄──
                ─▄██▀────▀██▄─────────▄██▀────▀██▄
                ─██────────██─────────██────────██
                ─██───██───██─────────██───██───██
                ─██────────██─────────██────────██
                ──██▄────▄██───────────██▄────▄██─
                ───▀██████▀─────────────▀██████▀──
                ──────────────────────────────────
                   ────────────────────────────
                    ─────────────────────────
                     ──────█████████████───
                     ──────────────────────
                     ──────────────────────
     */
}


Solution 2 (runtime: 1843 ms):
// https://codeforces.com/problemset/problem/2063/C

import java.util.*;

public class Main
{
    private static void dfs_dp (int v, int p, final ArrayList<Integer>[] tree, int[] dp)
    {
        for (int to : tree[v])
        if (to != p)
        {
            dfs_dp(to, v, tree, dp);

            dp[v] = Math.max(dp[v], Math.max(dp[to], tree[to].size()));
        }
    }

    private static int dfs_ans (int v, int p, final ArrayList<Integer>[] tree, final int[] dp)
    {
        int ans = 0;

        for (int to : tree[v])
        if (to != p)
            ans = Math.max(ans, dfs_ans(to, v, tree, dp));

        for (int to : tree[v])
        if (to != p)
            ans = Math.max(ans, tree[v].size()-1 + Math.max(tree[to].size() - 1, dp[to]));
        
        int maxi = -1;

        for (int to : tree[v])
        if (to != p)
        {
            int cur = Math.max(tree[to].size(), dp[to]);

            if (maxi != -1)
                ans = Math.max(ans, maxi + cur - 1);
            
            maxi = Math.max(maxi, cur);
        }

        return ans;
    }
    
    private static void solve (Scanner in)
    {
        int n = in.nextInt();

        ArrayList<Integer>[] tree = new ArrayList[n];

        for (int i = 0; i < n; i++)
            tree[i] = new ArrayList<Integer>();

        for (int i = 1; i < n; i++)
        {
            int u = in.nextInt() - 1;
            int v = in.nextInt() - 1;

            tree[u].add(v);
            tree[v].add(u);
        }

        int[] dp = new int[n];

        for (int i = 0; i < n; i++)
            dp[i] = 0;

        dfs_dp(0, -1, tree, dp);
        
        int ans = dfs_ans(0, -1, tree, dp);

        System.out.println(ans);
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        int t = in.nextInt();

        for (int test = 0; test < t; test++)
            solve(in);

        in.close();
    }
}