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

Solution 1 (runtime: 546 ms):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
#include <unordered_set>
using namespace std;

// Vector declaration shorthands
#define VI vector<int>                       // Vector of integers
#define VLL vector<long long>                // Vector of long long
#define VD vector<double>                    // Vector of doubles
#define VPII vector<pair<int, int>>          // Vector of pair<int, int>
#define VVI vector<vector<int>>              // 2D vector of integers
#define VVLL vector<vector<long long>>       // 2D vector of long long
#define VVD vector<vector<double>>           // 2D vector of doubles
#define VVPII vector<vector<pair<int, int>>> // 2D vector of pair<int, int>
#define VB vector<bool>                      // Vector of booleans
#define VS vector<string>                    // Vector of strings

// Vector shorthands
#define PB push_back
#define ALL(v) (v).begin(), (v).end() // For sorting and algorithms
#define SZ(v) int((v).size())         // Get vector size

// Fast loop shorthands
#define FOR(i, a, b, c) for (int i = (a); i < (b); i += c)  // For loops with range [a, b)
#define ROF(i, a, b, c) for (int i = (a); i >= (b); i -= c) // Reverse for loops from a to b
#define EACH(x, v) for (auto &x : (v))                      // Range-based for loop shorthand

// Debugging shorthands
template <class Fun>
class y_combinator_result
{
    Fun fun_;

public:
    template <class T>
    explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}
    template <class... Args>
    decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); }
};
template <class Fun>
decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template <typename... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t)
{
    os << '(';
    apply([&os](const Args &...args)
          { size_t n = 0; ((os << args << (++n != sizeof...(Args) ? ", " : "")), ...); }, t);
    return os << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
    os << '{';
    string sep;
    for (const T &x : v)
        os << sep << x, sep = ", ";
    return os << '}';
}

void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
    cerr << ' ' << H;
    dbg_out(T...);
}
#ifdef NEAL_DEBUG
#define dbg(...) cerr << '[' << __FILE__ << ':' << __LINE__ << "] (" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

void run_case()
{
    int n;
    cin >> n;
    vector<vector<int>> a(n);
    FOR(i, 0, n - 1, 1)
    {
        int u, v;
        cin >> u >> v;
        --u, --v;
        a[u].push_back(v);
        a[v].push_back(u);
    }
    int ans = 0;
    for (int i = 0; i < n; ++i)
    {
        for (int j : a[i])
        {
            ans = max(ans, (int)(a[i].size() + a[j].size()) - 2);
        }
    }
    multiset<int> S;
    for (int i = 0; i < n; ++i)
    {
        S.insert(a[i].size());
    }
    for (int i = 0; i < n; ++i)
    {
        S.erase(S.find(a[i].size()));
        for (int j : a[i])
        {
            S.erase(S.find(a[j].size()));
        }
        if (!S.empty())
        {
            ans = max(ans, (int)a[i].size() + *S.rbegin() - 1);
        }
        S.insert(a[i].size());
        for (int j : a[i])
        {
            S.insert(a[j].size());
        }
    }
    cout << ans << "\n";
}

int main()
{
    ios::sync_with_stdio(false);
#ifndef NEAL_DEBUG
    cin.tie(nullptr);
#endif

    int tests;
    cin >> tests;

    while (tests-- > 0)
        run_case();
}

Solution 2 (runtime: 530 ms):
#include <bits/stdc++.h>
using namespace std;

void sol() {
    int n, output = 1;
    cin >> n;
    vector <int> a(n, 0);
    vector<vector<int>> p(n);
    for(int i = 0; i < n - 1; i++) {
    	int u, v;
    	cin >> u >> v;
    	a[u - 1]++;
    	a[v - 1]++;
    	p[u - 1].push_back(v - 1);
    	p[v - 1].push_back(u - 1);
    }
    int mx = 0, mxcnt = 0;
    for(int i = 0; i < n; i++) {
        if(a[i] > mx) {
            mx = a[i]; mxcnt = 1;
        }
        else if(a[i] == mx) mxcnt++;
    }
    if(mxcnt > 2) {
        cout << 2 * mx - 1 << endl;
        return;
    }
    int max = 0;
    for(int i = 0; i < n; i++) {
        if(a[i] > a[max]) max = i;
    }
    output += (a[max] - 1);
    a[max] = -1;
    for(int i = 0; i < p[max].size(); i++) {
        a[p[max][i]]--;
    }
    max = 0;
    for(int i = 0; i < n; i++) {
	if(a[i] > a[max]) max = i;
    }
    output += (a[max] - 1);
    cout << output << endl;
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        sol();
    }
    return 0;
}