Problem: 2063C - Remove Exactly Two
Language: java

Solution 1 (runtime: 1968 ms):
import java.util.*;

public class RemoveExactlyTwo
{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int t = in.nextInt();

        while (t-- > 0) {
            int n = in.nextInt();


            ArrayList<Integer>[] arr = new ArrayList[n];

            HashSet<Integer>[] s = new HashSet[n];

            for (int i = 0; i < n-1; i++) {
                int x = in.nextInt()-1;
                int y = in.nextInt()-1;

                if(arr[x] == null) {arr[x] = new ArrayList<>(); s[x] = new HashSet<>();}
                if(arr[y] == null) {arr[y] = new ArrayList<>(); s[y] = new HashSet<>();}

                arr[x].add(y); arr[y].add(x);
                s[x].add(y); s[y].add(x);
            }

            Comparator<int[]> comparator = (a, b) -> Integer.compare(b[0], a[0]);

            PriorityQueue<int[]> pq = new PriorityQueue<>(comparator);

//            pq.add(new int[]{n,0});

            HashSet<Integer> vis = new HashSet<>();

            int ans = dfs(pq, 0, vis, s, arr);

            System.out.println(ans);

        }



    }

    public static int dfs(PriorityQueue<int[]> pq, int m, HashSet<Integer> vis, HashSet<Integer>[] s, ArrayList<Integer>[] arr)
    {
        vis.add(m);
        int v = s[m].size();

        int max = 0;

        if(pq.isEmpty()){
            max = 0;
        }
        else if(s[m].contains(pq.peek()[1])) {
            int[] a = pq.poll();
            max = v + a[0] - 2;
            if(!pq.isEmpty()) max = Math.max( v + a[0] - 2, v + pq.peek()[0] - 1);
            pq.add(a);
        }
        else if(!s[m].contains(pq.peek()[1]))
        {
            max = Math.max(max, v + pq.peek()[0] - 1);
        }

        pq.add(new int[]{v, m});

        for (int i = 0; i < arr[m].size(); i++) {
            if(vis.contains(arr[m].get(i))) continue;
            max = Math.max(max, dfs(pq,arr[m].get(i), vis, s, arr));
        }

        return max;

    }



    /*          ─────────▄▄───────────────────▄▄──
                ──────────▀█───────────────────▀█─
                ──────────▄█───────────────────▄█─
                ──█████████▀───────────█████████▀─
                ───▄██████▄─────────────▄██████▄──
                ─▄██▀────▀██▄─────────▄██▀────▀██▄
                ─██────────██─────────██────────██
                ─██───██───██─────────██───██───██
                ─██────────██─────────██────────██
                ──██▄────▄██───────────██▄────▄██─
                ───▀██████▀─────────────▀██████▀──
                ──────────────────────────────────
                   ────────────────────────────
                    ─────────────────────────
                     ──────█████████████───
                     ──────────────────────
                     ──────────────────────
     */
}


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

        }
    }
}