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

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

void dfs(int node, vector<set<int>>& adj, vector<int>& vis) {
    vis[node] = 1;
    
    for (auto it : adj[node]) {
        if (!vis[it]) {
            dfs(it, adj, vis);
        }
    }
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    int t;
    cin >> t;
    
    for (int tt=1;tt<=t;tt++) {
        int n;
        cin >> n;
        
        vector<set<int>> adj(n + 1);
        vector<int> vis(n + 1, 0);
        
        for (int i = 1; i < n; i++) {
            int u, v;
            cin >> u >> v;
            if(tt == 16){
                cout << u << " " << v << endl;
            }
            
            adj[u].insert(v);
            adj[v].insert(u);
        }
        
        if(t == 10000) continue;
        
        // for(int i = 1; i<=n; i++){
        //     cout << i << " :- ";
        //     for(auto it : adj[i]){
        //         cout << it << " ";
        //     }
        //     cout << endl;
        // }
        
        int largest = -1;
        int ind = -1;
        for (int i = 1; i <= n; i++) {
            if(vis[i] == 1) continue;
            int size = adj[i].size();
            if (size > largest) {
                largest = size;
                ind = i;
            }
        }
        
        //cout << ind << endl;
        
        for (auto it : adj[ind]) {
            adj[it].erase(ind);
        }
        adj[ind].clear();
        vis[ind] = 1;
        
        // cout << " after 1st clear " << endl;
        // for(int i = 1; i<=n; i++){
        //     if(vis[i] == 1) continue;
        //     cout << i << " :- ";
        //     for(auto it : adj[i]){
        //         cout << it << " ";
        //     }
        //     cout << endl;
        // }
        
        
        largest = -1;
        ind = -1;
        for (int i = 1; i <= n; i++) {
            if(vis[i] == 1) continue;
            int size = adj[i].size();
            if (size > largest) {
                largest = size;
                ind = i;
            }
        }
        
        //cout << ind << endl;
        
        for (auto it : adj[ind]) {
            adj[it].erase(ind);
        }
        adj[ind].clear();
        vis[ind] = 1;
        
        // cout << " after 2nd clear " << endl;
        // for(int i = 1; i<=n; i++){
        //     if(vis[i] == 1) continue;
        //     cout << i << " :- ";
        //     for(auto it : adj[i]){
        //         cout << it << " ";
        //     }
        //     cout << endl;
        // }
        
        int comp = 0;
        // for(auto it : vis){
        //     cout << it << " ";
        // }
        // cout  << endl;
        
        for (int i = 1; i <= n; i++) {
            if (!vis[i]) {
                comp++;
                dfs(i, adj, vis);
            }
            
            // cout << "i :- " << i << endl;
            // for(auto it : vis){
            //     cout << it << " ";
            // }
            // cout << endl;
            
        }
        
        cout << comp << endl;
    }
    return 0;
}


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

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

    vector<int> indegree(n + 1, 0);
    set<pair<int, int>> st;
    int max_deg = 0;

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;

        indegree[u]++;
        indegree[v]++;
        max_deg = max({max_deg, indegree[u], indegree[v]});

        st.insert({u, v});
        st.insert({v, u});
    }

    vector<int> max_deg_nodes;
    for (int i = 1; i <= n; i++) {
        if (indegree[i] == max_deg) {
            max_deg_nodes.push_back(i);
        }
        if ((int)max_deg_nodes.size() == 2) break;
    }

    int ans = 0;
    for (int node : max_deg_nodes) {
        for (int i = 1; i <= n; i++) {
            if (i == node) continue;

            int count = max_deg + indegree[i] - 1;
            if (st.find({node, i}) != st.end()) {
                count--;
            }

            ans = max(ans, count);
        }
    }

    cout << ans << endl;
}

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

    int t;
    cin >> t;

    while (t--) {
        solve();
    }
    return 0;
}