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 pb push_back

#define endl "\n"
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(a) cout<<#a<<' '<<a<<endl;
#define cut cout<<endl <<"-------------------"<<endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef double db;

const int N = 2e5+10;
const ll mod=1e9 + 7, inf = 0x3f3f3f3f;
mt19937 mrand(time(0));     
int rnd(int x) { return mrand() % x;}
ll qmi(ll a,ll b,ll p) {ll res=1;a%=p; assert(b>=0); for(;b;b/=2){if(b&1)res=res*a%p;a=a*a%p;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

void init()
{
    int n;
    cin >> n;
    vector<pii> g;
    vector<int> d(n + 1, 0);
    for(int i = 1; i < n; i ++)
    {
        int l, r;
        cin >> l >> r;
        g.pb({l, r});
        d[l] ++, d[r] ++;
    }
    if(n == 2)
    {
        cout << 0 << endl;
        return;
    }
    int mx = -1, id = -1;
    for(int i = 1; i <= n; i ++)
    {
        if(d[i] > mx)
        mx = d[i], id = i;
    }
    for(int i = 1; i <= n; i ++) d[i] = 0;
    d[id] = -1;
    for(auto [l, r] : g)
    {
        if(l == id || r == id) continue;
        d[l] ++, d[r] ++;
    }
    mx = -1;
    int idd = -1;
    for(int i = 1; i <= n; i ++)
    {
        if(d[i] > mx)
        mx = d[i], idd = i;
    }
    int s = 0;
    for(auto [l, r] : g)
    {
        if(l == id || r == id) continue;
        if(l == idd || r == idd) continue;
        s ++;
    }
    // debug(s)
    if(n - 2 - s < 0)
    {
        while(1){
            
        }
    }
    cout << n - 2 - s << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin.tie(0);
    
    int _;
    cin>>_;
    // _ = 1; 
    while(_--)
    {
        init();
    }

    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

#define endl "\n"
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(a) cout<<#a<<' '<<a<<endl;
#define cut cout<<endl <<"-------------------"<<endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef double db;

const int N = 2e5+10;
const ll mod=1e9 + 7, inf = 0x3f3f3f3f;
mt19937 mrand(time(0));     
int rnd(int x) { return mrand() % x;}
ll qmi(ll a,ll b,ll p) {ll res=1;a%=p; assert(b>=0); for(;b;b/=2){if(b&1)res=res*a%p;a=a*a%p;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

void init()
{
    int n;
    cin >> n;
    vector<vector<int>> g(n + 1);
    vector<int> d(n + 1, 0);
    for(int i = 1; i < n; i ++)
    {
        int l, r;
        cin >> l >> r;
        g[l].pb(r), g[r].pb(l);
    }
    multiset<int> s;
    for(int i = 1; i <= n; i ++)
    {
        s.insert({SZ(g[i])});
    }
    int ans = 0;
    for(int i = 1; i <= n; i ++)
    {
        s.erase(s.lower_bound(SZ(g[i])));
        for(auto x : g[i])
        {
            s.erase(s.lower_bound(SZ(g[x])));
            s.insert(SZ(g[x]) - 1);
        }
        ans = max(ans, n - 2 - (n - 1 - SZ(g[i]) - *(--s.end())));
        s.insert(SZ(g[i]));
        for(auto x : g[i])
        {
            s.erase(s.lower_bound(SZ(g[x]) - 1));
            s.insert(SZ(g[x]));
        }
    }
    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin.tie(0);
    
    int _;
    cin>>_;
    // _ = 1; 
    while(_--)
    {
        init();
    }

    return 0;
}