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


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 snd[kMaxN];
int low[kMaxN];

void DFS(int v, int p = 0) {
    low[v] = -1;
    int max_low = -1;
    int max_deg = 0;
    for (int u : g[v]) {
        if (u != p) {
            DFS(u, v);
            max_low = max(max_low, low[u]);
            low[v] = max({low[v], low[u]});
            if (g[u].size() > 1) {
                low[v] = max(low[v], (int)g[u].size());
            }
            max_deg = max(max_deg, (int)g[u].size());
        }
    }
    debug(v, snd[v]);
    if (g[v].size() > 1 || !p) {
        snd[v] = max((int)g[v].size() + max_low - 1, (int)(g[v].size()) - 2 + max_deg);
    } else {
        snd[v] = 0;
    }
    debug(snd[v], max_low, low[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);
    }

    DFS(1);
    // for (int i = 1; i <= n; ++i) {
    //     cout << snd[i] << " ";
    // }
    // cout << "\n";
    cout << *std::max_element(snd + 1, snd + 1 + n) << "\n";
}

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

    int t;
    cin >> t;

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