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

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

ll dfs(ll x, vector<set<ll>> &adj, vector<ll> &vis) {
    vis[x]++;
    ll ans = 0;
    for(ll i: adj[x]) {
        if(adj[i].size() == 0 or vis[i]) {
            continue;
        } 
        ans = dfs(i, adj, vis);
    }
    return 1 + ans;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        vector<set<ll>> adj(n);
        for(ll i = 0; i < n - 1; i++) {
            ll u, v;
            cin >> u >> v;
            u--;
            v--;
            adj[u].insert(v);
            adj[v].insert(u);
        }
        if(n == 2) {
            cout << "0\n";
            continue;
        }
        ll mn = adj[0].size();
        ll mnV = 0;
        for(ll i = 1; i < n; i++) {
            if(mn > adj[i].size()) {
                mn = adj[i].size();
                mnV = i;
            }
        }
        for(ll i: adj[mnV]) {
            adj[i].erase(mnV);
        }
        adj[mnV].clear();
        mn = n;
        mnV = -1;

        for(ll i = 0; i < n; i++) {
            if(adj[i].size() == 0) {
                continue;
            }
            if(mn > adj[i].size()) {
                mn = adj[i].size();
                mnV = i;
            }
        }
        for(ll i: adj[mnV]) {
            adj[i].erase(mnV);
        }
        adj[mnV].clear();

        vector<ll> vis(n);
        ll ans = 0;
        for(ll i = 0; i < n; i++) {
            if(adj[i].size() == 0 or vis[i]) {
                continue;
            }
            ans = max(ans, dfs(i, adj, vis));
        }
        cout << ans << '\n';
    }
    return 0;
}

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

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        vector<set<ll>> adj(n);
        vector<ll> deg(n);
        for(ll i = 0; i < n - 1; i++) {
            ll u, v;
            cin >> u >> v;
            u--;
            v--;
            adj[u].insert(v);
            adj[v].insert(u);
            deg[u]++;
            deg[v]++;
        }
        vector<pair<ll, ll>> e;
        for(ll i = 0; i < n; i++) {
            e.push_back({deg[i], i});
        }
        sort(e.rbegin(), e.rend());
        ll ans = 0;
        for(ll i = 0; i < n; i++) {
            for(ll j = i + 1; j < n; j++) {
                ll u = e[i].second;
                ll v = e[j].second;
                if(adj[u].count(v)) {
                    continue;
                }
                ans = max(ans, deg[u] + deg[v] - 1);
            }
        }
        for(ll i = 0; i < n; i++) {
            for(ll j: adj[i]) {
                ans = max(ans, deg[i] + deg[j] - 2);
            }
        }
        cout << ans << '\n';
    }
    return 0;
}