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: 1140 ms):

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();
            TreeSet<Integer>[] graph=new TreeSet[n+1];
            for (int i = 0; i <= n; i++) {
                graph[i]=new TreeSet<>();
            }

            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 = i+1; 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);

        }
    }
}