Problem: 2063C - Remove Exactly Two
Language: java

Solution 1 (runtime: 1093 ms):
  
/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {

    //------------------------------------------main--------------------------------------------------//
    public static void main(String[] args) throws java.lang.Exception {
        OutputStream outputStream = System.out;
        PrintWriter out = new PrintWriter(outputStream);
        FastReader sc = new FastReader();
        Task s = new Task(sc, out);
        s.solve();
        out.close();
        // your code goes here"
    }
}

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

    public int[] readIntArray(int n) throws IOException {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }

    public long[] readLongArray(int n) throws IOException {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }
}

class Task {

    PrintWriter out;
    FastReader sc;

    Task(FastReader sc, PrintWriter out) {
        this.sc = sc;
        this.out = out;
    }
    //----------------------------------------------inputfunction------------------------------------------//

    String st() {
        return sc.nextLine();
    }

    int i() {
        return sc.nextInt();
    }

    long l() {
        return sc.nextLong();
    }

    double d() {
        return sc.nextDouble();
    }

    //---------------------------------------------absolute----------------------------------------------//
    public long abs(long a) {
        return a < 0 ? -a : a;
    }

    public int abs(int a) {
        return a < 0 ? -a : a;
    }

    //-------------------------------------------max--------------------------------------------------//
    public int max(int a, int b) {
        return a >= b ? a : b;
    }

    public long max(long a, long b) {
        return a >= b ? a : b;
    }

    public int max(int a, int b, int c) {
        return Math.max(a, Math.max(b, c));
    }

    public long max(long a, long b, long c) {
        return Math.max(a, Math.max(b, c));
    }

    //------------------------------------------min---------------------------------------------------//
    public int min(int a, int b) {
        return a <= b ? a : b;
    }

    public long min(long a, long b) {
        return a <= b ? a : b;
    }

    public int min(int a, int b, int c) {
        return Math.min(a, Math.min(b, c));
    }

    public long min(long a, long b, long c) {
        return Math.min(a, Math.min(b, c));
    }

    //----------------------------------------sortingfunction---------------------------------------------//
    private void sort(int[] arr) {
        int n = arr.length;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void sort(char[] arr) {
        int n = arr.length;
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(char[] arr) {
        int n = arr.length;
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(int[] arr) {
        int n = arr.length;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void sort(long[] arr) {
        int n = arr.length;
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(long[] arr) {
        int n = arr.length;
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }
    //---------------------------------------power----------------------------------------------------//

    public long pow(long a, int b) {
        long ans = 1L;
        while (b > 0) {
            if ((b & 1) > 0) {
                ans *= a;
            }
            a *= a;
            b >>= 1;
        }
        return b;
    }
    //----------------------------------------binary_exponentiation-----------------------------------//

    public long exp(long a, long b, long mod) {
        long ans = 1L;
        while (b > 0) {
            if ((b & 1) > 0) {
                ans = (ans * a) % mod;
            }
            a = (a * a) % mod;
            b >>= 1;
        }
        return ans;
    }
    int t;
    public void solve() throws IOException {
        t = sc.nextInt();
        while (t-- > 0) {
            int n = i();
            List<Set<Integer>> a = new ArrayList<>();
            int[][] in = new int[n+1][2];
            for(int i =0;i<=n;i++){
                a.add(new HashSet<>());
                in[i][1] = i;
            }
            for(int i = 1;i<n;i++){
                int x = i(), y = i();
                a.get(x).add(y);
                a.get(y).add(x);
                in[x][0]++; in[y][0]++;
            }
            Arrays.sort(in, (x, y) -> y[0] - x[0]);
            int ans = 0;
            for(int i = 1;i<=n;i++){
                for(int e: a.get(i)){
                    ans = max(ans, a.get(i).size() + a.get(e).size() - 2);
                }
            }
            for(int i = 0;i<n;i++){
                for(int j = i+1;j<n;j++){
                    if(a.get(in[i][1]).contains(in[j][1])) continue;
                    ans = max(ans, in[i][0] + in[j][0] - 1);
                    break;
                }
            }
            out.print(ans);
            out.print("\n");
        }
    }
}


Solution 2 (runtime: 1421 ms):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RemoveExactlyTwo {

    public static int[][] dfs(int src, int par, List<List<Integer>> arr) {

        int[][] new_components = new int[3][2];

        int first_max = 0, second_max = 0;

        for (int v : arr.get(src)) {
            if (par == v) continue;

            int[][] components = dfs(v, src, arr);

            new_components[0][0] = Math.max(new_components[0][0], components[0][0]);
            new_components[0][1] = Math.max(new_components[0][1], components[0][1]);

            new_components[1][0] = Math.max(new_components[1][0], Math.max(components[1][0], components[1][1]));
            new_components[1][1] = Math.max(new_components[1][1], components[0][0] + arr.get(src).size());
            new_components[1][1] = Math.max(new_components[1][1], components[0][1] + arr.get(src).size() - 1);
            
            new_components[2][0] = Math.max(new_components[2][0], Math.max(components[2][0], components[2][1]));
            new_components[2][1] = Math.max(new_components[2][1], components[1][0] + arr.get(src).size());
            new_components[2][1] = Math.max(new_components[2][1], components[1][1] + arr.get(src).size() - 1);

            if (first_max < components[1][0] || first_max < components[1][1]) {
                second_max = first_max;
                first_max = Math.max(components[1][0], components[1][1]);
            } else if (second_max < components[1][0] || second_max < components[1][1]) {
                second_max = Math.max(components[1][0], components[1][1]);
            }
        }

        if (arr.get(src).isEmpty()) {

            new_components[0][0] = 1;
            new_components[0][1] = Integer.MIN_VALUE/2;

            new_components[1][0] = Integer.MIN_VALUE/2;
            new_components[1][1] = 0;

            new_components[2][0] = Integer.MIN_VALUE/2;
            new_components[2][1] = Integer.MIN_VALUE/2;
            
        } else {
            new_components[2][0] = Math.max(first_max + second_max, new_components[2][0]);
        }

        return new_components;
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t =  Integer.parseInt(br.readLine());

        while(t-- > 0) {

           int n = Integer.parseInt(br.readLine());
           
           int edges = n - 1;

           List<List<Integer>> arr = new ArrayList<>();

           for (int i = 0; i < n; i++) arr.add(new ArrayList<>());

           while (edges-- > 0) {
                int[] e = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();

                arr.get(e[0] - 1).add(e[1] - 1);
                arr.get(e[1] - 1).add(e[0] - 1);
           }

           int[][] components = dfs(0, -1, arr);

           System.out.println(Math.max(components[2][0], components[2][1]) - 1);

        }
    }
}