Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 577 ms):
#include <iostream>
#include <bits/stdc++.h>

using namespace std;
using ll = long long int ;
using pii = pair<int,int> ;

void dfs(const vector<vector<int> > &adj , vector<int> &dp , vector<int> &prev , int v , int p = -1)
{
    prev[v] = p ;

    for(int to : adj[v])
    {
        if(to != p)
        {
            dfs(adj , dp , prev , to , v) ;
            dp[v] = max(dp[v] ,
                        dp[to] ) ;
        }
    }

    dp[v] = max(dp[v] ,
                int(adj[v].size()) ) ;
}

void solve()
{
    int n ;
    cin >> n ;

    vector<vector<int> > adj(n + 1) ;
    vector<int> dp(n + 1 , 0) ;
    vector<int> prev(n + 1) ;
    for(int i = 1 ; i < n ; ++i)
    {
        int u , v ;
        cin >> u >> v ;

        adj[u].push_back(v) ;
        adj[v].push_back(u) ;
    }

    dfs(adj , dp , prev , 1 , -1) ;

    int ans = 0 ;

    for(int i = 1 ; i <= n ; ++i)
    {
        for(int to : adj[i])
        {
            if(to == prev[i])
                continue ;

            ans = max(ans ,
                      int(adj[i].size()) + int(adj[to].size()) - 2) ;

            for(int to2 : adj[to])
            {
                if(to2 == i)
                    continue ;

                ans = max(int(adj[i].size()) + dp[to2] - 1 ,
                          ans ) ;


            }
        }
    }

    for(int i = 1 ; i <= n ; ++i)
    {
        pii k = {-100000 , -100000} ;
        for(int to : adj[i])
        {
            if(to == prev[i])
                continue ;


            if(dp[to] >= k.first)
            {
                k = {dp[to] , k.first} ;
            }
            else
            {
                if(dp[to] >= k.second)
                {
                    k = {k.first , dp[to]} ;
                }
            }
        }

        ans = max(ans ,
                  k.first + k.second  - 1) ;

    }

    cout << ans << "\n" ;

}

int main()
{
    int t ;
    cin >> t ;

    while(t--)
        solve() ;

    return 0;
}


Solution 2 (runtime: 453 ms):
#include <bits/stdc++.h>
using namespace std;
#define loop(n) for (int i = 0; i < n; i++)
#define fr(i,m,n) for(int i = m; i < n; i++)
#define in cin>>
#define out cout<<
#define pb push_back
#define pp pop_back()
#define ll long long int
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl


int main(){
    int t;
    in t;
    while(t--){
        int n;
        in n;
        vector<pair<int,int>> adj;
        vector<int> deg(n+1,0);
        loop(n-1){
            int u,v;
            in u>>v;
            adj.pb({u,v});
            deg[u]++;
            deg[v]++;
        }
        int maxind = 0;
        int secmaxind = 0;

        fr(i,1,n+1){
            if(deg[maxind]<=deg[i]){
                secmaxind = maxind;
                maxind = i;
            }
            else if(deg[secmaxind]<deg[i]){
                secmaxind = i;
            }
        }
        int maxi = deg[maxind];
        int secmaxi = deg[secmaxind];

        vector<int> v1,v2;
        v1= deg;
        v2 = deg;
        // swap(v2,deg);

        for(auto it:adj){
            int x = it.first;
            int y = it.second;
            if(x==maxind) v1[y]--;
            if(y==maxind) v1[x]--;
            if(x==secmaxind) v2[y]--;
            if(y==secmaxind) v2[x]--;
        }
        v1[maxind] = -1e9;
        v2[secmaxind] = -1e9;
        out max(maxi - 1 + *max_element(v1.begin(), v1.end()), secmaxi - 1 + *max_element(v2.begin(), v2.end())) << '\n';

    }
    return 0;
}