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 ll long long int 
#define vi vector<ll>
#define pb push_back
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define endl "\n"
const ll mod = 1000000007;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//.......................................................................................................................................................................................................................................................................
 

void solve(){ 
     
 ll q;
 cin>>q;
 vector<ll>adj(q+1,0);
 for(int i=0;i<q-1;i++){
    ll x,y;
    cin>>x>>y;
    adj[x]++;
    adj[y]++; 
 }
 
 ll count=0;
 multiset<ll>st;
 for(auto it:adj){
   st.insert(it);
 }
 debug(st);
 if(st.size()<=3){
    cout<<0<<endl;
    return;
 }
 ll maxi=*st.rbegin();
 st.erase(st.find(maxi));
 ll smaxi=*st.rbegin();
 if(smaxi==1){
   cout<<maxi-1<<endl;
 }else{
   cout<<maxi+smaxi-1<<endl; 
 }
 





} 

 
 
int main() {

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("ooutput.txt", "w", stdout);
#endif
 
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0); 
ll t;
cin>>t; 
 
  
while (t--) { 
    
solve();
}

 
}


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int 
#define vi vector<ll>
#define pb push_back
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define endl "\n"
const ll mod = 1000000007;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//.......................................................................................................................................................................................................................................................................
 

void solve(){ 
     
 ll n;
 cin>>n;
 vector<vector<ll>>adj(n+1);
 for(int i=1;i<n;i++){
    ll x,y;
    cin>>x>>y;
    adj[x].push_back(y);
    adj[y].push_back(x);
 }
 ll maxi=0;
 
 for(auto it:adj){
    maxi=max(maxi,(long long)it.size());
 }
 
 for(int i=0;i<adj.size();i++){
    if(adj[i].size()==maxi){
        int x=adj[i].back();
       
        adj[i]={};
        adj[x].pop_back();
    }
 }
 
 ll smaxi=0;
  for(auto it:adj){
    smaxi=max(smaxi,(long long)it.size());
 }
   
  

if(maxi==1){
    cout<<0<<endl;
}else if(smaxi<=1){
    cout<<maxi-1<<endl;
}else{
    cout<<maxi+smaxi-1<<endl;
}

} 

 
 
int main() {

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("ooutput.txt", "w", stdout);
#endif
 
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0); 
ll t;
cin>>t; 
 
  
while (t--) { 
    
solve();
}

 
}