Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 5):
#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()
{
    long long n;
    cin >> n;
    VVI adj(n + 1);
    VI degree(n + 1, 0);
    for (int i = 0; i < n - 1; i++)
    {
        long long u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        degree[u]++;
        degree[v]++;
    }
    VI pts(n);
    FOR(i, 1, n + 1, 1)
    {
        pts[i - 1] = i;
    }
    sort(pts.begin(), pts.end(), [&](int a, int b)
         { return degree[a] > degree[b]; });
    long long K = min(n, 100000LL);
    vector<unordered_set<int>> neigh(K);
    FOR(i, 0, K, 1)
    {
        long long v = pts[i];
        neigh[i] = unordered_set<int>(ALL(adj[v]));
    }

    long long ans = 0;
    FOR(i, 0, K, 1)
    {
        long long pt1 = pts[i];
        long long d1 = degree[pt1];
        FOR(j, 0, K, 1)
        {
            if (i == j)
                continue;
            long long pt2 = pts[j];
            long long d2 = degree[pt2];
            bool flag = (neigh[i].find(pt2) != neigh[i].end());
            long long curr = flag ? (d1 + d2 - 2) : (d1 + d2 - 1);
            ans = max(ans, curr);
        }
    }
    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();
}

Submission (verdict: Time limit exceeded on test 5):
#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()
{
    long long n;
    cin >> n;
    VVI adj(n + 1);
    VI degree(n + 1, 0);
    for (int i = 0; i < n - 1; i++)
    {
        long long u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        degree[u]++;
        degree[v]++;
    }
    VI pts(n);
    FOR(i, 1, n + 1, 1)
    {
        pts[i - 1] = i;
    }
    sort(pts.begin(), pts.end(), [&](int a, int b)
         { return degree[a] > degree[b]; });
    long long K = min(n, 10000LL);
    vector<unordered_set<int>> neigh(K);
    FOR(i, 0, K, 1)
    {
        long long v = pts[i];
        neigh[i] = unordered_set<int>(ALL(adj[v]));
    }

    long long ans = 0;
    FOR(i, 0, K, 1)
    {
        long long pt1 = pts[i];
        long long d1 = degree[pt1];
        FOR(j, 0, K, 1)
        {
            if (i == j)
                continue;
            long long pt2 = pts[j];
            long long d2 = degree[pt2];
            bool flag = (neigh[i].find(pt2) != neigh[i].end());
            long long curr = flag ? (d1 + d2 - 2) : (d1 + d2 - 1);
            ans = max(ans, curr);
        }
    }
    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();
}