Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll mod = 1e9 + 7;
const ll MX = 2e5 + 5;
inline void norm(ll &a)
{
    a %= mod;
    (a < 0) && (a += mod);
} // positive mod value
inline ll modAdd(ll a, ll b)
{
    a %= mod, b %= mod;
    norm(a), norm(b);
    return (a + b) % mod;
} // modular addition
inline ll modSub(ll a, ll b)
{
    a %= mod, b %= mod;
    norm(a), norm(b);
    return (a - b) % mod;
} // modular subtraction
inline ll modMul(ll a, ll b)
{
    a %= mod, b %= mod;
    norm(a), norm(b);
    return (a * b) % mod;
} // modular multiplication
inline ll bigMod(ll b, ll p)
{
    ll r = 1;
    while (p)
    {
        if (p & 1LL)
            r = modMul(r, b);
        b = modMul(b, b);
        p >>= 1LL;
    }
    return r;
}
inline ll modInverse(ll a) { return bigMod(a, mod - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

void dfs(ll node, vector<vector<ll>> &adj, vector<ll> &vis)
{
    vis[node] = 1;
    for(auto child: adj[node])
    {
        if(!vis[child])
            dfs(child, adj, vis);
    }
}

void solve()
{
    ll n;
    cin >> n;
    vector<vector<ll>> adj(n+1, vector<ll>());
    vector<ll> vis(n+1, 0); 

    map<ll, ll> mp;
    vector<ll> u(n), v(n);
    for (int i = 0; i < n - 1; i++)
    {
        cin >> u[i] >> v[i];
        mp[u[i]]++, mp[v[i]]++;
    }

    vector<pair<ll, ll>> vp;
    for (int i = 1; i <= n; i++)
        vp.push_back({mp[i], i});
    sort(vp.rbegin(), vp.rend());

    // for(auto i: vp) cout << i.first << ' ' << i.second <<endl;
    ll c1 = vp[0].second;
    // cout << c1 << endl;
    mp.clear();

    for (int i = 0; i < n - 1; i++)
    {
        if (u[i] == c1 or v[i] == c1)
            continue;
        mp[u[i]]++;
        mp[v[i]]++;
    }

    vp.clear();
    for (int i = 1; i <= n; i++)
    {
        if (i == c1)
            continue;
        vp.push_back({mp[i], i});
    }

    sort(vp.rbegin(), vp.rend());
    // for(auto i: vp) cout << i.first << ' ' << i.second << endl;
    ll c2 = vp[0].second;
    // cout << c2 << endl;

    for (int i = 0; i < n - 1; i++)
    {
        if (u[i] == c1 or u[i] == c2 or v[i] == c1 or v[i] == c2)
            continue;
        adj[u[i]].push_back(v[i]); 
        adj[v[i]].push_back(u[i]); 
    }

    ll ans = 0; 
    for(int i=1;i<=n; i++)
    {
        if(i==c1 or i==c2) continue;
        if(!vis[i])
        {
            dfs(i, adj, vis); 
            ans++; 
        }

    }
    cout << ans << endl; 
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll tc = 1;
    cin >> tc;
    for (ll t = 1; t <= tc; t++)
    {
        solve();
    }
    return 0;
}

Submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll mod=1e9+7;
const ll MX=2e5+5;
inline void norm(ll &a) {a%=mod; (a<0) && (a+=mod) ;}                            //positive mod value
inline ll modAdd(ll a,ll b) {a%=mod, b%=mod; norm(a),norm(b); return (a+b)%mod;} //modular addition
inline ll modSub(ll a,ll b) {a%=mod, b%=mod; norm(a),norm(b); return (a-b)%mod;} //modular subtraction
inline ll modMul(ll a,ll b) {a%=mod, b%=mod; norm(a),norm(b); return (a*b)%mod;} //modular multiplication
inline ll bigMod(ll b,ll p)  {ll r=1; while(p) {if(p & 1LL) r=modMul(r,b) ;b=modMul(b,b) ; p>>=1LL ; } return r; }
inline ll modInverse(ll a) {return bigMod(a,mod-2); }
inline ll modDiv(ll a ,ll b) { return modMul(a,modInverse(b)) ;}

void solve()
{
    ll n; 
    cin >> n; 
    vector<vector<ll>> adj(n+1, vector<ll> ()); 

    vector<ll> deg(n+1, 0); 
    for(int i=1;i<n; i++)
    {
        ll u, v; 
        cin >> u >> v; 
        adj[u].push_back(v); 
        adj[v].push_back(u);
        deg[u]++, deg[v]++; 
    }

    for(int i=1;i<=n; i++) sort(adj[i].begin(), adj[i].end());
    ll ans = 0; 
    for(int i=1;i<=n; i++)
    {
        for(auto child: adj[i])
        {
            ans = max(ans, deg[i] + deg[child] - 2); 
        }
    }

    vector<pair<ll,ll>> vp; 
    for(int i=1;i<=n; i++)
    {
        vp.push_back({deg[i], i}); 
    }

    sort(vp.rbegin(), vp.rend());

    for(int i=1;i<=n; i++)
    {
        for(int j=i+1;j<=n; j++)
        {
            ll u = vp[i-1].second, v = vp[j-1].second;
            if(binary_search(adj[u].begin(), adj[u].end(), v)) continue;
            ans = max(ans, vp[i-1].first + vp[j-1].first-1);
        }
    }

    cout << ans << endl; 
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll tc = 1;
    cin >> tc;
    for (ll t = 1; t <= tc; t++)
    {
        solve();
    }
    return 0;
}