Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include <chrono>
#include <iostream>
#include <random>

#include <algorithm>

#include <map>
#include <set>

#ifdef GRINDSET
#include "../debug.h"
#else
#define debug(...) ;
#define print(...) ;
#endif

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;

template <typename T>
concept iterable =
    requires(T a) {
        { std::begin(a) } -> std::input_iterator;
        { std::end(a) } -> std::sentinel_for<decltype(std::begin(a))>;
    } &&
    !std::convertible_to<T, std::string>;

template<class T>
requires iterable<T>
std::ostream& operator<<(std::ostream& out, const T& a);

template<class T, class S>
std::ostream& operator<<(std::ostream& out, const std::pair<T, S>& a);

template<class T>
requires iterable<T>
std::istream& operator>>(std::istream& in, T& a);

template<class T, class S>
std::istream& operator>>(std::istream& in, std::pair<T, S>& a);

template<class T>
requires iterable<T>
std::ostream& operator<<(std::ostream& out, const T& a) {
    for (auto& e : a) {
        out << e << " ";
    }
    out << "\n";
    return out;
}

template<class T, class S>
std::ostream& operator<<(std::ostream& out, const std::pair<T, S>& a) {
    out << a.first << ": " << a.second << "\n";
    return out;
}

template<class T>
requires iterable<T>
std::istream& operator>>(std::istream& in, T& a) {
    for (auto& e : a) {
        in >> e;
    }
    return in;
}

template<class T, class S>
std::istream& operator>>(std::istream& in, std::pair<T, S>& a) {
    in >> a.first >> a.second;
    return in;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int kMaxN = 2e5 + 5;
vi g[kMaxN];
int par[kMaxN];

void DFS(int v, int p = 0) {
    par[v] = p;
    for (int u : g[v]) {
        if (u != p) {
            DFS(u, v);
        }
    }
}

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

    for (int i = 1; i <= n; ++i) {
        g[i].clear();
    }

    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    vi ord(n + 1);
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin() + 1, ord.end(), [] (int i, int j) { return g[i].size() > g[j].size(); });

    int ans = g[ord[1]].size() + g[ord[2]].size() - 2;
    DFS(ord[1]);

    debug(ans);
    for (int i = 2; i <= n; ++i) {
        debug(i, ord[i]);
        if (par[ord[i]] != ord[1] && g[ord[i]].size() == g[ord[2]].size()) {
            ans++;
            break;
        }
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        Solve();
    }
}


Submission (verdict: Accepted):
#include <chrono>
#include <iostream>
#include <random>

#include <algorithm>

#include <map>
#include <set>

#ifdef GRINDSET
#include "../debug.h"
#else
#define debug(...) ;
#define print(...) ;
#endif

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;

template <typename T>
concept iterable =
    requires(T a) {
        { std::begin(a) } -> std::input_iterator;
        { std::end(a) } -> std::sentinel_for<decltype(std::begin(a))>;
    } &&
    !std::convertible_to<T, std::string>;

template<class T>
requires iterable<T>
std::ostream& operator<<(std::ostream& out, const T& a);

template<class T, class S>
std::ostream& operator<<(std::ostream& out, const std::pair<T, S>& a);

template<class T>
requires iterable<T>
std::istream& operator>>(std::istream& in, T& a);

template<class T, class S>
std::istream& operator>>(std::istream& in, std::pair<T, S>& a);

template<class T>
requires iterable<T>
std::ostream& operator<<(std::ostream& out, const T& a) {
    for (auto& e : a) {
        out << e << " ";
    }
    out << "\n";
    return out;
}

template<class T, class S>
std::ostream& operator<<(std::ostream& out, const std::pair<T, S>& a) {
    out << a.first << ": " << a.second << "\n";
    return out;
}

template<class T>
requires iterable<T>
std::istream& operator>>(std::istream& in, T& a) {
    for (auto& e : a) {
        in >> e;
    }
    return in;
}

template<class T, class S>
std::istream& operator>>(std::istream& in, std::pair<T, S>& a) {
    in >> a.first >> a.second;
    return in;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int kMaxN = 2e5 + 5;
vi g[kMaxN];
int par[kMaxN];

void DFS(int v, int p = 0) {
    par[v] = p;
    for (int u : g[v]) {
        if (u != p) {
            DFS(u, v);
        }
    }
}

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

    for (int i = 1; i <= n; ++i) {
        g[i].clear();
    }

    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    vi ord(n + 1);
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin() + 1, ord.end(), [] (int i, int j) { return g[i].size() > g[j].size(); });

    int ans = g[ord[1]].size() + g[ord[2]].size() - 2;
    DFS(ord[1]);

    bool ok = false;
    for (int i = 2; i <= n; ++i) {
        if (par[ord[i]] != ord[1] && g[ord[i]].size() == g[ord[2]].size()) {
            ans++;
            ok = true;
            break;
        }
    }

    if (!ok && g[ord[2]].size() == g[ord[1]].size()) {
        DFS(ord[2]);
        for (int i = 1; i <= n; ++i) {
            if (i != 2 && par[ord[i]] != ord[2] && g[ord[i]].size() == g[ord[2]].size()) {
                ans++;
                break;
            }
        }
    }

    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        Solve();
    }
}