Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace __gnu_pbds;
using namespace std;
//pbds
// X.find_by_order(k) return kth element. 0 indexed.
// X.order_of_key(k) returns count of elements strictly less than k.
 
#define ll                         long long
#define vi                          vector<int>
#define vvi                         vector<vector<int>> 
#define f(n) for(ll i=0;i<n;i++)
#define f1(v) for(auto r:v) cout<<r<<" "
#define nl cout<<endl
#define rep(i,a,b)                  for(int i = a; i < b; i++)
#define repr(i,a,b)                 for(int i=a;i>=b;i--)
#define F                          first
#define S                          second
#define maxv(a) *max_element(a.begin(),a.end())
#define minv(a) *min_element(a.begin(),a.end())
#define acc accumulate(a.begin(),a.end())
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define zero cout<<"0"<<endl
#define minus cout<<"-1"<<endl
#define si                          set<int>
#define sc                          set<char>
#define out return
#define mii                         map<int,int> 
#define mci                         map<char,int>
#define pb                          push_back
#define pp                          pop_back
#define msi                         map<string, int>
#define mis                         map<int, string>
#define in                          insert
// #define mm                         1000000007
#define pi                          3.14159265358979323846
#define prec(x)                     cout << fixed << setprecision(x)
#define bpc(x)                      __builtin_popcountll(x)
#define ull                         unsigned long long
#define sv(v) sort(v.begin(),v.end())
#define sv1(v) sort(v.begin(),v.end(),greater<ll>())
#define svp(v) sort(v.begin(),v.end(),greater<pair<ll,ll>>())
#define all(x)                      x.begin(), x.end()
#define read(a, n)                  rep(i, 0, n)    cin >> a[i]
#define print(a, n)                 rep(i, 0, n)    cout << a[i] << " "; cout << endl
vector<int>prime;
set<int>prime_set;
void fastio()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}




void solve(){
 ll n;
 cin>>n;
 
 ll x,y;
// map<ll,vector<ll>> m;
 vector<vector<ll>> v(n);
 f(n-1){
   cin>>x>>y;
  // m[x-1].pb(y-1);
  // m[y-1].pb(x-1);
   v[x-1].pb(y-1);
   v[y-1].pb(x-1);
 }
vector<pair<ll,ll>> v1;
ll i=0;
for(i;i<n;){
  v1.pb({v[i].size(),i});
  i++;
}
svp(v1);
// for(auto x:v1){
//   cout<<x.first<<" "<<x.second<<endl;
// }
x=v1[0].second;
// cout<<x<<" ";
ll s=0;
s+=v[x].size()-1;
// cout<<s<<" ";
v[x].clear();
for (int i = 0; i < n; i++) {
        for(auto y=v[i].begin();y!=v[i].end();y++){
          if(*y==x) {v[i].erase(y);
          break;}
        }
    }
    
v1.clear();
i=0;
for(i;i<n;i++){
  v1.pb({v[i].size(),i});
}
svp(v1);
// nl;
// for(auto x:v1){
//   cout<<x.first<<" "<<x.second<<endl;
// }
x=v1[0].second;
// cout<<x<<" ";
s+=v[x].size();
// cout<<s<<" ";
// if(v[x].size()==0) s++;
 


cout<<s<<endl;
 
 
 
 
 
 
 
 
 
 
 
 
                          
}
 
int32_t main(){
   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    ll t=1;
   
       cin >> t;
 
    while (t--) {
        solve();
 
    }
    return 0;
} 

Submission (verdict: Accepted):
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace __gnu_pbds;
using namespace std;
//pbds
// X.find_by_order(k) return kth element. 0 indexed.
// X.order_of_key(k) returns count of elements strictly less than k.
 
#define ll                         long long
#define vi                          vector<int>
#define vvi                         vector<vector<int>> 
#define f(n) for(ll i=0;i<n;i++)
#define f1(v) for(auto r:v) cout<<r<<" "
#define nl cout<<endl
#define rep(i,a,b)                  for(int i = a; i < b; i++)
#define repr(i,a,b)                 for(int i=a;i>=b;i--)
#define F                          first
#define S                          second
#define maxv(a) *max_element(a.begin(),a.end())
#define minv(a) *min_element(a.begin(),a.end())
#define acc accumulate(a.begin(),a.end())
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define zero cout<<"0"<<endl
#define minus cout<<"-1"<<endl
#define si                          set<int>
#define sc                          set<char>
#define out return
#define mii                         map<int,int> 
#define mci                         map<char,int>
#define pb                          push_back
#define pp                          pop_back
#define msi                         map<string, int>
#define mis                         map<int, string>
#define in                          insert
// #define mm                         1000000007
#define pi                          3.14159265358979323846
#define prec(x)                     cout << fixed << setprecision(x)
#define bpc(x)                      __builtin_popcountll(x)
#define ull                         unsigned long long
#define sv(v) sort(v.begin(),v.end())
#define sv1(v) sort(v.begin(),v.end(),greater<ll>())
#define svp(v) sort(v.begin(),v.end(),greater<pair<ll,ll>>())
#define all(x)                      x.begin(), x.end()
#define read(a, n)                  rep(i, 0, n)    cin >> a[i]
#define print(a, n)                 rep(i, 0, n)    cout << a[i] << " "; cout << endl
vector<int>prime;
set<int>prime_set;
void fastio()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}




void solve(){
 ll n;
 cin>>n;
 
 ll x,y;
// map<ll,vector<ll>> m;
 vector<vector<ll>> v(n);
 f(n-1){
   cin>>x>>y;
  // m[x-1].pb(y-1);
  // m[y-1].pb(x-1);
   v[x-1].pb(y-1);
   v[y-1].pb(x-1);
 }
vector<pair<ll,ll>> v1;
ll i=0;
for(i;i<n;){
  v1.pb({v[i].size(),i});
  i++;
}
svp(v1);
// for(auto x:v1){
//   cout<<x.first<<" "<<x.second<<endl;
// }
x=v1[0].second;
ll p=0;
for(auto y:v1){
  if(y.first==v[x].size()){
    p++;
    if(p==3) break;
  }
  else break;
  
}
if(p>=3){
  cout<<2*v[x].size()-1<<endl;
  // yes;
  out;
}
// cout<<x<<" ";
ll s=0;
s+=v[x].size()-1;
// cout<<s<<" ";
v[x].clear();
for (int i = 0; i < n; i++) {
        for(auto y=v[i].begin();y!=v[i].end();y++){
          if(*y==x) {v[i].erase(y);
          break;}
        }
    }
    
v1.clear();
i=0;
for(i;i<n;i++){
  v1.pb({v[i].size(),i});
}
svp(v1);
// nl;
// for(auto x:v1){
//   cout<<x.first<<" "<<x.second<<endl;
// }
x=v1[0].second;
// cout<<x<<" ";
s+=v[x].size();
// cout<<s<<" ";
// if(v[x].size()==0) s++;
 


cout<<s<<endl;
 
 
 
 
 
 
 
 
 
 
 
 
                          
}
 
int32_t main(){
   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    ll t=1;
   
       cin >> t;
 
    while (t--) {
        solve();
 
    }
    return 0;
}