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

Previous submission (verdict: Time limit exceeded on test 6):
#include "bits/stdc++.h"
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define dbg(x...)
#endif
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
const int mod = 1000000007;
// clang-format off
template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }
template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\n'; return cout; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
// #define _GLIBCXX_DEBUG
// clang-format on

void solve()
{
    ll t, m, n, a, b;
    string h;
    cin >> n;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++)
    {
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<int> v1(n), v2(n, -1e9);
    vector<pair<int, int>> v;
    for (int i = 0; i < n; i++)
    {
        int maxx = 0;
        sort(g[i].begin(), g[i].end());
        for (auto &x : g[i])
            maxx = max(maxx, (ll)g[x].size());
        v1[i] = maxx;
        v.push_back({(ll)g[i].size(), i});
    }
    sort(v.rbegin(), v.rend());
    int cnt = 0;
    int i = 0;
    while (cnt < n && i < n)
    {
        for (int j = 0; j < n; j++)
        {
            if (v[i].second != j && !binary_search(g[j].begin(), g[j].end(), v[i].second))
            {
                if (v2[j] == -1e9)
                {
                    cnt++;
                    v2[j] = v[i].first;
                }
            }
        }
        i++;
    }
    dbg(v1, v2);
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        int tp = g[i].size();
        ans = max(ans, tp + v1[i] - 2);
        ans = max(ans, tp + v2[i] - 1);
    }
    cout << ans << endl;
}
int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Submission (verdict: Accepted):
#include "bits/stdc++.h"
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define dbg(x...)
#endif
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
const int mod = 1000000007;
// clang-format off
template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }
template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\n'; return cout; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
// #define _GLIBCXX_DEBUG
// clang-format on

void solve()
{
    ll t, m, n, a, b;
    string h;
    cin >> n;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++)
    {
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<int> v1(n), v2(n, -1e9);
    multiset<int> s;
    for (int i = 0; i < n; i++)
        s.insert(g[i].size());
    int ans = 0;
    dbg(g);
    for (int i = 0; i < n; i++)
    {
        int node = g[i].size();
        auto it = s.find(node);
        s.erase(it);
        for (auto &x : g[i])
        {
            auto it = s.find(g[x].size());
            s.erase(it);
            s.insert(*it - 1);
        }
        int tp = *s.rbegin();
        dbg(s);
        ans = max(ans, (ll)g[i].size() + tp - 1);
        for (auto &x : g[i])
        {
            int tp2 = g[x].size();
            auto it = s.find(tp2 - 1);
            s.erase(it);
            s.insert(tp2);
        }
        s.insert(node);
        dbg(i, ans);
    }
    cout << ans << endl;
}
int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}