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


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