Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define N    1000010
#define int  long long
#define i128 __int128
#define ull  unsigned long long
#define db   long double
#define pii  pair<int,int>
#define st   first
#define ed   second
#define mkp  make_pair
#define pb   push_back
#define eps  1e-9
#define mod  998244353
#define mod2 1000000007
#define bs   13131
#define bs2  131
#define INF  0x3f3f3f3f3f3f3f3f
#define il   inline
#define vi   vector<int>
#define ins  insert
#define umap unordered_map
#define uset unordered_set
#define R(x) x.begin(),x.end()
#define B(x) x.begin()
#define E(x) x.end()
#define lb   lower_bound
#define ub   upper_bound
#define prq  priority_queue
#define rd(l,r) (rnd()%(r-l+1)+l)
mt19937_64 rnd(chrono::system_clock::now().time_since_epoch().count());
il int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
int T=1,n,m,q,k;
int ans;
int d[N];
int fl;
vector<int> v[N];
il void work(int id){
	int p=1;
	for(int i=2;i<=n;i++)
		if(d[i]>d[p])p=i;
	ans+=d[p]-1;
	int cnt=0;
	for(auto x:v[p])cnt+=(d[x]==d[p]),d[x]--;
	if(id==1&&cnt>1){
		printf("%lld\n",d[p]*2-1);
		fl=1;
	}
	d[p]=-INF;
}
il void solve(){
	n=read();
	for(int i=1;i<=n;i++)v[i].clear(),d[i]=0;
	for(int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].pb(y),v[y].pb(x);
		d[x]++,d[y]++;
	}
	ans=1;
	fl=0;
	work(1);
	if(fl){
		return ;
	}
	work(1);
	printf("%lld\n",ans);
}
signed main(){
	T=read();
	while(T--)solve();
	return 0;
}





Submission (verdict: Accepted):
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define N    1000010
#define int  long long
#define i128 __int128
#define ull  unsigned long long
#define db   long double
#define pii  pair<int,int>
#define st   first
#define ed   second
#define mkp  make_pair
#define pb   push_back
#define eps  1e-9
#define mod  998244353
#define mod2 1000000007
#define bs   13131
#define bs2  131
#define INF  0x3f3f3f3f3f3f3f3f
#define il   inline
#define vi   vector<int>
#define ins  insert
#define umap unordered_map
#define uset unordered_set
#define R(x) x.begin(),x.end()
#define B(x) x.begin()
#define E(x) x.end()
#define lb   lower_bound
#define ub   upper_bound
#define prq  priority_queue
#define rd(l,r) (rnd()%(r-l+1)+l)
mt19937_64 rnd(chrono::system_clock::now().time_since_epoch().count());
il int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
int T=1,n,m,q,k;
int ans;
int d[N];
int fl;
vector<int> v[N];
il void work(int id){
	int p=1;
	for(int i=2;i<=n;i++)
		if(d[i]>d[p])p=i;
	ans+=d[p]-1;
	int cnt=0;
	for(auto x:v[p])cnt+=(d[x]==d[p]),d[x]--;
	if(id==1&&cnt>1){
		printf("%lld\n",d[p]*2-1);
		fl=1;
	}
	d[p]=-INF;
}
il void solve(){
	n=read();
	for(int i=1;i<=n;i++)v[i].clear(),d[i]=0;
	for(int i=1;i<n;i++){
		int x=read(),y=read();
		v[x].pb(y),v[y].pb(x);
		d[x]++,d[y]++;
	}
	ans=1;
	fl=0;
	work(1);
	if(fl){
		return ;
	}
	work(2);
	printf("%lld\n",ans);
}
signed main(){
	T=read();
	while(T--)solve();
	return 0;
}