Problem: 2063C - Remove Exactly Two
Language: c++23 

Code 1:
#include <bits/stdc++.h>
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifndef LOCAL_STRESS_TEST
struct IOPre { static constexpr int TEN = 10, SZ = TEN * TEN * TEN * TEN; std::array<char, 4 * SZ> num; constexpr IOPre() : num{} { for (int i = 0; i < SZ; i++) { int n = i; for (int j = 3; j >= 0; j--) { num[i * 4 + j] = static_cast<char>(n % TEN + '0'); n /= TEN; } } } }; struct IO {
#if !HAVE_DECL_FREAD_UNLOCKED
#define fread_unlocked fread
#endif
#if !HAVE_DECL_FWRITE_UNLOCKED
#define fwrite_unlocked fwrite
#endif
    static constexpr int SZ = 1 << 17, LEN = 32, TEN = 10, HUNDRED = TEN * TEN, THOUSAND = HUNDRED * TEN, TENTHOUSAND = THOUSAND * TEN, MAGIC_MULTIPLY = 205, MAGIC_SHIFT = 11, MASK = 15, TWELVE = 12, SIXTEEN = 16; static constexpr IOPre io_pre = {}; std::array<char, SZ> input_buffer, output_buffer; int input_ptr_left, input_ptr_right, output_ptr_right; std::stringstream ss; IO() : input_buffer{}, output_buffer{}, input_ptr_left{}, input_ptr_right{}, output_ptr_right{} {} IO(const IO&) = delete; IO(IO&&) = delete; IO& operator=(const IO&) = delete; IO& operator=(IO&&) = delete; ~IO() { flush(); } template <class T> struct is_char { static constexpr bool value = std::is_same_v<T, char>; }; template <class T> struct is_bool { static constexpr bool value = std::is_same_v<T, bool>; }; template <class T> struct is_string { static constexpr bool value = std::is_same_v<T, std::string> || std::is_same_v<T, const char*> || std::is_same_v<T, char*>; }; template <class T, class D = void> struct is_custom { static constexpr bool value = false; }; template <class T> struct is_custom<T, std::void_t<typename T::internal_value_type>> { static constexpr bool value = true; }; template <class T> struct is_default { static constexpr bool value = is_char<T>::value || is_bool<T>::value || is_string<T>::value || std::is_integral_v<T>; }; template <class T, class D = void> struct is_iterable { static constexpr bool value = false; }; template <class T> struct is_iterable< T, typename std::void_t<decltype(std::begin(std::declval<T>()))>> { static constexpr bool value = true; }; template <class T, class D = void, class E = void> struct is_applyable { static constexpr bool value = false; }; template <class T> struct is_applyable<T, std::void_t<typename std::tuple_size<T>::type>, std::void_t<decltype(std::get<0>(std::declval<T>()))>> { static constexpr bool value = true; }; template <class T> static constexpr bool needs_newline = (is_iterable<T>::value || is_applyable<T>::value) && (!is_default<T>::value); template <typename T, typename U> struct any_needs_newline { static constexpr bool value = false; }; template <typename T> struct any_needs_newline<T, std::index_sequence<>> { static constexpr bool value = false; }; template <typename T, std::size_t I, std::size_t... Is> struct any_needs_newline<T, std::index_sequence<I, Is...>> { static constexpr bool value = needs_newline<decltype(std::get<I>(std::declval<T>()))> || any_needs_newline<T, std::index_sequence<Is...>>::value; }; inline void load() { memmove(std::begin(input_buffer), std::begin(input_buffer) + input_ptr_left, input_ptr_right - input_ptr_left); input_ptr_right = input_ptr_right - input_ptr_left + static_cast<int>(fread_unlocked(std::begin(input_buffer) + input_ptr_right - input_ptr_left, 1, SZ - input_ptr_right + input_ptr_left, stdin)); input_ptr_left = 0; } inline void read_char(char& c) { if (input_ptr_left + LEN > input_ptr_right) load(); c = input_buffer[input_ptr_left++]; } inline void read_string(std::string& x) { char c; while (read_char(c), c < '!') continue; x = c; while (read_char(c), c >= '!') x += c; } template <class T> inline std::enable_if_t<std::is_integral_v<T>, void> read_int(T& x) { if (input_ptr_left + LEN > input_ptr_right) load(); char c = 0; do c = input_buffer[input_ptr_left++]; while (c < '-'); [[maybe_unused]] bool minus = false; if constexpr (std::is_signed<T>::value == true) if (c == '-') minus = true, c = input_buffer[input_ptr_left++]; x = 0; while (c >= '0') x = x * TEN + (c & MASK), c = input_buffer[input_ptr_left++]; if constexpr (std::is_signed<T>::value == true) if (minus) x = -x; } inline void skip_space() { if (input_ptr_left + LEN > input_ptr_right) load(); while (input_buffer[input_ptr_left] <= ' ') input_ptr_left++; } inline void flush() { fwrite_unlocked(std::begin(output_buffer), 1, output_ptr_right, stdout); output_ptr_right = 0; } inline void write_char(char c) { if (output_ptr_right > SZ - LEN) flush(); output_buffer[output_ptr_right++] = c; } inline void write_bool(bool b) { if (output_ptr_right > SZ - LEN) flush(); output_buffer[output_ptr_right++] = b ? '1' : '0'; } inline void write_string(const std::string& s) { for (auto x : s) write_char(x); } inline void write_string(const char* s) { while (*s) write_char(*s++); } inline void write_string(char* s) { while (*s) write_char(*s++); } template <typename T> inline std::enable_if_t<std::is_integral_v<T>, void> write_int(T x) { if (output_ptr_right > SZ - LEN) flush(); if (!x) { output_buffer[output_ptr_right++] = '0'; return; } if constexpr (std::is_signed<T>::value == true) if (x < 0) output_buffer[output_ptr_right++] = '-', x = -x; int i = TWELVE; std::array<char, SIXTEEN> buf{}; while (x >= TENTHOUSAND) { memcpy(std::begin(buf) + i, std::begin(io_pre.num) + (x % TENTHOUSAND) * 4, 4); x /= TENTHOUSAND; i -= 4; } if (x < HUNDRED) { if (x < TEN) { output_buffer[output_ptr_right++] = static_cast<char>('0' + x); } else { std::uint32_t q = (static_cast<std::uint32_t>(x) * MAGIC_MULTIPLY) >> MAGIC_SHIFT; std::uint32_t r = static_cast<std::uint32_t>(x) - q * TEN; output_buffer[output_ptr_right] = static_cast<char>('0' + q); output_buffer[output_ptr_right + 1] = static_cast<char>('0' + r); output_ptr_right += 2; } } else { if (x < THOUSAND) { memcpy(std::begin(output_buffer) + output_ptr_right, std::begin(io_pre.num) + (x << 2) + 1, 3), output_ptr_right += 3; } else { memcpy(std::begin(output_buffer) + output_ptr_right, std::begin(io_pre.num) + (x << 2), 4), output_ptr_right += 4; } } memcpy(std::begin(output_buffer) + output_ptr_right, std::begin(buf) + i + 4, TWELVE - i); output_ptr_right += TWELVE - i; } inline IO& operator<<(std::ostream& (*pf)(std::ostream&)) { if (pf == static_cast<std::ostream & (*)(std::ostream&)>(std::endl)) write_char('\n'); return *this; } template <typename T_> IO& operator<<(T_&& x) { using T = typename std::remove_cv<typename std::remove_reference<T_>::type>::type; if constexpr (is_custom<T>::value) { write_int(x.get()); } else if constexpr (is_default<T>::value) { if constexpr (is_bool<T>::value) { write_bool(x); } else if constexpr (is_string<T>::value) { write_string(x); } else if constexpr (is_char<T>::value) { write_char(x); } else if constexpr (std::is_integral_v<T>) { write_int(x); } } else if constexpr (is_iterable<T>::value) { using E = decltype(*std::begin(x)); constexpr char sep = needs_newline<E> ? '\n' : ' '; int i = 0; for (const auto& y : x) { if (i++) write_char(sep); operator<<(y); } } else if constexpr (is_applyable<T>::value) { constexpr char sep = (any_needs_newline< T, std::make_index_sequence<std::tuple_size_v<T>>>::value) ? '\n' : ' '; int i = 0; std::apply([this, &sep, &i] (auto const &... y) { (((i++ ? write_char(sep) : void()), this->operator<<(y)), ...); }, x); } else { ss.str(""); ss << x; write_string(ss.str()); } return *this; } template <typename T> IO& operator>>(T& x) { static_assert(is_custom<T>::value or is_default<T>::value or is_iterable<T>::value or is_applyable<T>::value); static_assert(!is_bool<T>::value); if constexpr (is_custom<T>::value) { typename T::internal_value_type y; read_int(y); x = y; } else if constexpr (is_default<T>::value) { if constexpr (is_string<T>::value) { read_string(x); } else if constexpr (is_char<T>::value) { read_char(x); } else if constexpr (std::is_integral_v<T>) { read_int(x); } } else if constexpr (is_iterable<T>::value) { for (auto& y : x) operator>>(y); } else if constexpr (is_applyable<T>::value) { std::apply([this] (auto &... y) { ((this->operator>>(y)), ...); }, x); } return *this; } IO* tie(std::nullptr_t) { return this; } void sync_with_stdio(bool) {}
}; IO io;
#define cin io
#define cout io
#endif
#define int long long
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define FOR(...) GET_MACRO(__VA_ARGS__, _FOR3, _FOR2)(__VA_ARGS__)
#define _FOR2(i, n) for (int i = 0; i < (n); i++)
#define _FOR3(i, a, n) for (int i = (a); i < (n); i++)
#define ALL(arr) arr.begin(), arr.end()
#define SZ(arr) (int)arr.size()
using namespace std; template <typename T, size_t N> struct Arr : public array<T, N> { Arr() = default; Arr(const T& v) : array<T, N>{} { this->fill(v); } Arr(initializer_list<T> i) : array<T, N>{} { copy(i.begin(), i.end(), this->begin()); } template <typename... A, typename = enable_if_t<N == sizeof...(A)>> Arr(A&&... a) : array<T, N>{static_cast<T>(a)...} {} Arr& operator=(const T& v) { this->fill(v); return *this; } Arr& operator+=(const Arr& o) { FOR(i, N)(*this)[i] += o[i]; return *this; } Arr& operator-=(const Arr& o) { FOR(i, N)(*this)[i] -= o[i]; return *this; } Arr operator+(const Arr& o) const { Arr r = *this; r += o; return r; } Arr operator-(const Arr& o) const { Arr r = *this; r -= o; return r; } friend istream& operator>>(istream& in, Arr& a) { FOR(i, N) in >> a[i]; return in; } friend ostream& operator<<(ostream& out, const Arr& a) { FOR(i, N) out << a[i] << " \n"[i == N - 1]; return out; } }; namespace std { template <typename T, size_t N> struct tuple_size<Arr<T, N>> : integral_constant<size_t, N> {}; template <size_t I, typename T, size_t N> struct tuple_element<I, Arr<T, N>> { using type = T; }; template <size_t I, typename T, size_t N> T get(const Arr<T, N>& a) { return a[I]; } }; template <int D, typename T> struct MVec : vector<MVec<D - 1, T>> { MVec() : MVec(0) {} template <typename U, typename... Args> MVec(U n = U(), Args... args) : vector<MVec<D - 1, T>>(n, MVec<D - 1, T>(args...)) {} friend ostream& operator<<(ostream& s, const MVec& v) { for (auto& e : v) s << e; return s; } void Fill(const T& x) { for (auto& v : *this) v.Fill(x); } MVec& operator+=(const MVec& o) { FOR(i, this->size())(*this)[i] += o[i]; return *this; } MVec& operator-=(const MVec& o) { FOR(i, this->size())(*this)[i] -= o[i]; return *this; } MVec operator+(const MVec& o) const { MVec r(this->size()); FOR(i, this->size()) r[i] = (*this)[i] + o[i]; return r; } MVec operator-(const MVec& o) const { MVec r(this->size()); FOR(i, this->size()) r[i] = (*this)[i] - o[i]; return r; } friend istream& operator>>(istream& is, MVec& v) { for (auto& e : v) is >> e; return is; } }; template <typename T> using Vec = MVec<1, T>; template <typename T> ostream& operator<<(ostream& os, const MVec<1, T>& vec) noexcept { FOR(i, SZ(vec)) os << vec[i] << (i + 1 == SZ(vec) ? "\n" : " "); return os; } template <typename T, size_t N> ostream& operator<<(ostream& os, const MVec<1, Arr<T, N>>& vec) noexcept { for (auto& arr : vec) os << arr; return os; } template <typename T> struct MVec<1, T> : vector<T> { MVec() : MVec(0) {} using vector<T>::vector; MVec(initializer_list<T> l) : vector<T>(l) {} void Fill(const T& x) { fill(this->begin(), this->end(), x); } MVec operator+(const MVec& o) const { MVec r(this->size()); FOR(i, this->size()) r[i] = (*this)[i] + o[i]; return r; } MVec operator-(const MVec& o) const { MVec r(this->size()); FOR(i, this->size()) r[i] = (*this)[i] - o[i]; return r; } MVec& operator+=(const MVec& o) { FOR(i, this->size())(*this)[i] += o[i]; return *this; } MVec& operator-=(const MVec& o) { FOR(i, this->size())(*this)[i] -= o[i]; return *this; } friend istream& operator>>(istream& s, MVec& v) { for (auto& e : v) s >> e; return s; } }; template <typename Edge> struct Graph { struct OutgoingEdges { const Graph* g; int l, r; OutgoingEdges(const Graph* g, int l, int r) : g(g), l(l), r(r) {} const Edge* begin() const { return l == r ? nullptr : &g->prepared_edges[l]; } const Edge* end() const { return l == r ? nullptr : &g->prepared_edges[r]; } }; vector<Edge> prepared_edges; vector<pair<int, Edge>> edges; vector<int> start; int N; inline int size() { return N - 1; } Graph() {} Graph(int N) : N(N + 1), start(N + 2) {} void AddEdge(int from, const Edge& e) { edges.emplace_back(from, e); } void Prepare() { for (const auto& e : edges) ++start[e.first + 1]; for (int i = 1; i < N; ++i) start[i] += start[i - 1]; prepared_edges.resize(edges.size() + 1); auto counter = start; for (const auto& e : edges) prepared_edges[counter[e.first]++] = e.second; } OutgoingEdges operator[](int from) const { return (from < 0 || from + 1 >= start.size()) ? OutgoingEdges(this, 0, 0) : OutgoingEdges(this, start[from], start[from + 1]); } }; inline Graph<int> ReadGraph(int N, int M, bool undirected = true, bool one_indexed = true) { Graph<int> adj(N); FOR(i, M) { int a, b; cin >> a >> b; if (one_indexed) { a--; b--; } adj.AddEdge(a, b); if (undirected) { adj.AddEdge(b, a); } } adj.Prepare(); return adj; } inline Graph<Arr<int, 2>> ReadWeightedGraph(int N, int M, bool undirected = true, bool one_indexed = true) { Graph<Arr<int, 2>> adj(N); FOR(i, M) { int a, b, d; cin >> a >> b >> d; if (one_indexed) { a--; b--; } adj.AddEdge(a, { b, d }); if (undirected) { adj.AddEdge(b, { a, d }); } } adj.Prepare(); return adj; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template <class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
const string YN[2] = { "NO\n", "YES\n" };
const int oo = 4000000002701507200LL;

struct Solution {
    const static bool kReadTestCases = 1;

    inline void Solve(int test_case = 0) {
        int N;cin >> N;
        Vec<set<int>> adj(N);
        FOR(i, N - 1) {
            int a, b;cin >> a >> b;a--;b--;
            adj[a].insert(b);
            adj[b].insert(a);
        }
        int comps = 1;
        FOR(iter, 2) {
            int best = -1;
            int best_node = -1;
            FOR(i, N) {
                if (SZ(adj[i]) > best) {
                    best = SZ(adj[i]);
                    best_node = i;
                }
            }
            comps--;
            comps += best;
            for (int x : adj[best_node]) {
                adj[x].erase(best_node);
            }
            adj[best_node].clear();
        }
        cout << comps << endl;
    }
    Solution() {

    }
    inline void Run() {
        cout << fixed << setprecision(20);
        int num_test_cases = 1;
        if (kReadTestCases) cin >> num_test_cases;
        FOR(test_case, 1, num_test_cases + 1) Solve(test_case);
    }
};
#ifndef LOCAL_STRESS_TEST
Solution solution;int32_t main() { solution.Run(); }
#else
#include "./_run_folder/_StressTester.h"
int32_t main() { StressTester stress_tester;stress_tester.Run(__FILE__); }
#endif

Code 2:
#include <bits/stdc++.h>
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifndef LOCAL_STRESS_TEST
struct IOPre { static constexpr int TEN = 10, SZ = TEN * TEN * TEN * TEN; std::array<char, 4 * SZ> num; constexpr IOPre() : num{} { for (int i = 0; i < SZ; i++) { int n = i; for (int j = 3; j >= 0; j--) { num[i * 4 + j] = static_cast<char>(n % TEN + '0'); n /= TEN; } } } }; struct IO {
#if !HAVE_DECL_FREAD_UNLOCKED
#define fread_unlocked fread
#endif
#if !HAVE_DECL_FWRITE_UNLOCKED
#define fwrite_unlocked fwrite
#endif
    static constexpr int SZ = 1 << 17, LEN = 32, TEN = 10, HUNDRED = TEN * TEN, THOUSAND = HUNDRED * TEN, TENTHOUSAND = THOUSAND * TEN, MAGIC_MULTIPLY = 205, MAGIC_SHIFT = 11, MASK = 15, TWELVE = 12, SIXTEEN = 16; static constexpr IOPre io_pre = {}; std::array<char, SZ> input_buffer, output_buffer; int input_ptr_left, input_ptr_right, output_ptr_right; std::stringstream ss; IO() : input_buffer{}, output_buffer{}, input_ptr_left{}, input_ptr_right{}, output_ptr_right{} {} IO(const IO&) = delete; IO(IO&&) = delete; IO& operator=(const IO&) = delete; IO& operator=(IO&&) = delete; ~IO() { flush(); } template <class T> struct is_char { static constexpr bool value = std::is_same_v<T, char>; }; template <class T> struct is_bool { static constexpr bool value = std::is_same_v<T, bool>; }; template <class T> struct is_string { static constexpr bool value = std::is_same_v<T, std::string> || std::is_same_v<T, const char*> || std::is_same_v<T, char*>; }; template <class T, class D = void> struct is_custom { static constexpr bool value = false; }; template <class T> struct is_custom<T, std::void_t<typename T::internal_value_type>> { static constexpr bool value = true; }; template <class T> struct is_default { static constexpr bool value = is_char<T>::value || is_bool<T>::value || is_string<T>::value || std::is_integral_v<T>; }; template <class T, class D = void> struct is_iterable { static constexpr bool value = false; }; template <class T> struct is_iterable< T, typename std::void_t<decltype(std::begin(std::declval<T>()))>> { static constexpr bool value = true; }; template <class T, class D = void, class E = void> struct is_applyable { static constexpr bool value = false; }; template <class T> struct is_applyable<T, std::void_t<typename std::tuple_size<T>::type>, std::void_t<decltype(std::get<0>(std::declval<T>()))>> { static constexpr bool value = true; }; template <class T> static constexpr bool needs_newline = (is_iterable<T>::value || is_applyable<T>::value) && (!is_default<T>::value); template <typename T, typename U> struct any_needs_newline { static constexpr bool value = false; }; template <typename T> struct any_needs_newline<T, std::index_sequence<>> { static constexpr bool value = false; }; template <typename T, std::size_t I, std::size_t... Is> struct any_needs_newline<T, std::index_sequence<I, Is...>> { static constexpr bool value = needs_newline<decltype(std::get<I>(std::declval<T>()))> || any_needs_newline<T, std::index_sequence<Is...>>::value; }; inline void load() { memmove(std::begin(input_buffer), std::begin(input_buffer) + input_ptr_left, input_ptr_right - input_ptr_left); input_ptr_right = input_ptr_right - input_ptr_left + static_cast<int>(fread_unlocked(std::begin(input_buffer) + input_ptr_right - input_ptr_left, 1, SZ - input_ptr_right + input_ptr_left, stdin)); input_ptr_left = 0; } inline void read_char(char& c) { if (input_ptr_left + LEN > input_ptr_right) load(); c = input_buffer[input_ptr_left++]; } inline void read_string(std::string& x) { char c; while (read_char(c), c < '!') continue; x = c; while (read_char(c), c >= '!') x += c; } template <class T> inline std::enable_if_t<std::is_integral_v<T>, void> read_int(T& x) { if (input_ptr_left + LEN > input_ptr_right) load(); char c = 0; do c = input_buffer[input_ptr_left++]; while (c < '-'); [[maybe_unused]] bool minus = false; if constexpr (std::is_signed<T>::value == true) if (c == '-') minus = true, c = input_buffer[input_ptr_left++]; x = 0; while (c >= '0') x = x * TEN + (c & MASK), c = input_buffer[input_ptr_left++]; if constexpr (std::is_signed<T>::value == true) if (minus) x = -x; } inline void skip_space() { if (input_ptr_left + LEN > input_ptr_right) load(); while (input_buffer[input_ptr_left] <= ' ') input_ptr_left++; } inline void flush() { fwrite_unlocked(std::begin(output_buffer), 1, output_ptr_right, stdout); output_ptr_right = 0; } inline void write_char(char c) { if (output_ptr_right > SZ - LEN) flush(); output_buffer[output_ptr_right++] = c; } inline void write_bool(bool b) { if (output_ptr_right > SZ - LEN) flush(); output_buffer[output_ptr_right++] = b ? '1' : '0'; } inline void write_string(const std::string& s) { for (auto x : s) write_char(x); } inline void write_string(const char* s) { while (*s) write_char(*s++); } inline void write_string(char* s) { while (*s) write_char(*s++); } template <typename T> inline std::enable_if_t<std::is_integral_v<T>, void> write_int(T x) { if (output_ptr_right > SZ - LEN) flush(); if (!x) { output_buffer[output_ptr_right++] = '0'; return; } if constexpr (std::is_signed<T>::value == true) if (x < 0) output_buffer[output_ptr_right++] = '-', x = -x; int i = TWELVE; std::array<char, SIXTEEN> buf{}; while (x >= TENTHOUSAND) { memcpy(std::begin(buf) + i, std::begin(io_pre.num) + (x % TENTHOUSAND) * 4, 4); x /= TENTHOUSAND; i -= 4; } if (x < HUNDRED) { if (x < TEN) { output_buffer[output_ptr_right++] = static_cast<char>('0' + x); } else { std::uint32_t q = (static_cast<std::uint32_t>(x) * MAGIC_MULTIPLY) >> MAGIC_SHIFT; std::uint32_t r = static_cast<std::uint32_t>(x) - q * TEN; output_buffer[output_ptr_right] = static_cast<char>('0' + q); output_buffer[output_ptr_right + 1] = static_cast<char>('0' + r); output_ptr_right += 2; } } else { if (x < THOUSAND) { memcpy(std::begin(output_buffer) + output_ptr_right, std::begin(io_pre.num) + (x << 2) + 1, 3), output_ptr_right += 3; } else { memcpy(std::begin(output_buffer) + output_ptr_right, std::begin(io_pre.num) + (x << 2), 4), output_ptr_right += 4; } } memcpy(std::begin(output_buffer) + output_ptr_right, std::begin(buf) + i + 4, TWELVE - i); output_ptr_right += TWELVE - i; } inline IO& operator<<(std::ostream& (*pf)(std::ostream&)) { if (pf == static_cast<std::ostream & (*)(std::ostream&)>(std::endl)) write_char('\n'); return *this; } template <typename T_> IO& operator<<(T_&& x) { using T = typename std::remove_cv<typename std::remove_reference<T_>::type>::type; if constexpr (is_custom<T>::value) { write_int(x.get()); } else if constexpr (is_default<T>::value) { if constexpr (is_bool<T>::value) { write_bool(x); } else if constexpr (is_string<T>::value) { write_string(x); } else if constexpr (is_char<T>::value) { write_char(x); } else if constexpr (std::is_integral_v<T>) { write_int(x); } } else if constexpr (is_iterable<T>::value) { using E = decltype(*std::begin(x)); constexpr char sep = needs_newline<E> ? '\n' : ' '; int i = 0; for (const auto& y : x) { if (i++) write_char(sep); operator<<(y); } } else if constexpr (is_applyable<T>::value) { constexpr char sep = (any_needs_newline< T, std::make_index_sequence<std::tuple_size_v<T>>>::value) ? '\n' : ' '; int i = 0; std::apply([this, &sep, &i] (auto const &... y) { (((i++ ? write_char(sep) : void()), this->operator<<(y)), ...); }, x); } else { ss.str(""); ss << x; write_string(ss.str()); } return *this; } template <typename T> IO& operator>>(T& x) { static_assert(is_custom<T>::value or is_default<T>::value or is_iterable<T>::value or is_applyable<T>::value); static_assert(!is_bool<T>::value); if constexpr (is_custom<T>::value) { typename T::internal_value_type y; read_int(y); x = y; } else if constexpr (is_default<T>::value) { if constexpr (is_string<T>::value) { read_string(x); } else if constexpr (is_char<T>::value) { read_char(x); } else if constexpr (std::is_integral_v<T>) { read_int(x); } } else if constexpr (is_iterable<T>::value) { for (auto& y : x) operator>>(y); } else if constexpr (is_applyable<T>::value) { std::apply([this] (auto &... y) { ((this->operator>>(y)), ...); }, x); } return *this; } IO* tie(std::nullptr_t) { return this; } void sync_with_stdio(bool) {}
}; IO io;
#define cin io
#define cout io
#endif
#define int long long
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define FOR(...) GET_MACRO(__VA_ARGS__, _FOR3, _FOR2)(__VA_ARGS__)
#define _FOR2(i, n) for (int i = 0; i < (n); i++)
#define _FOR3(i, a, n) for (int i = (a); i < (n); i++)
#define ALL(arr) arr.begin(), arr.end()
#define SZ(arr) (int)arr.size()
using namespace std; template <typename T, size_t N> struct Arr : public array<T, N> { Arr() = default; Arr(const T& v) : array<T, N>{} { this->fill(v); } Arr(initializer_list<T> i) : array<T, N>{} { copy(i.begin(), i.end(), this->begin()); } template <typename... A, typename = enable_if_t<N == sizeof...(A)>> Arr(A&&... a) : array<T, N>{static_cast<T>(a)...} {} Arr& operator=(const T& v) { this->fill(v); return *this; } Arr& operator+=(const Arr& o) { FOR(i, N)(*this)[i] += o[i]; return *this; } Arr& operator-=(const Arr& o) { FOR(i, N)(*this)[i] -= o[i]; return *this; } Arr operator+(const Arr& o) const { Arr r = *this; r += o; return r; } Arr operator-(const Arr& o) const { Arr r = *this; r -= o; return r; } friend istream& operator>>(istream& in, Arr& a) { FOR(i, N) in >> a[i]; return in; } friend ostream& operator<<(ostream& out, const Arr& a) { FOR(i, N) out << a[i] << " \n"[i == N - 1]; return out; } }; namespace std { template <typename T, size_t N> struct tuple_size<Arr<T, N>> : integral_constant<size_t, N> {}; template <size_t I, typename T, size_t N> struct tuple_element<I, Arr<T, N>> { using type = T; }; template <size_t I, typename T, size_t N> T get(const Arr<T, N>& a) { return a[I]; } }; template <int D, typename T> struct MVec : vector<MVec<D - 1, T>> { MVec() : MVec(0) {} template <typename U, typename... Args> MVec(U n = U(), Args... args) : vector<MVec<D - 1, T>>(n, MVec<D - 1, T>(args...)) {} friend ostream& operator<<(ostream& s, const MVec& v) { for (auto& e : v) s << e; return s; } void Fill(const T& x) { for (auto& v : *this) v.Fill(x); } MVec& operator+=(const MVec& o) { FOR(i, this->size())(*this)[i] += o[i]; return *this; } MVec& operator-=(const MVec& o) { FOR(i, this->size())(*this)[i] -= o[i]; return *this; } MVec operator+(const MVec& o) const { MVec r(this->size()); FOR(i, this->size()) r[i] = (*this)[i] + o[i]; return r; } MVec operator-(const MVec& o) const { MVec r(this->size()); FOR(i, this->size()) r[i] = (*this)[i] - o[i]; return r; } friend istream& operator>>(istream& is, MVec& v) { for (auto& e : v) is >> e; return is; } }; template <typename T> using Vec = MVec<1, T>; template <typename T> ostream& operator<<(ostream& os, const MVec<1, T>& vec) noexcept { FOR(i, SZ(vec)) os << vec[i] << (i + 1 == SZ(vec) ? "\n" : " "); return os; } template <typename T, size_t N> ostream& operator<<(ostream& os, const MVec<1, Arr<T, N>>& vec) noexcept { for (auto& arr : vec) os << arr; return os; } template <typename T> struct MVec<1, T> : vector<T> { MVec() : MVec(0) {} using vector<T>::vector; MVec(initializer_list<T> l) : vector<T>(l) {} void Fill(const T& x) { fill(this->begin(), this->end(), x); } MVec operator+(const MVec& o) const { MVec r(this->size()); FOR(i, this->size()) r[i] = (*this)[i] + o[i]; return r; } MVec operator-(const MVec& o) const { MVec r(this->size()); FOR(i, this->size()) r[i] = (*this)[i] - o[i]; return r; } MVec& operator+=(const MVec& o) { FOR(i, this->size())(*this)[i] += o[i]; return *this; } MVec& operator-=(const MVec& o) { FOR(i, this->size())(*this)[i] -= o[i]; return *this; } friend istream& operator>>(istream& s, MVec& v) { for (auto& e : v) s >> e; return s; } }; template <typename Edge> struct Graph { struct OutgoingEdges { const Graph* g; int l, r; OutgoingEdges(const Graph* g, int l, int r) : g(g), l(l), r(r) {} const Edge* begin() const { return l == r ? nullptr : &g->prepared_edges[l]; } const Edge* end() const { return l == r ? nullptr : &g->prepared_edges[r]; } }; vector<Edge> prepared_edges; vector<pair<int, Edge>> edges; vector<int> start; int N; inline int size() { return N - 1; } Graph() {} Graph(int N) : N(N + 1), start(N + 2) {} void AddEdge(int from, const Edge& e) { edges.emplace_back(from, e); } void Prepare() { for (const auto& e : edges) ++start[e.first + 1]; for (int i = 1; i < N; ++i) start[i] += start[i - 1]; prepared_edges.resize(edges.size() + 1); auto counter = start; for (const auto& e : edges) prepared_edges[counter[e.first]++] = e.second; } OutgoingEdges operator[](int from) const { return (from < 0 || from + 1 >= start.size()) ? OutgoingEdges(this, 0, 0) : OutgoingEdges(this, start[from], start[from + 1]); } }; inline Graph<int> ReadGraph(int N, int M, bool undirected = true, bool one_indexed = true) { Graph<int> adj(N); FOR(i, M) { int a, b; cin >> a >> b; if (one_indexed) { a--; b--; } adj.AddEdge(a, b); if (undirected) { adj.AddEdge(b, a); } } adj.Prepare(); return adj; } inline Graph<Arr<int, 2>> ReadWeightedGraph(int N, int M, bool undirected = true, bool one_indexed = true) { Graph<Arr<int, 2>> adj(N); FOR(i, M) { int a, b, d; cin >> a >> b >> d; if (one_indexed) { a--; b--; } adj.AddEdge(a, { b, d }); if (undirected) { adj.AddEdge(b, { a, d }); } } adj.Prepare(); return adj; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template <class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
const string YN[2] = { "NO\n", "YES\n" };
const int oo = 4000000002701507200LL;

struct Solution {
    const static bool kReadTestCases = 1;

    inline void Solve(int test_case = 0) {
        int N;cin >> N;
        Vec<set<int>> adj(N);
        FOR(i, N - 1) {
            int a, b;cin >> a >> b;a--;b--;
            adj[a].insert(b);
            adj[b].insert(a);
        }
        multiset<int> sizes;
        int best = -oo;
        FOR(i, N) {
            sizes.insert(SZ(adj[i]));
        }
        FOR(i, N) {
            int ans = -1;
            ans += SZ(adj[i]);
            sizes.erase(sizes.find(SZ(adj[i])));
            for (int j : adj[i]) {
                sizes.erase(sizes.find(SZ(adj[j])));
                sizes.insert(SZ(adj[j]) - 1);
            }
            ans += *sizes.rbegin();
            sizes.insert(SZ(adj[i]));
            for (int j : adj[i]) {
                sizes.insert(SZ(adj[j]));
                sizes.erase(sizes.find(SZ(adj[j]) - 1));
            }
            ckmax(best, ans);
        }
        cout << best << endl;

    }
    Solution() {

    }
    inline void Run() {
        cout << fixed << setprecision(20);
        int num_test_cases = 1;
        if (kReadTestCases) cin >> num_test_cases;
        FOR(test_case, 1, num_test_cases + 1) Solve(test_case);
    }
};
#ifndef LOCAL_STRESS_TEST
Solution solution;int32_t main() { solution.Run(); }
#else
#include "./_run_folder/_StressTester.h"
int32_t main() { StressTester stress_tester;stress_tester.Run(__FILE__); }
#endif

Answer: 2