Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef long double ld;
typedef vector<vector<ll>> VI;
ll mod = 1e9 + 7;
const ll INF = 5e18;

vi inputVector(int n)
{
  vi vec(n);
  for (int i = 0; i < n; ++i)
  {
    cin >> vec[i];
  }
  return vec;
}
vi inputVector2(int n)
{
  vi vec(n + 1);
  for (int i = 1; i <= n; ++i)
  {
    cin >> vec[i];
  }
  return vec;
}
VI input2D(ll n, ll m)
{
  VI vec(n, vi(m));
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < m; j++)
    {
      cin >> vec[i][j];
    }
  }
  return vec;
}
ll power(ll x, ll y)
{
  ll res = 1;
  while (y > 0)
  {
    if (y % 2 == 1)
      res = ((res % mod) * (x % mod)) % mod;

    y = y >> 1;
    x = ((x % mod) * (x % mod)) % mod;
  }
  return res % mod;
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t;
  cin >> t;
  while (t--)
  {
    ll n;
    cin >> n;
    VI adj(n + 1);
    vi in(n + 1, 0);
    for (ll i = 1; i < n; i++)
    {
      ll x, y;
      cin >> x >> y;
      adj[x].push_back(y);
      adj[y].push_back(x);
      in[x]++;
      in[y]++;
    }
    ll ans = 0, node = 0;
    for (ll i = 1; i <= n; i++)
    {
      if (in[i] > in[node])
      {
        node = i;
      }
    }
    ans += in[node];
    in[node] = 0;
    for (auto it : adj[node])
    {
      in[it]--;
    }
    ans--;
    node = 0;
    for (ll i = 1; i <= n; i++)
    {
      if (in[i] > in[node])
      {
        node = i;
      }
    }
    ans += in[node];
    cout << ans << endl;
  }
}


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef long double ld;
typedef vector<vector<ll>> VI;
ll mod = 1e9 + 7;
const ll INF = 5e18;

vi inputVector(int n)
{
  vi vec(n);
  for (int i = 0; i < n; ++i)
  {
    cin >> vec[i];
  }
  return vec;
}
vi inputVector2(int n)
{
  vi vec(n + 1);
  for (int i = 1; i <= n; ++i)
  {
    cin >> vec[i];
  }
  return vec;
}
VI input2D(ll n, ll m)
{
  VI vec(n, vi(m));
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < m; j++)
    {
      cin >> vec[i][j];
    }
  }
  return vec;
}
ll power(ll x, ll y)
{
  ll res = 1;
  while (y > 0)
  {
    if (y % 2 == 1)
      res = ((res % mod) * (x % mod)) % mod;

    y = y >> 1;
    x = ((x % mod) * (x % mod)) % mod;
  }
  return res % mod;
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t;
  cin >> t;
  while (t--)
  {
    ll n;
    cin >> n;
    VI adj(n + 1);
    vi in(n + 1, 0);
    for (ll i = 1; i < n; i++)
    {
      ll x, y;
      cin >> x >> y;
      adj[x].push_back(y);
      adj[y].push_back(x);
      in[x]++;
      in[y]++;
    }
    ll ans = 1, node = 0;
    for (ll i = 1; i <= n; i++)
    {
      if (in[i] > in[node])
      {
        node = i;
      }
    }
    ans += in[node] - 1;
    in[node] = 0;
    for (auto it : adj[node])
    {
      in[it]--;
    }
    node = 0;
    for (ll i = 1; i <= n; i++)
    {
      if (in[i] > in[node])
      {
        node = i;
      }
    }
    ans += in[node] - 1;
    cout << ans << endl;
  }
}