Problem: 2063C - Remove Exactly Two
Language: java

Solution 1 (runtime: 1515 ms):
import java.util.*;
import java.lang.*;
import java.io.*;
public class Codechef
    {
	static FastScanner fs = null;
	static int res;
	static ArrayList<Set<Integer>> list;
	static int co[];
	static Map<Integer,Integer> map;
	static TreeSet<Integer> ts;
	static int inf = (int)1e8;
	static boolean vis[];
	public static void main(String[] args) {
		fs = new FastScanner();

		PrintWriter out = new PrintWriter(System.out);
		int t = fs.nextInt();
        while(t-->0){
            list = new ArrayList<>();
            int n = fs.nextInt();
            co = new int[n+1];
            ts = new TreeSet<>();
            map = new HashMap<>();
            vis = new boolean[n+1];
            Arrays.fill(vis,false);
            for(int i = 0;i<=n;i++){
                Set<Integer> se = new HashSet<>();
                list.add(se);
            }
            for(int i = 1;i<n;i++){
                int u = fs.nextInt();
                int v = fs.nextInt();
                list.get(u).add(v);
                list.get(v).add(u);
                co[u]+=1;
                co[v]+=1;
            }
            for(int i = 1;i<=n;i++){
                int num = co[i];
                ts.add(num);
                if(map.get(num)==null){
                    map.put(num,1);
                }
                else{
                    map.replace(num,1+map.get(num));
                }
            }
            res = 0;
            //System.out.println("---------");
            dfs(1,-1);
            //out.println(res+" ------ ");
            dfs2(1);
           // res = Math.min(res,n-2);
            out.println(res);
        }
		out.close();
	}
	static void dfs2(int node){
	    vis[node] = true;
	    int ans1 = co[node];
	        map.replace(ans1,-1+map.get(ans1));
	        if(map.get(ans1)==0){
	            ts.remove(ans1);
	        }
	    for(Integer p:list.get(node)){
	        int nu = co[p];
	        //System.out.println(p+" -- "+node+" -- "+nu+" --- "+map.get(nu));
	        
	        map.replace(nu,-1+map.get(nu));
	        if(map.get(nu)==0){
	            ts.remove(nu);
	        }
	    }
	    int re = ans1;
	    if(ts.floor(inf)!=null){
	        re+=ts.floor(inf);
	    }
	   // else{
	   //     re-=1;
	   // }
	   
	   
	   map.replace(ans1,+1+map.get(ans1));
	        ts.add(ans1);
	    for(Integer p:list.get(node)){
	        int nu = co[p];
	        map.replace(nu,+1+map.get(nu));
	        ts.add(nu);
	    }
	    //System.out.println(node+" --- "+re+" -- "+ans1);
	    res = Math.max(res,re-1);
	    for(Integer p:list.get(node)){
	        if(!vis[p]){
	            dfs2(p);
	        }
	    }
	     
	}
	static int dfs(int node,int par){
	    for(Integer p:list.get(node)){
	        if(p==par)
	        continue;
	        dfs(p,node);
	        if(par!=-1){
	            res = Math.max(res,co[node]+co[par]-2); 
	        }
	    }
	    
	    //System.out.println(d1+" -- "+d2+" --- "+node+" -- "+par+" -- "+sz);
	    return 0;
	}
	static void sort(int[] a) {
		ArrayList<Integer> l=new ArrayList<>();
		for (int i:a) l.add(i);
		Collections.sort(l);
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
	}
}

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

        }
    }
}