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: 968 ms):
import java.io.*;
import java.util.*;




public class HelpLancelot {
    


    static FastReader in = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static long mod = 1_000_000_007;
    
    static class Pair{
        int a, b;
        Pair(int a, int b){ this.a =a; this.b = b;}
    }
    
    static int[] isPerm(int a[]){
        int n = a.length;
        int pres[] = new int[n+1];
        Arrays.fill(pres, -1);
        pres[0] = 1;
        for(int i=0; i<n; i++) pres[a[i]] = i;
        
        for(int i=1; i<=n; i++) {
            if(pres[i] == -1) {
                pres[0] = -1;
                break;
            }
        }
        
        return pres;
    }

    public static void main(String[] args) {

        int t = in.nextInt();
        while (t-- > 0) {
            int n = in.nextInt();
            ArrayList<HashSet<Integer>> adj = new ArrayList<>();
            for(int i=0; i<n; i++){
                adj.add(new HashSet<>());
            }
            
            int deg[] = new int[n];
            for(int i=1; i<=n-1; i++){
                int u = in.nextInt(); int v = in.nextInt();
                u--; v--;
                deg[u]++; deg[v]++;
                adj.get(v).add(u);
                adj.get(u).add(v);
            }
            
            ArrayList<Pair> pair = new ArrayList<>();
            for(int i=0; i<n; i++) pair.add(new Pair(i, deg[i]));
            
            Collections.sort(pair, (p,q) -> q.b - p.b);
            
            if(n == 2) out.println(0);
            else {
                Pair pair1 = pair.get(0);
                Pair pair2 = pair.get(1);
                
                
                int ans = 0;
                if(adj.get(pair1.a).contains(pair2.a)) ans = Math.max(ans, pair1.b + pair2.b - 2);
                else ans = Math.max(ans, pair1.b + pair2.b - 1);
                
                for(int i=2; i<n; i++){
                    Pair pair3 = pair.get(i);
                    if(adj.get(pair3.a).contains(pair2.a)) ans = Math.max(ans, pair3.b + pair2.b - 2);
                    else ans = Math.max(ans, pair3.b + pair2.b - 1);
                    
                    if(adj.get(pair1.a).contains(pair3.a)) ans = Math.max(ans, pair1.b + pair3.b - 2);
                    else ans = Math.max(ans, pair1.b + pair3.b - 1);
                }
                out.println(ans);
            }
            
            
        }
        out.close();
    }

    static void inArr(int a[], int s, int e) {
        for (int i = s; i <= e; i++) {
            a[i] = in.nextInt();
        }
    }

    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 {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}