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

Code 1:
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;




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

    vector<vector<int>> adj(n+1, vector<int>());
    FOR(i, 1, n - 1){
        int u,v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }

    multiset<int> s;
    FOR(i, 1, n){
        s.insert(sz(adj[i]));
    }

    int answer = 0;
    FOR(x, 1, n){
        s.erase(s.find(sz(adj[x])));
        for(int v : adj[x]){
            answer = max(answer, sz(adj[x]) + sz(adj[v]) - 2);
            s.erase(s.find(sz(adj[v])));
        }

        if(s.size() > 0)
            answer = max(answer, sz(adj[x]) + (*(--(s.end()))) - 1);

        for(int v : adj[x]){
            s.insert(sz(adj[v]));
        }
        s.insert(sz(adj[x]));
    }

    cout << answer << "\n";

    return;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

    int t = 1; cin >> t;
    while(t--) solve();
    return 0;
}


Code 2:
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;




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

    vector<vector<int>> adj(n + 1, vector<int>());
    FOR(i, 1, n - 1){
        int u,v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }

    vector<int> f(n + 1, -1e9);

    int answer = 0;

    auto dfs = [&](auto&& dfs, int x, int p) -> void{
        for(int v : adj[x])if(v != p){
            dfs(dfs, v, x);

            f[x] = max(f[x], max(sz(adj[v]) - 1, f[v]));
            answer = max(answer, sz(adj[x]) + sz(adj[v]) - 2);
            answer = max(answer, sz(adj[x]) + f[v]);
        }
    };

    dfs(dfs, 1, -1);

    cout << answer << "\n";
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

    int t = 1; cin >> t;
    while(t--) solve();
    return 0;
}


Answer: 1