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

Previous submission (verdict: Compilation error):
#include<bits/stdc++.h>
using namespace std;

using ll = long long;
using str = string;
using vi = vector<int>;
using pi = pair<int, int>;
using vpi = vector<pi>;
using si = set<int>;

#define sz (int)(x).size()
#define pb push_back
#define all(x) begin(x), end(x)
#define sor(x) sort(all(x))
#define rev(x) reverse(all(x))
#define del(x, i) erase(begin(x) + i)
#define rem(x, i) erase(begin(x), begin(x) + i)

#define mp make_pair
#define sec second
#define fir first

#define ins insert
#define ez erase
#define cnt count

#define MOD 1e9 + 7
#define MAX_INT 1e18 * 9

#define possibilities(x) while(next_permutation(all(x)))

#define Trav(a, x) for (auto &a : x)
#define For(i, a, b) for (int i = (a); i < (b); ++i)

void solve() {
    int n;
    cin >> n;
    vector<vector<int>> adj(n);
    vector<int> degree(n);
    For(i, 0, n - 1) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        degree[x]++, degree[y]++;
        adj[x].pb(y), adj[y].pb(x);
    }
    if(n <= 2) {
        cout << 0 << endl;
        return;
    }
    int res = 1;
    set<pair<int, int>> ms;

    For(i, 0, n) {
        ms.ins({degree[i], i});
    }

    For(i, 0, 2) {
        set<pair<int, int>> temp(ms);
        int curr = 0;
        int rm = (temp.rbegin() - i)->second;
        curr += degree[rm];
        temp.erase({degree[rm], rm});
        for(auto &x: adj[rm]) {
            temp.erase({degree[x], x});
            temp.ins({degree[x] - 1, x});
        }
        rm = temp.rbegin()->second;
        curr += degree[rm];
        res = max(res, curr - 1);
    }
        
    cout << res << endl;
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}

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

using ll = long long;
using str = string;
using vi = vector<int>;
using pi = pair<int, int>;
using vpi = vector<pi>;
using si = set<int>;

#define sz (int)(x).size()
#define pb push_back
#define all(x) begin(x), end(x)
#define sor(x) sort(all(x))
#define rev(x) reverse(all(x))
#define del(x, i) erase(begin(x) + i)
#define rem(x, i) erase(begin(x), begin(x) + i)

#define mp make_pair
#define sec second
#define fir first

#define ins insert
#define ez erase
#define cnt count

#define MOD 1e9 + 7
#define MAX_INT 1e18 * 9

#define possibilities(x) while(next_permutation(all(x)))

#define Trav(a, x) for (auto &a : x)
#define For(i, a, b) for (int i = (a); i < (b); ++i)

void solve() {
    int n;
    cin >> n;
    vector<vector<int>> adj(n);
    vector<int> degree(n);
    For(i, 0, n - 1) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        degree[x]++, degree[y]++;
        adj[x].pb(y), adj[y].pb(x);
    }
    if(n <= 2) {
        cout << 0 << endl;
        return;
    }
    int res = 1;
    set<pair<int, int>> ms;

    For(i, 0, n) {
        ms.ins({degree[i], i});
    }

    For(i, 0, 2) {
        set<pair<int, int>> temp(ms);
        int curr = 0;
        auto it1 = temp.end();
        For(j, 0, i + 1) it1--; 
        int rm = it1->second;
        curr += it1->first;
        temp.erase({degree[rm], rm});
        for(auto &x: adj[rm]) {
            temp.erase({degree[x], x});
            temp.ins({degree[x] - 1, x});
        }
        auto it2 = temp.end();
        it2--;
        rm = it2->second;
        curr += it2->first;
        res = max(res, curr - 1);
    }
        
    cout << res << endl;
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}