Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <random>
#include <bitset>
#include <cmath>
#include <cassert>
#include <sstream>
 
using namespace std;
 
#define ll long long
#define vec vector
#define pii pair<int, int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define debug(x) cerr << (#x) << " = " << (x) << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define time (double)clock() / CLOCKS_PER_SEC
#define forn(i, n) for (int i = 0; i < n; i++)
#define each(i, a) for (auto& i : a)
 
template <typename T>
void print(const vec<T> & a) {
    each(e, a)
        cout << e << " ";
    cout << endl;
}
 
template <typename T>
void scan(vec<T> & a) {
    each(e, a) 
        cin >> e;
}

void scan_graph(int n, int m, vec<vec<int>> & g) {
    forn(i, m) {
        int a, b; cin >> a >> b; a--; b--;
        g[a].pb(b);
        g[b].pb(a);
    }
}

void scan_graph(int n, int m, vec<vec<pii>> & g) {
    forn(i, m) {
        int a, b, c; cin >> a >> b >> c; a--; b--;
        g[a].eb(b, c);
        g[b].eb(a, c);
    }
}
 
template <typename T>
T sum(const vec<T> & a) {
    T sum = 0;
    each(e, a) 
        sum += e;
    return sum;
}
 
ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
 
const int inf = 1e9 + 1;
const ll llinf = 1e18; 
 
const ll mod = 998244353;
 
inline int sm(int a, int b) {
    if (a + b >= mod) return a + b - mod;
    return a + b;
}
 
inline int dif(int a, int b) {
    if (a < b) return a + mod - b;
    return a - b;
}
 
int mul(int a, int b) {
    return (ll)a * b % mod;
}

void bool_ans(bool fl) {
    if (fl)
        cout << "YES\n";
    else
        cout << "NO\n";
}

void solve() {
    int n; cin >> n;
    vec<vec<int>> g(n);
    scan_graph(n, n - 1, g);
    int mx1 = 0, mx2 = 1;
    if (g[mx1].size() < g[mx2].size()) swap(mx1, mx2);
    for (int i = 2; i < n; i++) {
        if (g[i].size() > g[mx1].size()) mx2 = mx1, mx1 = i;
        else if (g[i].size() > g[mx2].size()) mx2 = i; 
    }
    int best = g[mx1].size() + g[mx2].size() - 1;
    if (find(all(g[mx1]), mx2) == g[mx1].end()) {
        cout << best << endl;
        return;
    }

    set<int> st;
    forn (i, n) {
        if (g[i].size() == g[mx2].size())
            st.insert(i);
    }
    for (auto to : g[mx1]) {
        if (st.find(to) != st.end())
            st.erase(to);
    }
    if (st.find(mx1) != st.end())
        st.erase(mx1);
    if (st.size())
        cout << best << endl;
    else 
        cout << best - 1 << endl;
    
    return;
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int t_cases = 1;
    cin >> t_cases;
    while (t_cases--) {
        solve();
        cerr << "----------\n";
    }
    return 0;
}

Submission (verdict: Accepted):
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <random>
#include <bitset>
#include <cmath>
#include <cassert>
#include <sstream>
 
using namespace std;
 
#define ll long long
#define vec vector
#define pii pair<int, int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define debug(x) cerr << (#x) << " = " << (x) << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define time (double)clock() / CLOCKS_PER_SEC
#define forn(i, n) for (int i = 0; i < n; i++)
#define each(i, a) for (auto& i : a)
 
template <typename T>
void print(const vec<T> & a) {
    each(e, a)
        cout << e << " ";
    cout << endl;
}
 
template <typename T>
void scan(vec<T> & a) {
    each(e, a) 
        cin >> e;
}

void scan_graph(int n, int m, vec<vec<int>> & g) {
    forn(i, m) {
        int a, b; cin >> a >> b; a--; b--;
        g[a].pb(b);
        g[b].pb(a);
    }
}

void scan_graph(int n, int m, vec<vec<pii>> & g) {
    forn(i, m) {
        int a, b, c; cin >> a >> b >> c; a--; b--;
        g[a].eb(b, c);
        g[b].eb(a, c);
    }
}
 
template <typename T>
T sum(const vec<T> & a) {
    T sum = 0;
    each(e, a) 
        sum += e;
    return sum;
}
 
ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
 
const int inf = 1e9 + 1;
const ll llinf = 1e18; 
 
const ll mod = 998244353;
 
inline int sm(int a, int b) {
    if (a + b >= mod) return a + b - mod;
    return a + b;
}
 
inline int dif(int a, int b) {
    if (a < b) return a + mod - b;
    return a - b;
}
 
int mul(int a, int b) {
    return (ll)a * b % mod;
}

void bool_ans(bool fl) {
    if (fl)
        cout << "YES\n";
    else
        cout << "NO\n";
}

void solve() {
    int n; cin >> n;
    vec<vec<int>> g(n);
    scan_graph(n, n - 1, g);
    int mx1 = 0, mx2 = 1;
    if (g[mx1].size() < g[mx2].size()) swap(mx1, mx2);
    for (int i = 2; i < n; i++) {
        if (g[i].size() > g[mx1].size()) mx2 = mx1, mx1 = i;
        else if (g[i].size() > g[mx2].size()) mx2 = i; 
    }
    int best = g[mx1].size() + g[mx2].size() - 1;
    if (find(all(g[mx1]), mx2) == g[mx1].end()) {
        cout << best << endl;
        return;
    }

    if (g[mx1].size() != g[mx2].size()) {
        set<int> st;
        forn (i, n) {
            if (g[i].size() == g[mx2].size())
                st.insert(i);
        }
        for (auto to : g[mx1]) {
            if (st.find(to) != st.end())
                st.erase(to);
        }
        if (st.find(mx1) != st.end())
            st.erase(mx1);
        if (st.size())
            cout << best << endl;
        else 
            cout << best - 1 << endl;
        return;
    }
    int cnt = 0;
    forn (i, n) if (g[i].size() == g[mx1].size()) cnt++;
    if (cnt > 2) {
        cout << best << endl;
    } else {
        cout << best - 1 << endl;
    }

    return;
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int t_cases = 1;
    cin >> t_cases;
    while (t_cases--) {
        solve();
        cerr << "----------\n";
    }
    return 0;
}