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;
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")

#define pb push_back                    
#define ll long long
//#define sort(all(v)) sort(v.begin(),v.end())

int mod = 998244353;
const int N = 4e5 + 10;
const int inf = 1e9;
int fact[200001];
ll binpow(ll a, ll b){
 if(b == 0) return 1;
 else if(b % 2 == 1) return (a * binpow(a, b - 1)) % mod;
 ll p = binpow(a,b / 2);
 return (p * p) % mod;
}
vector <int> g[N];
int sum[N],was[N];
void dfs(int u){
    was[u] = 1;
    if(sum[u] == 0 && u != 1) sum[u]++;
    for(int to : g[u]){
        if(was[to] != 0) continue;
        sum[u]++;
        dfs(to);
    }
}
signed main(){
    //freopen("mootube.in", "r", stdin);
    //freopen("mootube.out", "w", stdout);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i = 1; i < n; i++){
            int u,v;
            cin >> u >> v;
            g[u].pb(v);
            g[v].pb(u);
        }
        dfs(1);
        int mx = 0;
        multiset<int> mt;
        for(int i = 1; i <= n; i++){
            mx = max(mx,sum[i]);  
            mt.insert(sum[i]);
        }
        int mx1 = 0,v = 0,ans = 1;
        for(int i = 1; i <= n; i++){
            if(sum[i] == mx){
                for(int to : g[i]){
                    mt.erase(mt.find(sum[to]));
                    mt.insert(sum[to] - 1);
                }
                if(mx1 < *mt.rbegin() || (mx1 == *mt.rbegin() && v == 1)){
                    mx1 = *mt.rbegin();
                    v = i;
                }
                for(int to : g[i]){
                    mt.erase(mt.find(sum[to] - 1));
                    mt.insert(sum[to]);
                }
            }
        }
        ans += mx - 1;
        for(int to : g[v]){
            sum[to]--;
        }
        mx = 0;
        for(int i = 1; i <= n; i++){
            if(v == i) continue;
            mx = max(mx,sum[i]);   
        }
        cout << ans + mx - 1 << '\n';
        for(int i = 1; i <= n; i++){
            sum[i] = was[i] = 0;
            g[i].clear();
        }
    }
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")

#define pb push_back                    
#define ll long long
//#define sort(all(v)) sort(v.begin(),v.end())

int mod = 998244353;
const int N = 4e5 + 10;
const int inf = 1e9;
int fact[200001];
ll binpow(ll a, ll b){
 if(b == 0) return 1;
 else if(b % 2 == 1) return (a * binpow(a, b - 1)) % mod;
 ll p = binpow(a,b / 2);
 return (p * p) % mod;
}
vector <int> g[N];
int sum[N],was[N];
void dfs(int u){
    was[u] = 1;
    if(sum[u] == 0 && u != 1) sum[u]++;
    for(int to : g[u]){
        if(was[to] != 0) continue;
        sum[u]++;
        dfs(to);
    }
}
signed main(){
    //freopen("mootube.in", "r", stdin);
    //freopen("mootube.out", "w", stdout);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i = 1; i < n; i++){
            int u,v;
            cin >> u >> v;
            g[u].pb(v);
            g[v].pb(u);
        }
        dfs(1);
        multiset<int> mt;
        for(int i = 1; i <= n; i++)mt.insert(sum[i]);
        int ans = 0;
        for(int i = 1; i <= n; i++){
            mt.erase(mt.find(sum[i]));
            for(int to : g[i]){
                mt.erase(mt.find(sum[to]));
                mt.insert(sum[to] - 1);
            }
            ans = max(ans,*mt.rbegin() + sum[i] - 1);
            for(int to : g[i]){
                mt.erase(mt.find(sum[to] - 1));
                mt.insert(sum[to]);
            }
            mt.insert(sum[i]);
        }
        cout << ans << '\n';
        for(int i = 1; i <= n; i++){
            sum[i] = was[i] = 0;
            g[i].clear();
        }
    }
}