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

Previous submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        vector<int> adj[n+1];
        map<int, int> hash;
        for(int i=0; i<n-1; i++) {
            int u, v;
            cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
            hash[u]++;
            hash[v]++;
        }
        int first = 0;
        int second = 0;
        int firstele=0, secele=0;
        
        for(auto it: hash) {
            if(it.second >= first) {
                second = first;
                first = it.second;
                secele = firstele;
                firstele = it.first;
            }
            else if(it.second > second) {
                second = it.second;
                secele = it.first;
            }
        }
        
        set<int> st;
        
        for(auto it: adj[firstele]) {
            st.insert(it);
        }
        
        for(auto it: hash) {
            if(it.second == second && st.find(it.first)==st.end()) {
                secele = it.first;
                second = it.second;
            } 
        }
        
        // cout << firstele << "-->" << hash[firstele] << "  " << secele << "-->" << hash[secele] << "\n";
        int flag = 0;
        int ans;
        for(auto it: adj[firstele]) {
            if(it == secele) flag = 1;
        }
        if(flag == 1) ans = hash[firstele] + hash[secele] - 2;
        else ans = hash[firstele] + hash[secele] - 1;
        cout << ans << "\n";
    }
    
    return 0;
}

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        vector<int> adj[n+1];
        map<int, int> hash;
        for(int i=0; i<n-1; i++) {
            int u, v;
            cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
            hash[u]++;
            hash[v]++;
        }
        if(n==2) {
            cout << 0 << "\n";
            continue;
        }
        int first = 0;
        int second = 0;
        int firstele=0, secele=0;
        
        for(auto it: hash) {
            if(it.second >= first) {
                second = first;
                first = it.second;
                secele = firstele;
                firstele = it.first;
            }
            else if(it.second > second) {
                second = it.second;
                secele = it.first;
            }
        }
        
        set<int> st;
        
        for(auto it: adj[firstele]) {
            st.insert(it);
        }
        
        for(auto it: hash) {
            if(it.second == second && st.find(it.first)==st.end()) {
                secele = it.first;
                second = it.second;
            } 
        }
        
        // cout << firstele << "-->" << hash[firstele] << "  " << secele << "-->" << hash[secele] << "\n";
        int flag = 0;
        int ans;
        for(auto it: adj[firstele]) {
            if(it == secele) flag = 1;
        }
        if(flag == 1) ans = hash[firstele] + hash[secele] - 2;
        else ans = hash[firstele] + hash[secele] - 1;
        cout << ans << "\n";
    }
    
    return 0;
}