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

Previous submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
#include<unordered_map>	
using namespace std;
#define ll long long int
#define vi vector<ll> 
#define all(v) v.begin() , v.end()
#define srt(v) sort(v.begin(), v.end())
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define co(c) cout<<c<<"\n"
#define rn return 
#define acc(v) accumulate(v.begin() , v.end() , 0)


template<class T, class U> istream& operator>>(istream &is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T> istream& operator>>(istream& is, vector<T>& vec) { for(auto &x : vec) cin >> x; return is; }
template<class T, class U> ostream& operator<<(ostream &os, const pair<T, U>& p) { os << "<" << p.first << "," << p.second << ">"; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& vec) { for(auto x : vec) os << x << " "; return os; }

void solve(){
  int n;
  cin >> n;

  vi adj[n+1];
  vi out(n+1 , 0);

  for(int i= 0 ; i < n -1 ; i++){
     int a, b;
     cin >> a >> b;
     
     adj[a].push_back(b);
     adj[b].push_back(a);

     out[a]++;
     out[b]++;
  }
  int ans = 1;
  int ind = max_element(out.begin(), out.end()) - out.begin();
  
  int d = out[ind];
  out[ind] = -1;
  
  ans += (d -1);
  //set<int> connected;
  int cnt = 0;
  for(auto t: adj[ind]){
      if( d == out[t]){
           cnt++;
      }
      out[t]--;  
      
  }
  if(cnt >= 2){
      co((2*d - 1));
      rn;
  } 
  
  
  

  ind = max_element(out.begin(), out.end() ) - out.begin();
  if(out[ind] == d)
  {
       co((2*d - 1)) ;
         rn;
  }
  d = out[ind];
  
  ans += (d - 1);

  co(ans);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int testcases;
    cin >> testcases;

    //testcases = 1;
    while (testcases--) {
       solve();
        return 0;

    }}



Submission (verdict: Accepted):
#include<bits/stdc++.h>
#include<unordered_map>	
using namespace std;
#define ll long long int
#define vi vector<ll> 
#define all(v) v.begin() , v.end()
#define srt(v) sort(v.begin(), v.end())
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define co(c) cout<<c<<"\n"
#define rn return 
#define acc(v) accumulate(v.begin() , v.end() , 0)


template<class T, class U> istream& operator>>(istream &is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T> istream& operator>>(istream& is, vector<T>& vec) { for(auto &x : vec) cin >> x; return is; }
template<class T, class U> ostream& operator<<(ostream &os, const pair<T, U>& p) { os << "<" << p.first << "," << p.second << ">"; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& vec) { for(auto x : vec) os << x << " "; return os; }

void solve(){
  int n;
  cin >> n;

  vi adj[n+1];
  vi out(n+1 , 0);

  for(int i= 0 ; i < n -1 ; i++){
     int a, b;
     cin >> a >> b;
     
     adj[a].push_back(b);
     adj[b].push_back(a);

     out[a]++;
     out[b]++;
  }
  int ans = 1;
  int ind = max_element(out.begin(), out.end()) - out.begin();
  
  int d = out[ind];
  out[ind] = -1;
  
  ans += (d -1);
  //set<int> connected;
  int cnt = 0;
  for(auto t: adj[ind]){
      if( d == out[t]){
           cnt++;
      }
      out[t]--;  
      
  }
  if(cnt >= 2){
      co((2*d - 1));
      rn;
  } 
  
  
  

  ind = max_element(out.begin(), out.end() ) - out.begin();
  if(out[ind] == d)
  {
       co((2*d - 1)) ;
         rn;
  }
  d = out[ind];
  
  ans += (d - 1);

  co(ans);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int testcases;
    cin >> testcases;

    //testcases = 1;
    while (testcases--) {
       solve();
        //return 0;

    }
  return 0;
}