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 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){
        
        for(auto it:adj[i]){
            adj[it].pop_back();
        }
       
        adj[i]={};
         
    }
 }
 
 ll smaxi=0;
  for(auto it:adj){
    smaxi=max(smaxi,(long long)it.size());
 }
 debug(maxi,smaxi);
   
  

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();
}

 
}

Submission (verdict: Accepted):
#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;
 vi d(n+1);
 set<pair<ll,ll>>edges;

 for(int i=1;i<n;i++){
    ll x,y;
    cin>>x>>y;
    d[x]++;
    d[y]++;
    edges.insert({x,y});
     edges.insert({y,x});
 }
 
ll maxi=0;
vector<pair<ll,ll>>v;
for(int i=1;i<=n;i++){
    v.push_back({d[i],i});
}
sort(allr(v));

for(int i=0;i<v.size();i++){
  for(int j=i+1;j<v.size();j++){
    if(edges.find({v[i].second,v[j].second})==edges.end()){
        maxi=max(maxi,v[i].first+v[j].first-1);
        break;
    }
  }
}

for(auto it:edges){
    maxi=max(maxi,d[it.first]+d[it.second]-2);
}
cout<<maxi<<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();
}

 
}