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

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
#ifndef InfernoStinger
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
#define all(x) (x).begin() , (x).end()
#define ll long long
#define int long long  
#define mod 1000000007
#define mod1 998244353
#define inf 1e18
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(long double t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(bool t) {cerr << t;}
void _print(unsigned long long t) {cerr << t;}
template <class T> bool ckmin(T&a, const T&b) { bool B = a > b; a = min(a,b); return B; }
template <class T> bool ckmax(T&a, const T&b) { bool B = a < b; a = max(a,b); return B; }
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

int solve()
{   
    int n;
    cin>>n;
    vector<set<int>> g(n+1);
    vector<int> deg(n+1, 0);
    set<int> cur;
    for(int i=1;i<=n;i++){
        cur.insert(i);
    }
    for(int i=0;i<n-1;i++){
        int v,u;
        cin>>v>>u;
        g[v].insert(u);
        g[u].insert(v);
        deg[u]++;deg[v]++;
    }
    auto red = [&]() -> void {
        int val=0;
        for(auto i:cur){
            if(deg[i]>=deg[val]){
                val=i;
            }
        }
        vector<int> torem;
        for(auto ver:g[val]){
            torem.pb(ver);
        }
        for(auto rem:torem){
            g[rem].erase(val);
            g[val].erase(rem);
            deg[rem]--;deg[val]--;
        }
        cur.erase(val);
    };
    red();
    red();
    queue<int> q;
    vector<bool> vis(n+1, false);
    int ans=0;
    for(auto i:cur){
        if(vis[i]) continue;
        q.push(i);
        ans++;
        while(q.size()){
            int node=q.front();
            q.pop();
            vis[node]=true;
            for(auto ver:g[node]){
                if(!vis[ver]) q.push(ver);
            }
        }
    }
    cout<<ans<<"\n";
    return 0;
}
 
int32_t main() 
{   
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
    #endif
    int t;
    cin>>t;
    for(int tc=0;tc<t;tc++)
    {   
        solve();   
    }
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
#ifndef InfernoStinger
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
#define all(x) (x).begin() , (x).end()
#define ll long long
#define int long long  
#define mod 1000000007
#define mod1 998244353
#define inf 1e18
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(long double t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(bool t) {cerr << t;}
void _print(unsigned long long t) {cerr << t;}
template <class T> bool ckmin(T&a, const T&b) { bool B = a > b; a = min(a,b); return B; }
template <class T> bool ckmax(T&a, const T&b) { bool B = a < b; a = max(a,b); return B; }
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

int solve()
{   
    int n;
    cin>>n;
    vector<set<int>> g(n+1);
    vector<int> deg(n+1, 0);
    multiset<int> curs;
    for(int i=0;i<n-1;i++){
        int v,u;
        cin>>v>>u;
        g[v].insert(u);
        g[u].insert(v);
        deg[u]++;deg[v]++;
    }
    for(int i=1;i<=n;i++){
        curs.insert(deg[i]);
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        int temp=deg[i];
        curs.erase(curs.find(deg[i]));
        for(auto nei:g[i]){
            curs.erase(curs.find(deg[nei]));
            curs.insert(deg[nei]-1);
        }
        ans=max(ans, temp+*curs.rbegin());
        for(auto nei:g[i]){
            curs.erase(curs.find(deg[nei]-1));
            curs.insert(deg[nei]);
        }
        curs.insert(temp);
    }
    cout<<ans-1<<"\n";
    return 0;
}
 
int32_t main() 
{   
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
    #endif
    int t;
    cin>>t;
    for(int tc=0;tc<t;tc++)
    {   
        solve();   
    }
    return 0;
}