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

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--){
        int n;
        cin >> n;
        vector<pair<int,int>> edges;
        int counting[200015]; // counting[i] denotes the number of edges that connects to edge i
        for (int i=1; i<=n; i++) counting[i] = 0; // reset counting
        vector<pair<int,int>> cnt; // counting, index
        int dem = 1; // number of connected components

        for (int i=1; i<n; i++){
            int x, y;
            cin >> x >> y;
            edges.push_back({x,y});
            counting[x]++;
            counting[y]++;
        }

        // first deletion
        for (int i=1; i<=n; i++) cnt.push_back({counting[i], i});
        sort(cnt.begin(),cnt.end());

        int node = cnt.back().second;
        for (auto edge : edges){
            int x = edge.first, y = edge.second;

            if (x == node || y == node) counting[x + y - node]--;
        }

        dem += cnt.back().first - 1;
        counting[node] = 0;
        cnt.clear();

        // second deletion
        for (int i=1; i<=n; i++) cnt.push_back({counting[i], i});
        sort(cnt.begin(),cnt.end());

        dem += cnt.back().first - 1;

        // clearing for next test case
        cnt.clear();
        edges.clear();

        cout << dem << endl;
    }

    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<pair<int,int>> edges;
        int counting[200015]; // counting[i] denotes the number of edges that connects to edge i
        bool adj[200015]; // adj[i] denotes whether node i is adjacent to the maxnode
        for (int i=1; i<=n; i++){ // reset counting and adj
            counting[i] = 0;
            adj[i] = false;
        }
        vector<pair<int,int>> cnt; // counting, index
        int dem = 1; // number of connected components
        vector<int> maxnode2;

        for (int i=1; i<n; i++){
            int x, y;
            cin >> x >> y;
            edges.push_back({x,y});
            counting[x]++;
            counting[y]++;
        }

        for (int i=1; i<=n; i++) cnt.push_back({counting[i], i});
        sort(cnt.begin(),cnt.end());

        int maxnode = cnt.back().second;
        for (auto edge: edges){
            int x = edge.first;
            int y = edge.second;

            if (x == maxnode || y == maxnode) adj[x + y - maxnode] = true;
        }

        for (int i=cnt.size() - 2; i >= 0; i--){
            if (cnt[i].first == cnt[cnt.size() - 2].first) maxnode2.push_back(cnt[i].second);
        }

        dem = cnt.back().first + counting[maxnode2[0]] - 1;

        if (cnt.back().first == counting[maxnode2[0]] && maxnode2.size() > 1) continue; // more than 3 max nodes
        else{
            bool check = false; // true = there is a node in maxnode2 that doesnt directly connect to maxnode

            for (auto x : maxnode2){
                if (adj[x] == false) check = true;
            }

            if (!check) dem--;
        }

        // clearing for next test case
        cnt.clear();
        edges.clear();
        maxnode2.clear();

        cout << dem << endl;
    }

    return 0;
}