Problem: 2063C - Remove Exactly Two
Language: c++20 

Code 1:
#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;
}
ll fn(ll node, set<pair<ll, ll>> &st, ll n, vi &in)
{
  ll ans = 0;
  for (ll i = 1; i <= n; i++)
  {
    if (i == node)
    {
      continue;
    }
    if (st.find({node, i}) == st.end())
    {
      ans = max(ans, 1 + in[node] - 1 + in[i] - 1);
    }
    else
    {
      ans = max(ans, 1 + in[node] - 1 + in[i] - 1 - 1);
    }
  }
  return ans;
}
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);
    set<pair<ll, ll>> st;
    for (ll i = 1; i < n; i++)
    {
      ll x, y;
      cin >> x >> y;
      // adj[x].push_back(y);
      // adj[y].push_back(x);
      st.insert({x, y});
      st.insert({y, x});
      in[x]++;
      in[y]++;
    }
    ll maxi = 0, smaxi = 0;
    ll n1 = 0, n2 = 0;
    for (ll i = 1; i <= n; i++)
    {
      if (in[i] >= maxi)
      {
        n2 = n1;
        smaxi = maxi;
        maxi = in[i];
        n1 = i;
      }
      else if (in[i] > smaxi)
      {
        smaxi = in[i];
        n2 = i;
      }
    }
    ll ans = fn(n1, st, n, in);
    ans = max(ans, fn(n2, st, n, in));
    cout << ans << endl;
  }
}


Code 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;
  }
}


Answer: 1