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

Solution 1 (runtime: 546 ms):
#include <bits/stdc++.h>
using namespace std;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
#define int long long
#define rep(i, a, b) for (int (i) = (a); (i) <= (b); ++i)
#define rep2(i, a, b) for (int (i) = (a); (i) >= (b); --i)
#define debug(x) std::cout << #x << " : " << x << "\n";
#define debug2(x,y) std::cout << #x << " : " << x << " " << #y << " : " << y << "\n";

const int N = 1e6;
void solve()
{
    int n;
    cin >> n;
    auto cal = [&](int u, int v)->int {
        return u * 1e6 + v;
    };
    set<int> ss;
    vector<vector<int>> adj(n+1);
    rep(i, 1, n - 1) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        ss.emplace(cal(u, v));
        ss.emplace(cal(v, u));
    }
    vector<vector<int>> nums(n+1);
    rep(i, 1, n) nums[adj[i].size()].push_back(i);
    int mx1 = 0, mx2 = 0;
    for (int i = n; i >= 1; i--)
    {
        if (!mx1) {
            if (nums[i].size()) mx1 = i;
        }
        else {
            if (!mx2 && nums[i].size()) {
                mx2 = i;
                break;
            }
        } 
    }
    int ans = 1;
    if (nums[mx1].size() >= 2) {
        ans += 2 * (mx1 - 1);
        // debug(ans)
        int f = 1;
        // check 是否有不直接相连的点
        for (auto u : nums[mx1])
        {
            for (auto v : nums[mx1])
            {
                if (u == v) continue;
                // debug2(u, v)
                if (!ss.count(cal(u, v))) 
                {
                    f = 0;
                    break;
                }
            }
        }
        if (f) ans--;
    }
    else {
        if (nums[mx2].size() == 1) {
            ans += (mx1 - 1);
            ans += (mx2 - 1);
            int u = nums[mx1][0], v = nums[mx2][0];
            if (ss.count(cal(u, v))) ans--;
        }
        else {
            ans += (mx1 - 1);
            ans += (mx2 - 1);
            int f = 1;
            // check 是否有不直接相连的点
            int v = nums[mx1][0];
            for (auto u : nums[mx2])
            {
                if (!ss.count(cal(u, v))) {
                    f = 0;
                    break;
                }
            }
            if (f) ans--;
        }
    }
    cout << ans << "\n";
}
signed main()
{
    std::ios::sync_with_stdio(false);
    // cout<<std::fixed<<std::setprecision(10);
    std::cin.tie(0);
    int t__ = 1;
    cin >> t__;
    while (t__--)
        solve();
    return 0;
}

Solution 2 (runtime: 1296 ms):
#include<bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        set<pair<int, int> > edges;
        map<int, int> degree;
        for(int i=0; i<n-1; i++) {
            int u, v;
            cin >> u >> v;
            edges.insert({u, v});
            edges.insert({v, u});
            degree[u]++;
            degree[v]++;
        }
        int ans = 0;
        
        for(auto it: edges) {
            int deg1 = degree[it.first];
            int deg2 = degree[it.second];
            ans = max(deg1 + deg2 - 2, ans);
        }
        
        vector<pair<int, int>> v;
        for(auto it: degree) {
            v.push_back({it.second, it.first});
        }
        
        sort(v.begin(), v.end(), greater<pair<int, int>>());
        // for(auto it: edges) cout << it.first << " " << it.second << "\n";
        for(int i=0; i<n; i++) {
            for(int j=i+1; j<n; j++) {
                if(edges.find({v[i].second, v[j].second}) == edges.end()) {
                    // cout << "enter ";
                    int deg1 = v[i].first;
                    int deg2 = v[j].first;
                    ans = max(deg1 + deg2 - 1, ans);
                    break;
                }
            }
        }
        
        cout << ans << "\n";
        
    }
    
    return 0;
}