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

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

int const N = 100005;

signed main()
{
    int t = 1;
    cin>>t;
    while(t--)
    {
        int n ; cin >> n;
        vector<int>v[n+2];
        int b[n+2] = {0};
        int ans = 0;
        
        for(int i = 1 ; i < n ; i++)
        {
            int x, y;
            cin >> x >> y;
            v[x].pb(y);
            v[y].pb(x);
            b[x] += 1;
            b[y] += 1;
        }
        
        vector<pair<int, int>>a;
        for(int i = 1 ; i <= n ; i++)
        {
            int sz = v[i].size();
            a.pb({sz, i});
        }
        
        int mx = 0;
        int node = 0;
        for(int i =1 ; i <= n ; i++) {
            if(b[i] >= mx)  {
                mx = b[i];
                node = i;
            }
        }
        
        for(int i = 0 ; i< v[node].size(); i++) {
            b[v[node][i]] -= 1; 
        }
        b[node] = -1;
        
        // cout << node << " " << mx << "\n";
        
        ans += mx;
        mx = 0;
        for(int i = 1 ; i <= n ; i++) {
            if(b[i] > mx)  {
                mx = b[i];
                node = i;
            }
        }
        
        // cout << node << " " << mx << "\n";
        
        ans += mx;
        
        
        ans -= 1;
        cout << ans << "\n";
        
    }
}





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

int const N = 100005;

signed main()
{
    int t = 1;
    cin>>t;
    while(t--)
    {
        int n ; cin >> n;
        vector<int>v[n+2];
        int b[n+2] = {0};
        int ans = 0;
        
        for(int i = 1 ; i < n ; i++)
        {
            int x, y;
            cin >> x >> y;
            v[x].pb(y);
            v[y].pb(x);
            b[x] += 1;
            b[y] += 1;
        }
        
        // vector<pair<int, int>>a;
        for(int i = 1 ; i <= n ; i++)
        {
            int sz = v[i].size();
            // a.pb({sz, i});
        }
        
        int mx = 0;
        // int node = 0;
        for(int i =1 ; i <= n ; i++) {
            if(b[i] >= mx)  {
                mx = b[i];
                // node = i;
            }
        }
        vector<int>a;
        for(int i =1 ; i <= n ; i++) {
            if(b[i] == mx) a.pb(i);
        }
        
        // for(int i = 0 ; i < a.size(); i++) cout << a[i] << " "; cout << "\n";
        
        
        for(int j = 0 ; j < a.size(); j ++) {
        
            int node = a[j];
            // cout << node << " -------\n";
            for(int i = 0 ; i< v[node].size(); i++) {
                b[v[node][i]] -= 1; 
            }
            b[node] = -1;
            
            // cout << "---: ";
            // for(int i = 1 ; i <= n ; i++) cout << b[i] << " "; cout << "\n";
            
            // cout << node << " " << mx << "\n";
        
            // ans += mx;
            int local_mx = 0;
            for(int i = 1 ; i <= n ; i++) {
                if(b[i] > local_mx)  {
                    local_mx = b[i];
                    // node = i;
                }
            }
            
            for(int i = 0 ; i< v[node].size(); i++) {
                b[v[node][i]] += 1; 
            }
            b[node] = mx;
            
            // cout << "--- ";
            // for(int i = 1 ; i <= n ; i++) cout << b[i] << " "; cout << "\n";
            
            ans = max(ans, mx + local_mx);
            // cout << mx << " " << local_mx << endl;
        }
        
        // cout << node << " " << mx << "\n";
        
        // ans += mx;
        
        
        ans -= 1;
        cout << ans << "\n";
        
    }
}