Problem: 2063C - Remove Exactly Two
Language: Java 21

Previous submission (verdict: Wrong answer on test 2):
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;
        int cur = 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)
            cur = Math.max(cur, tree[v].size() - 1 + Math.max(tree[to].size() - 1, dp[to]));

        return Math.max(ans, cur);
    }
    
    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];

        dfs_dp(0, -1, tree, dp);
        
        System.out.println(dfs_ans(0, -1, tree, dp));
    }

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

Submission (verdict: Wrong answer on test 2):
// 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;
        int cur = 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)
            cur = Math.max(cur, Math.max(tree[to].size() - 1, dp[to]));
        
        cur += tree[v].size() - 1;

        return Math.max(ans, cur);
    }
    
    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();
    }
}