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 maxn 200002
//#define int long long
#define ll long long
//int arr[maxn] = {};

bool flag[maxn] = {};

vector<vector<int>> clear(int n, vector<vector<int>>& v) {
    int maxNum = 0, node = 0;
    for (int i = 1; i <= n; ++i) {
        if (v[i].size() > maxNum) {
            maxNum = v[i].size();
            node = i;
        }
    }

    vector<vector<int>> newV(n + 1);
    for (int i = 1; i <= n; ++i) {
        if (i == node) continue;
        for (auto j : v[i]) {
            if (j != node) {
                newV[i].push_back(j);
            }
        }
    }
    return newV;
}

void dfs(int node, vector<vector<int>>& v) {
    for (auto i : v[node]) {
        if (!flag[i]) {
            flag[i] = true;
            dfs(i, v);
        }
    }
}

void solve() {
    int n = 0;
    cin >> n;
    vector<vector<int>> v(n + 1);
    for (int i = 1; i < n; ++i) flag[i] = false;
    for (int i = 1, v1, v2; i < n; ++i) {
        cin >> v1 >> v2;
        v[v1].push_back(v2);
        v[v2].push_back(v1);
    }
    v = clear(n, v);
    v = clear(n, v);
    int res = 0;
    for (int i = 1; i <= n; ++i) {
        if (!flag[i]) {
            dfs(i, v);
            res += 1;
        }
    }
    cout << res - 2 << endl;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
//    solve();
    int t; cin >> t;
    while (t--) solve();
}

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

using namespace std;
#define maxn 200002
//#define int long long
#define ll long long
//int arr[maxn] = {};

bool flag[maxn] = {};

void solve() {
    int n = 0;
    cin >> n;
    vector<vector<int>> v(n + 1);
    for (int i = 1; i < n; ++i) flag[i] = false;
    for (int i = 1, v1, v2; i < n; ++i) {
        cin >> v1 >> v2;
        v[v1].push_back(v2);
        v[v2].push_back(v1);
    }

    int maxNum = 0;
    vector<int> maxNumNode;
    for (int i = 1; i <= n; ++i) {
        if (v[i].size() > maxNum) {
            maxNum = v[i].size();
            maxNumNode.clear();
            maxNumNode.push_back(i);
        } else if (v[i].size() == maxNum) {
            maxNumNode.push_back(i);
        }
    }
    if (maxNumNode.size() == 1) {
        int secNum = 0;
        vector<int> secNumNode;
        for (int i = 1; i <= n; ++i) {
            if (v[i].size() > secNum && v[i].size() != maxNum) {
                secNum = v[i].size();
                secNumNode.clear();
                secNumNode.push_back(i);
            } else if (v[i].size() == secNum) {
                secNumNode.push_back(i);
            }
        }
        bool expect = false;
        for (auto i : v[maxNumNode[0]]) {
            flag[i] = true;
        }
        for (int i = 0; i < secNumNode.size(); ++i) {
            if (!flag[secNumNode[i]]) {
                expect = true;
                break;
            }
        }
        if (expect) {
            cout << secNum + maxNum - 1 << endl;
        } else {
            cout << secNum + maxNum - 2 << endl;
        }

    } else {
        bool expect = false;
        for (int i = 0; i < maxNumNode.size(); ++i) {
            if (expect) break;
            for (auto j : v[maxNumNode[i]]) {
                flag[j] = true;
            }
            for (int j = i + 1; j < maxNumNode.size(); ++j) {
                if (!flag[maxNumNode[j]]) {
                    expect = true;
                    break;
                }
            }
            for (auto j : v[maxNumNode[i]]) {
                flag[j] = false;
            }
        }
        if (expect) {
            cout << maxNum + maxNum - 1 << endl;
        } else {
            cout << maxNum + maxNum - 2 << endl;
        }
    }

}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
//    solve();
    int t; cin >> t;
    while (t--) solve();
}