Problem: 2063C - Remove Exactly Two
Language: java

Solution 1 (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);

        }
    }
}


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