Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 530 ms):
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <climits>

using namespace std;

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

    unordered_map<int, set<int> > graph;
    unordered_map<int, int> neighbors;

    for (int edge = 0; edge < n-1; edge++) {
        int u, v;
        cin >> u >> v;
        graph[u].insert(v);
        graph[v].insert(u);
        neighbors[u]++;
        neighbors[v]++;
    }

    vector<pair<int, int> > nodes;
    for (const auto& entry : neighbors) {
        nodes.push_back(make_pair(entry.second, entry.first));
    }

    sort(nodes.begin(), nodes.end(), greater<pair<int, int> >());

    // for (const auto& node : nodes) {
    //     cout << "Node " << node.second << " has " << node.first << " neighbors." << endl;
    // }

    int max_neighbors = nodes[0].first;
    vector<int> max_nodes;

    for (const auto& node : nodes) {
        if (node.first == max_neighbors) {
            max_nodes.push_back(node.second);
        } else {
            break;
        }
    }

    int res = 1;
    res += nodes[0].first - 1;
    // cout << "after deleting max: " << res << endl;
    if (max_nodes.size() == 1) {
        max_neighbors = nodes[1].first;

        max_nodes.clear();
        for (int i = 1; i < nodes.size(); ++i) {
            if (nodes[i].first == max_neighbors) {
                max_nodes.push_back(nodes[i].second);
            } else {
                break;
            }
        }
        int add = max_neighbors - 2;
        for (const auto& node : max_nodes) {
            if (graph[node].find(nodes[0].second) == graph[node].end()) {
                add += 1;
                break;
            } 
        }
        res += add;
        cout << res << endl;
        return;
        
    }
    if (max_nodes.size() == 2) {
        int add = nodes[0].first - 1;
        if (graph[nodes[0].second].find(nodes[1].second) != graph[nodes[0].second].end()){
            // cout << "here1: " << endl;
            add-=1;
        }
        // cout << "here2: " << endl;

        
        // for (const auto& node : max_nodes) {
        //     if (graph[node].find(nodes[0].second) == graph[node].end()) {
        //     add -= 1;
        //     break;
        //     }
        // }
        res += add;
        cout << res << endl;
        return;
    }

   
    cout << res + nodes[0].first - 1<< endl;
  
    return;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

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

Solution 2 (runtime: 1015 ms):
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")

#define pb push_back                    
#define ll long long
//#define sort(all(v)) sort(v.begin(),v.end())

int mod = 998244353;
const int N = 4e5 + 10;
const int inf = 1e9;
int fact[200001];
ll binpow(ll a, ll b){
 if(b == 0) return 1;
 else if(b % 2 == 1) return (a * binpow(a, b - 1)) % mod;
 ll p = binpow(a,b / 2);
 return (p * p) % mod;
}
vector <int> g[N];
int sum[N],was[N];
void dfs(int u){
    was[u] = 1;
    if(sum[u] == 0 && u != 1) sum[u]++;
    for(int to : g[u]){
        if(was[to] != 0) continue;
        sum[u]++;
        dfs(to);
    }
}
signed main(){
    //freopen("mootube.in", "r", stdin);
    //freopen("mootube.out", "w", stdout);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i = 1; i < n; i++){
            int u,v;
            cin >> u >> v;
            g[u].pb(v);
            g[v].pb(u);
        }
        dfs(1);
        multiset<int> mt;
        for(int i = 1; i <= n; i++)mt.insert(sum[i]);
        int ans = 0;
        for(int i = 1; i <= n; i++){
            mt.erase(mt.find(sum[i]));
            for(int to : g[i]){
                mt.erase(mt.find(sum[to]));
                mt.insert(sum[to] - 1);
            }
            ans = max(ans,*mt.rbegin() + sum[i] - 1);
            for(int to : g[i]){
                mt.erase(mt.find(sum[to] - 1));
                mt.insert(sum[to]);
            }
            mt.insert(sum[i]);
        }
        cout << ans << '\n';
        for(int i = 1; i <= n; i++){
            sum[i] = was[i] = 0;
            g[i].clear();
        }
    }
}