Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

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

#define fr(i,s,e) for(ll i=s ; i<e ; i++)
#define rf(i,s,e) for(ll i=s ; i>=e ; i--)
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
typedef vector<ll> vll;

ll MOD=998244353;

ll fastpow(ll base , ll power){
	ll ans=1;
	while(power>0){
		if(power&1){
			ans*=base;
			ans%=MOD;
		}
		power>>=1;
		base*=base;
		base%=MOD;
	}
	return ans;
}

void solve(){
	ll n;
	cin >> n;
	vector<vll> v(n+1);
	vll used(n+1);
	fr(i, 1, n+1) v[i].pb(i);
	fr(i, 0, n-1){
		ll a,b;
		cin >> a >> b;
		v[a].pb(b);
		v[b].pb(a);
	}
	vector<vll> v1(v.begin(), v.end());
	sort(v.begin(), v.end(), [](vll a, vll b){return a.size() < b.size();});
	fr(i, 1, v[n].size()){
		v1[v[n][i]].erase( find(v1[v[n][i]].begin(), v1[v[n][i]].end(), v[n][0]) );
	}
	v1.erase(v1.begin() + v[n][0]);
	vector<vll> v2(v1.begin(), v1.end());
	sort(v1.begin(), v1.end(), [](vll a, vll b){return a.size() < b.size();});
	fr(i, 1, v1[n-1].size()){
		fr(j, 1, v2.size()){
			if(v2[j][0] == v1[n-1][i]) v2[j].erase( find(v2[j].begin(), v2[j].end(), v1[n-1][0]) );
		}
	}
	fr(j, 1, v2.size()){
		if(v2[j][0] == v1[n-1][0]) v2.erase( v2.begin() + j);
	}
	if(v2.size()==1){cout << "0\n"; return;}
	ll cnt = 0;
	fr(i, 1, v2.size()){
		if( !used[v2[i][0]] ){
			cnt++;
			for(auto x : v2[i]) used[x] = 1;;
		}
	}
	cout << cnt << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	ll tt=1;
	cin >> tt;
	while(tt--){
		solve();
	}
}

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

#define fr(i,s,e) for(ll i=s ; i<e ; i++)
#define rf(i,s,e) for(ll i=s ; i>=e ; i--)
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
typedef vector<ll> vll;

ll MOD=998244353;

ll fastpow(ll base , ll power){
	ll ans=1;
	while(power>0){
		if(power&1){
			ans*=base;
			ans%=MOD;
		}
		power>>=1;
		base*=base;
		base%=MOD;
	}
	return ans;
}

void solve(){
	ll n;
	cin >> n;
	vector<vll> v(n+1);
	vll used(n+1);
	fr(i, 1, n+1) v[i].pb(i);
	fr(i, 0, n-1){
		ll a,b;
		cin >> a >> b;
		v[a].pb(b);
		v[b].pb(a);
	}
	vector<vll> v1(v.begin(), v.end());
	sort(v.begin()+1, v.end(), [](vll a, vll b){return a.size() < b.size();});
	fr(i, 1, v[n].size()){
		v1[v[n][i]].erase( find(v1[v[n][i]].begin(), v1[v[n][i]].end(), v[n][0]) );
	}
	v1[v[n][0]].erase(v1[v[n][0]].begin(),v1[v[n][0]].end());
	vector<vll> v2(v1.begin(), v1.end());
	sort(v1.begin()+1, v1.end(), [](vll a, vll b){return a.size() < b.size();});
	fr(i, 1, v1[n].size()){
		v2[v1[n][i]].erase( find(v2[v1[n][i]].begin(), v2[v1[n][i]].end(), v1[n][0]) );
	}
	v2[v1[n][0]].erase(v2[v1[n][0]].begin(),v2[v1[n][0]].end());
	ll c = 0;
	fr(i, 1, n) if(v2[i].size() != 0) c++; 
	if(c==0){cout << "0\n"; return;}
	ll cnt = 0;
	stack<ll> s;
	fr(i, 1, n+1){
		if(v2[i].empty()) continue;
		if( !used[v2[i][0]] ){
			s.push(v2[i][0]);
			cnt++;
			while(!s.empty()){
				ll j = s.top();
				s.pop();
				used[j]=1;
				for(auto x:v2[j]){
					if(!used[x]) s.push(x); 
					used[x] = 1;
				}
			}
		}
	}
	cout << cnt << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	ll tt=1;
	cin >> tt;
	while(tt--){
		solve();
	}
}