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

Previous submission (verdict: Wrong answer on test 2):

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class C {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(
                    new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                if (st.hasMoreTokens()) {
                    str = st.nextToken("\n");
                } else {
                    str = br.readLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    public static void main(String[] args) {
        FastReader sc = new FastReader();
        int t = sc.nextInt();
        while (t != 0) {
            t--;
            int n = sc.nextInt();
            ArrayList<Integer>[] graph=new ArrayList[n];
            for (int i = 0; i < n; i++) {
                graph[i]=new ArrayList<>();
            }

            for (int i = 1; i <=n-1 ; i++) {
                int u= sc.nextInt();
                int v= sc.nextInt();
                u--;
                v--;
                graph[u].add(v);
                graph[v].add(u);
            }

            long[] noe=new long[n];
            for (int i = 0; i < n; i++) {
                noe[i]=graph[i].size();
            }
//            System.out.println(Arrays.toString(noe));
            long mev=0,me=noe[0];
            for (int i = 1; i < n; i++) {
                if (me<noe[i]){
                    mev=i;
                    me=noe[i];
                }
            }
//            System.out.println(mev+" "+me);

            ArrayList<Integer> snoe=new ArrayList<>();
            for (int i = 0; i < n; i++) {
//                System.out.println(graph[i]);
                if (i!=mev){
                    if (!graph[i].contains((int) mev)){
                        snoe.add(graph[i].size());
                    }
                    else {
                        snoe.add(graph[i].size()-1);
                    }
                }
            }
//          System.out.println(snoe);
            Collections.sort(snoe);
            if (n>2) {
                if (snoe.size() > 0) {
                    System.out.println((me - 1) + snoe.getLast());
                } else {
                    System.out.println(me - 1);
                }
            }
            else {
                System.out.println(0);
            }
        }
    }
}


Submission (verdict: Time limit exceeded on test 6):

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class C {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(
                    new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                if (st.hasMoreTokens()) {
                    str = st.nextToken("\n");
                } else {
                    str = br.readLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    public  static class Pair{
        int vtx;
        long deg;
        Pair(int vtx,long deg){
            this.vtx=vtx;
            this.deg=deg;
        }

        public long getDeg() {
            return this.deg;
        }
    }
    public static void main(String[] args) {
        FastReader sc = new FastReader();
        int t = sc.nextInt();
        while (t != 0) {
            t--;
            int n = sc.nextInt();
            ArrayList<Integer>[] graph=new ArrayList[n+1];
            for (int i = 0; i <= n; i++) {
                graph[i]=new ArrayList<>();
            }

            for (int i = 1; i <=n-1 ; i++) {
                int u= sc.nextInt();
                int v= sc.nextInt();
                graph[u].add(v);
                graph[v].add(u);
            }
            Pair[] d=new Pair[n];
            for (int i = 0; i <n ; i++) {
                d[i]=new Pair(i+1,graph[i+1].size());
            }
            long ans=Long.MIN_VALUE;
//            long c=0;
            for (int i=1;i<=n;i++){
                for (int nbr:graph[i]){
                    ans=Math.max(ans,graph[i].size()+graph[nbr].size()-2);
//                    ++c;
                }
            }
//            System.out.println(ans);
            Arrays.sort(d, Comparator.comparing(Pair::getDeg).reversed());
//            for (Pair p:d) {
////                System.out.println(p.vtx+" "+p.deg);
//            }
            for (int i = 0; i <n ; i++) {
                int src=d[i].vtx;
//                System.out.print(src+" ");
                for (int j = 0; j < n; j++) {
                    int nbr=d[j].vtx;
//                    System.out.print(nbr);
                    if (j!=i && !graph[src].contains(nbr)){
//                        System.out.println(src+" "+nbr);
                        ans=Math.max(ans,graph[src].size()+graph[nbr].size()-1);
                        break;
                    }
                }
//                System.out.println();
            }

            System.out.println(ans);

        }
    }
}