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 MAX 200005
struct node{
	int neicnt,id;
	vector <int> neis;
	bool operator < (const node &a) const{
		if(neicnt>a.neicnt)return 1;
		else return 0;
	}
};
node nodes[MAX];
node empt;
int senodes[MAX],neinodes[MAX];
int main(){
	int t;
	scanf("%d",&t);
	for(int tt=0;tt<t;tt++){
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			nodes[i]=empt;
			nodes[i].id=i;
		}
		for(int i=1;i<n;i++){
			int a,b;
			scanf("%d %d",&a,&b);
			nodes[a].neicnt++;
			nodes[a].neis.push_back(b);
			nodes[b].neicnt++;
			nodes[b].neis.push_back(a);
		}
		/*cout<<"nodes:"<<endl;
		for(int i=1;i<=n;i++){
			cout<<"node id:"<<nodes[i].id<<" ";
			cout<<"neicnt:"<<nodes[i].neicnt<<endl;
			cout<<"neis:";
			for(int j=0;j<nodes[i].neicnt;j++){
				cout<<nodes[i].neis[j]<<" ";
			}cout<<endl;
		}*/
		sort(nodes+1,nodes+n+1);
		/*cout<<"nodes:"<<endl;
		for(int i=1;i<=n;i++){
			cout<<"node id:"<<nodes[i].id<<" ";
			cout<<"neicnt:"<<nodes[i].neicnt<<endl;
			cout<<"neis:";
			for(int j=0;j<nodes[i].neicnt;j++){
				cout<<nodes[i].neis[j]<<" ";
			}cout<<endl;
		}*/
		int a=nodes[1].neicnt,b=nodes[2].neicnt,c=nodes[3].neicnt;
		int flag=0;
		for(int i=0;i<nodes[1].neicnt;i++){
			if(nodes[1].neis[i]==nodes[2].id){
				flag=1;break;
			}
		}
		//cout<<"flag:"<<flag<<endl;
		int ans=a+b-1;
		if(a==b&&b==c){
			ans=a+b-1;
		}else if(a==b&&b!=c){
			if(flag)ans--;
		}else if(a!=b){
			int now=2,cnt=0;
			while(nodes[now].neicnt==b){
				senodes[cnt++]=nodes[now].id;
				now++;
			}
			//cout<<"cnt:"<<cnt<<endl;
			int ncnt=nodes[1].neicnt;
			if(ncnt<cnt){
				ans=a+b-1;
				printf("%d\n",ans);return 0;
			}
			for(int i=0;i<ncnt;i++){
				neinodes[i]=nodes[1].neis[i];
			}
			sort(senodes,senodes+cnt);
			sort(neinodes,neinodes+ncnt);
			flag=1;int flag2=1;
			now=0;
			for(int i=0;i<cnt;i++){
				flag=1;
				for(int j=now;j<ncnt;j++){
					if(senodes[i]==neinodes[j]){
						flag=0;break;
					}
				}
				if(flag){
					flag2=0;break;
				}
			}
			if(flag2==0){
				ans=a+b-1;
			}else{
				ans=a+b-2;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
#define MAX 200005
struct node{
	int neicnt,id;
	vector <int> neis;
	bool operator < (const node &a) const{
		if(neicnt>a.neicnt)return 1;
		else return 0;
	}
};
node nodes[MAX];
node empt;
int senodes[MAX],neinodes[MAX];
int main(){
	int t;
	scanf("%d",&t);
	for(int tt=0;tt<t;tt++){
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			nodes[i]=empt;
			nodes[i].id=i;
		}
		for(int i=1;i<n;i++){
			int a,b;
			scanf("%d %d",&a,&b);
			nodes[a].neicnt++;
			nodes[a].neis.push_back(b);
			nodes[b].neicnt++;
			nodes[b].neis.push_back(a);
		}
		/*cout<<"nodes:"<<endl;
		for(int i=1;i<=n;i++){
			cout<<"node id:"<<nodes[i].id<<" ";
			cout<<"neicnt:"<<nodes[i].neicnt<<endl;
			cout<<"neis:";
			for(int j=0;j<nodes[i].neicnt;j++){
				cout<<nodes[i].neis[j]<<" ";
			}cout<<endl;
		}*/
		sort(nodes+1,nodes+n+1);
		/*cout<<"nodes:"<<endl;
		for(int i=1;i<=n;i++){
			cout<<"node id:"<<nodes[i].id<<" ";
			cout<<"neicnt:"<<nodes[i].neicnt<<endl;
			cout<<"neis:";
			for(int j=0;j<nodes[i].neicnt;j++){
				cout<<nodes[i].neis[j]<<" ";
			}cout<<endl;
		}*/
		int a=nodes[1].neicnt,b=nodes[2].neicnt,c=nodes[3].neicnt;
		int flag=0;
		for(int i=0;i<nodes[1].neicnt;i++){
			if(nodes[1].neis[i]==nodes[2].id){
				flag=1;break;
			}
		}
		//cout<<"flag:"<<flag<<endl;
		int ans=a+b-1;
		if(a==b&&b==c){
			ans=a+b-1;
		}else if(a==b&&b!=c){
			if(flag)ans--;
		}else if(a!=b){
			int flag2=1,id=nodes[1].id;
			int now=2;
			while(nodes[now].neicnt==b){
				flag=1;
				for(int i=0;i<b;i++){
					if(nodes[now].neis[i]==id){
						flag=0;break;
					}
				}
				if(flag){
					flag2=0;break;
				}
				now++;
			}
			if(flag2){
				ans=a+b-2;
			}else{
				ans=a+b-1;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}