Problem: 2063C - Remove Exactly Two
Language: java

Solution 1 (runtime: 859 ms):
import java.util.*;
import java.io.*;
import java.math.*;

public class sub {

	static int mod = 998244353;

	public static void main(String args[]) throws IOException {
		int t = sc.nextInt();
		while(t-->0) {
			int n = sc.nextInt();
			ArrayList<ArrayList<Integer>> adj = new ArrayList();
			for(int i = 0;i<n;i++)adj.add(new ArrayList());
			int deg[] = new int[n];
			for(int i = 0;i<n-1;i++) {
				int u = sc.nextInt()-1;
				int v = sc.nextInt()-1;
				adj.get(u).add(v);
				adj.get(v).add(u);
				deg[u]++;
				deg[v]++;
			}
			TreeMap<Integer, Integer> tm = new TreeMap(Collections.reverseOrder());
			for(int i = 0;i<deg.length;i++) {
				tm.put(deg[i], tm.getOrDefault(deg[i], 0)+1);
			}
			int ans = 0;
			for(int i = 0;i<n;i++) {
				for(int j : adj.get(i)) {
					ans = Math.max(ans, deg[j]+deg[i]-2);
					tm.put(deg[j], tm.get(deg[j])-1);
					if(tm.get(deg[j])==0) {
						tm.remove(deg[j]);
					}
				}
				tm.put(deg[i], tm.get(deg[i])-1);
				if(tm.get(deg[i])==0) {
					tm.remove(deg[i]);
				}
				//out.println(pq);
				ans=Math.max(ans, deg[i]+(tm.isEmpty() ? 0: tm.firstKey())-1);
				for(int j : adj.get(i)) {
					tm.put(deg[j], tm.getOrDefault(deg[j], 0)+1);
				}
				tm.put(deg[i], tm.getOrDefault(deg[i],0)+1);
			}
			out.println(ans);
		}
		out.close();
	}

//	static long bincoef(int n, int k) {
//	    if (n < k) return 0L;
//	    return (p[n] * pow((p[n - k] * p[k]) % mod, mod - 2, mod)) % mod;
//	}
	


	static long pow(long a, long b, long mod) {
		if (b == 0) {
			return 1L;
		}
		long val = pow(a, b / 2, mod);
		if (b % 2 == 0) {
			return val * val % mod;
		}
		return val * val % mod * a % mod;
	}

	static long gcd(long a, long b) {
		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}

	static long lcm(long a, long b) {
		return a * b / gcd(a, b);
	}

	static Kattio sc = new Kattio();
	static PrintWriter out = new PrintWriter(System.out);

	static class Kattio {

		static BufferedReader r;
		static StringTokenizer st;

		public Kattio() {
			r = new BufferedReader(new InputStreamReader(System.in));
		}

		public String next() {
			try {
				while (st == null || !st.hasMoreTokens()) {
					st = new StringTokenizer(r.readLine());
				}
				return st.nextToken();
			} catch (Exception e) {
				return null;
			}
		}

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

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

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

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