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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define loop(i, x, y) for (int i = x; i < y; i++)
#define rloop(i, x, y) for (int i = x; i >= y; i--)
#define all(arr) arr.begin(), arr.end()
#define rt return
#define mpp map<int, int>
#define ff first
#define ss second
#define pb push_back
#define sortv(arr) sort(arr.begin(), arr.end())
#define rsortv(arr) sort(arr.begin(), arr.end(), greater<int>())
#define pii pair<int, int>
#define br cout << endl
#define dbg(a) cout << a << endl
#define ct continue
#define disp(a)                        \
  {                                    \
    for (int i = 0; i < a.size(); i++) \
      cout << a[i] << " ";             \
    cout << endl;                      \
  }
#define inp(a)                         \
  {                                    \
    for (int i = 0; i < a.size(); i++) \
      { cin >> a[i]; }                 \
  }
const int mod = 998244353;
long long mod_exp(long long base, long long exp, long long mod) {
    long long result = 1;
    while (exp > 0) {
        if (exp % 2 == 1) { // If exp is odd
            result = (result * base) % mod;
        }
        base = (base * base) % mod; // Square the base
        exp /= 2; // Halve the exponent
    }
    return result;
}
 
long long mod_inverse(long long a, long long mod) {
    return mod_exp(a, mod - 2, mod); // Using modular exponentiation
}
void dfs(int i,vi& vis,vi adj[]){
   vis[i]=1;
   for(auto it :adj[i]){
    if(!vis[it])dfs(it,vis,adj);
   }
}
void Dinesh() {
  int n;
  cin>>n;
  vi deg(n+1);
  vector<pii>edges;
  loop(i,0,n-1){
    int u,v;
    cin>>u>>v;
    deg[u]++;
    deg[v]++;
    edges.pb({u,v});
  }
  int mx1=*max_element(deg.begin(),deg.end()),idx1=max_element(deg.begin(),deg.end())-deg.begin(),val2=0;
  loop(i,1,n+1){
    if(i!=idx1 && deg[i]>val2){
      val2=deg[i];
      
    }
  }
  // vi adj[n+1];
  // for(auto it:edges){
  //   int u=it.ff,v=it.ss;
  //   if(u==mx1 || u==mx2 || v==mx1 || v==mx2){
  //     continue;
  //   }
  //   adj[u].pb(v);
  //   adj[v].pb(u);
  // }
  // cout<<mx1;br;
  // cout<<val2;br;
  // vi vis(n+1);
  // int ans=0;
  // loop(i,1,n+1){
  //   if(i!=mx1 && i!=mx2 && !vis[i]){
  //     ans++;
  //     dfs(i,vis,adj);
  //   }
  // }
  if(val2==1)dbg(mx1-1);
  else dbg(mx1+val2-1);
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        Dinesh();
    }
    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define loop(i, x, y) for (int i = x; i < y; i++)
#define rloop(i, x, y) for (int i = x; i >= y; i--)
#define all(arr) arr.begin(), arr.end()
#define rt return
#define mpp map<int, int>
#define ff first
#define ss second
#define pb push_back
#define sortv(arr) sort(arr.begin(), arr.end())
#define rsortv(arr) sort(arr.begin(), arr.end(), greater<int>())
#define pii pair<int, int>
#define br cout << endl
#define dbg(a) cout << a << endl
#define ct continue
#define disp(a)                        \
  {                                    \
    for (int i = 0; i < a.size(); i++) \
      cout << a[i] << " ";             \
    cout << endl;                      \
  }
#define inp(a)                         \
  {                                    \
    for (int i = 0; i < a.size(); i++) \
      { cin >> a[i]; }                 \
  }
const int mod = 998244353;
long long mod_exp(long long base, long long exp, long long mod) {
    long long result = 1;
    while (exp > 0) {
        if (exp % 2 == 1) { // If exp is odd
            result = (result * base) % mod;
        }
        base = (base * base) % mod; // Square the base
        exp /= 2; // Halve the exponent
    }
    return result;
}
 
long long mod_inverse(long long a, long long mod) {
    return mod_exp(a, mod - 2, mod); // Using modular exponentiation
}
void dfs(int i,vi& vis,vi adj[]){
   vis[i]=1;
   for(auto it :adj[i]){
    if(!vis[it])dfs(it,vis,adj);
   }
}
void Dinesh() {
  int n;
  cin>>n;
  vi deg(n+1);
  set<pii>edges;
  loop(i,0,n-1){
    int u,v;
    cin>>u>>v;
    deg[u]++;
    deg[v]++;
    edges.insert({u,v});
  }
  int mx1=*max_element(deg.begin(),deg.end()),idx1=max_element(deg.begin(),deg.end())-deg.begin();
  int ans=0;
  // cout<<mx1<<" "<<idx1;br;
  int idx2=0;
  loop(i,1,n+1){
    if(deg[i]==mx1 && i!=idx1){idx2=i;break;}
  }
  loop(i,1,n+1){
    if(i!=idx1 && edges.find({idx1,i})==edges.end() && edges.find({i,idx1})==edges.end()){
      ans=max(ans,mx1+deg[i]-1);
    }
    else if(i!=idx1){
      ans=max(ans,mx1+deg[i]-2);
    }

  }
  if(idx2!=0)loop(i,1,n+1){
    if(i!=idx2 && edges.find({idx2,i})==edges.end() && edges.find({i,idx2})==edges.end()){
      ans=max(ans,mx1+deg[i]-1);
    }
    else if(i!=idx1){
      ans=max(ans,mx1+deg[i]-2);
    }

  }
  dbg(ans);
  
  
 
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        Dinesh();
    }
    return 0;
}