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

Previous submission (verdict: Wrong answer on test 2):
#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;
        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;
}

Submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <climits>
#include <stack>

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;
    vector<bool> visited(n + 1, false);
    int connected_components = 0;

    for (const auto& entry : graph) {
        if (!visited[entry.first]) {
            connected_components++;
            stack<int> s;
            s.push(entry.first);
            while (!s.empty()) {
                int node = s.top();
                s.pop();
                if (!visited[node]) {
                    visited[node] = true;
                    for (int neighbor : graph[node]) {
                        if (!visited[neighbor]) {
                            s.push(neighbor);
                        }
                    }
                }
            }
        }
    }

    // cout << "Number of connected components: " << connected_components << endl;
    res= connected_components;
    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;
        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;
}