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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <stack>
#include <set>
#include <list>
#include <map>
#include <cstring>
#include <iterator>
#define ll long long
#define MOD 1000000007 
using namespace std;
vector<int>sieve(int n){
    vector<int>p;
    vector<int>isprime(n+1, 1);
    isprime[0] = isprime[1] = 0;
    for (int i = 2; i<=n; i++){
        if (isprime[i]){
            for (int j = 2*i; j<=n; j+=i){
                isprime[j] = 0;
            }
        }
    }
    for (int i = 1; i<=n; i++){
        if (isprime[i]){
            p.push_back(i);
        }
    }
    return p;
}
//---------------------------------------------------------------------------
// bool check(int k, vector<int>d, vector<int>a){

// }

void solve(){
    int n;
    cin>>n;
    vector<vector<int>>adj(n);
    map<int,int>ed;
    for (int i = 0; i<n-1; i++){
        int u, v;
        cin>>u>>v;
        u--;
        v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    if (n <= 4){
        cout<<n-2<<endl;
        return;
    }
    for (int i = 0; i<n; i++){
        ed[i] = adj[i].size();
    }
    int edmax = 0;
    int maxver = 0;
    for (auto i:ed){
        if (i.second > edmax){
            edmax = i.second;
            maxver = i.first;
        }
    }
    if (edmax == 2){
        cout<<3<<endl;
        return;
    }
    for (int i:adj[maxver]){
        ed[i] -= 1;
    }
    int edmax1 = 0;
    int maxver1 = 0;
    for (auto i:ed){
        if (i.first != maxver && i.second > edmax1){
            edmax1 = i.second;
            maxver1 = i.first;
        }
    }
    cout<<edmax + edmax1 - 1<<endl;
}

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

Submission (verdict: Time limit exceeded on test 9):
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <stack>
#include <set>
#include <list>
#include <map>
#include <cstring>
#include <iterator>
#define ll long long
#define MOD 1000000007 
using namespace std;
vector<int>sieve(int n){
    vector<int>p;
    vector<int>isprime(n+1, 1);
    isprime[0] = isprime[1] = 0;
    for (int i = 2; i<=n; i++){
        if (isprime[i]){
            for (int j = 2*i; j<=n; j+=i){
                isprime[j] = 0;
            }
        }
    }
    for (int i = 1; i<=n; i++){
        if (isprime[i]){
            p.push_back(i);
        }
    }
    return p;
}
//---------------------------------------------------------------------------
// bool check(int k, vector<int>d, vector<int>a){

// }

void solve(){
    int n;
    cin>>n;
    vector<vector<int>>adj(n);
    map<int,int>ed;
    for (int i = 0; i<n-1; i++){
        int u, v;
        cin>>u>>v;
        u--;
        v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    if (n <= 4){
        cout<<n-2<<endl;
        return;
    }
    for (int i = 0; i<n; i++){
        ed[i] = adj[i].size();
    }
    // for (auto i:adj){
    //     for (int j:i){
    //         cout<<j<<" ";
    //     }cout<<endl;
    // }
    int edmax = 0;
    int maxver = 0;
    for (auto i:ed){
        if (i.second > edmax){
            edmax = i.second;
            maxver = i.first;
        }
    }
    // cout<<edmax<<" "<<endl;
    vector<pair<int,int>>maxvers;
    for (auto i:ed){
        if (i.second == edmax){
    //         // cout<<i.first<<endl;
            maxvers.push_back(make_pair(i.second, i.first));
        }
    }
    // for (auto i:maxvers){
    //     cout<<i.first<<" "<<i.second<<endl;
    // }
    int ans = 0;
    for (auto i:maxvers){
        set<pair<int,int>, greater<pair<int,int>>>s;
        for (auto j:ed){
            s.insert(make_pair(j.second, j.first));
        }
        s.erase(make_pair(i.first, i.second));
        for (auto k:adj[i.second]){
            s.erase(make_pair(ed[k], k));
            s.insert(make_pair(ed[k]-1, k));
        }
        auto it = s.begin()->first;
        ans = max(ans, i.first + it - 1);
    }
    cout<<ans<<endl;
    
    // int edmax = 0;
    // int maxver = 0;
    // for (auto i:ed){
    //     if (i.second > edmax){
    //         edmax = i.second;
    //         maxver = i.first;
    //     }
    // }
    // if (edmax == 2){
    //     cout<<3<<endl;
    //     return;
    // }
    // for (int i:adj[maxver]){
    //     ed[i] -= 1;
    // }
    // int edmax1 = 0;
    // for (auto i:ed){
    //     if (i.first != maxver && i.second > edmax1){
    //         edmax1 = i.second;
    //     }
    // }
    // cout<<edmax + edmax1 - 1<<endl;
}

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