Problem: 2063C - Remove Exactly Two
Language: java

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

public class RemoveExactlyTwo {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);

        int tc = Integer.parseInt(br.readLine());

        while (tc-- > 0) {
            int N = Integer.parseInt(br.readLine());

            // if (N <)

            ArrayList<Integer>[] adj = new ArrayList[N];
            HashMap<Integer, Integer> degCnt = new HashMap<>();

            for (int i = 0; i < N; i++) {
                adj[i] = new ArrayList<>(); 
            }

            for (int i = 0; i < N-1; i++) {
                String s = br.readLine();
                StringTokenizer st1 = new StringTokenizer(s);

                int a = Integer.parseInt(st1.nextToken()) - 1;
                int b = Integer.parseInt(st1.nextToken()) - 1;

                adj[a].add(b);
                adj[b].add(a);
            }

            for (int i = 0; i < N; i++) {
                int deg = adj[i].size();
                degCnt.put(i, deg);
            }
            int res = 1;
            ArrayList<Integer> maxKeys = getBiggestKeys(degCnt);

            if (maxKeys.size() == 1) {
                res += degCnt.get(maxKeys.get(0)) - 1;
                degCnt.remove(maxKeys.get(0));

                for (int n : adj[maxKeys.get(0)]) {
                    degCnt.put(n, degCnt.get(n)-1);
                }

                res += degCnt.get(getBiggestKeys(degCnt).get(0)) - 1;
            } else {
                int max = degCnt.get(maxKeys.get(0));
                boolean done = false;
                for (int key : maxKeys) {
                    ArrayList<Integer> affected = adj[key];
                    int affectedCount = 0;
                    for (int v : affected) {
                        if (degCnt.get(v) == max) {
                            affectedCount++;
                        }
                    }

                    if (maxKeys.size() - affectedCount > 1) {
                        res = max * 2 - 1;
                        done = true;
                        break;
                    }
                }

                if (!done) {
                    res = max*2-2;
                }
            }

            pw.println(res);
        }

        pw.close();
    }

    static ArrayList<Integer> getBiggestKeys(HashMap<Integer, Integer> hm) {
        ArrayList<Integer> maxKeys = new ArrayList<>();
        for (Map.Entry<Integer, Integer> entry : hm.entrySet()) {
            int key = entry.getKey();
            int val = entry.getValue();

            if (maxKeys.size() == 0 || val >= hm.get(maxKeys.get(0))) {
                if (maxKeys.size() == 0 || val > hm.get(maxKeys.get(0))) {
                    maxKeys.clear();
                    maxKeys.add(key);
                } else {
                    maxKeys.add(key);
                }
            }
        }
        return maxKeys;
    }
}

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();
    }
}