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

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;




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

    vector<vector<int>> adj(n+1, vector<int>());
    FOR(i, 1, n - 1){
        int u,v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }

    multiset<int> s;
    FOR(i, 1, n){
        s.insert(sz(adj[i]));
    }

    int answer = 0;
    FOR(x, 1, n){
        for(int v : adj[x]){
            answer = max(answer, sz(adj[x]) + sz(adj[v]) - 2);
            s.erase(s.find(sz(adj[v])));
        }

        answer = max(answer, sz(adj[x]) + (*(--(s.end()))) - 1);

        for(int v : adj[x]){
            s.insert(sz(adj[v]));
        }
    }

    cout << answer << "\n";

    return;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

    int t = 1; cin >> t;
    while(t--) solve();
    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;




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

    vector<vector<int>> adj(n+1, vector<int>());
    FOR(i, 1, n - 1){
        int u,v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }

    multiset<int> s;
    FOR(i, 1, n){
        s.insert(sz(adj[i]));
    }

    int answer = 0;
    FOR(x, 1, n){
        s.erase(s.find(sz(adj[x])));
        for(int v : adj[x]){
            answer = max(answer, sz(adj[x]) + sz(adj[v]) - 2);
            s.erase(s.find(sz(adj[v])));
        }

        if(s.size() > 0)
            answer = max(answer, sz(adj[x]) + (*(--(s.end()))) - 1);

        for(int v : adj[x]){
            s.insert(sz(adj[v]));
        }
        s.insert(sz(adj[x]));
    }

    cout << answer << "\n";

    return;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

    int t = 1; cin >> t;
    while(t--) solve();
    return 0;
}