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

Solution 1 (runtime: 765 ms):
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;

vector<vector<int>> g;

void solve() {
    int n;
    cin >> n;
    g.clear(), g.resize(n + 1);
    vector<pii> e(n - 1);
    vector<int> d(n + 1);

    for (auto& [u, v] : e) {
        cin >> u >> v;
        d[u] += 1, d[v] += 1;
        g[u].push_back(v), g[v].push_back(u);
    }
    multiset<int> ds;
    for (int i = 1; i <= n; i++) ds.insert(d[i]);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        ds.erase(ds.find(d[i]));
        for (auto& nxt : g[i]) ds.erase(ds.find(d[nxt])), ds.insert(d[nxt] - 1);

        ans = max(ans, d[i] + (*prev(ds.end()) - 1));
        ds.insert(d[i]);
        for (auto& nxt : g[i]) ds.erase(ds.find(d[nxt] - 1)), ds.insert(d[nxt]);
    }
    cout << ans << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int T = 1;
    cin >> T;
    while (T--) solve();
    return 0;
}

Solution 2 (runtime: 1140 ms):
// Created by Rishi Sharma (@rishisharmapro) on 08-02-2025 at 13:32
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>

using namespace std;

#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())

const ll MOD = 1e9 + 7;
const ll INF = 1e18;

void solve() {
    int n, maxComponents = 0;
    cin >> n;

    map<int , vector<int>> adjList;
    map<int, int> inDegree;
    int maxInDegree = 0;
    for(int i = 0; i < n-1; i++) {
        int u, v;
        cin >> u >> v;
        adjList[u].pb(v);
        adjList[v].pb(u);
        inDegree[u]++;
        inDegree[v]++;
        maxInDegree = max(maxInDegree, max(inDegree[u], inDegree[v]));
    }

    if(n ==  2) {
        cout << 0 << endl;
        return;
    }
    
    // calculating the nodes with maximum in degree
    set<int> nodes;
    for(auto i:inDegree) {
        if(i.second == maxInDegree) {
            nodes.insert(i.first);
        }
    }
    map<int, int> inDegreeAdjacent;
    // now finding the nodes with maximum in degree but there adjacent having minimum in degree
    for(auto i:nodes) {
        int degree = 0;
        for(auto j:adjList[i]) {
            if(inDegree[j] == maxInDegree)
                degree += inDegree[j];
        }
        inDegreeAdjacent[degree] = i;
    }

    int maxIndegreeNode = 0;
    maxIndegreeNode = inDegreeAdjacent.begin()->second;
    maxComponents = inDegree[maxIndegreeNode];
    // removing the node with maximum in degree from inDegree
    for(auto i:adjList[maxIndegreeNode]) {
        inDegree[i]--;
    }
    inDegree.erase(maxIndegreeNode);
    // cout << endl << maxIndegreeNode << " " << maxComponents << endl;
    
    int ans = maxComponents;
    maxInDegree = 0;
    int node = maxIndegreeNode;
    maxIndegreeNode = 0;
    for(auto i:inDegree) {
        if(i.second > maxInDegree) {
            maxInDegree = i.second;
            maxIndegreeNode = i.first;
        }
    }
    // the node is directly connected to the removed node
    // if(find(all(adjList[node]), maxIndegreeNode) != adjList[node].end()) {
    //     // if(maxInDegree > 1) {
    //     //     ans += maxInDegree - 2;
    //     // }
    //     // else {
    //     //     ans += maxInDegree - 1;
    //     // }
    //     // auto it = find(all(adjList[node]), maxIndegreeNode);
    //     // cout << maxIndegreeNode << " " << maxInDegree << endl;
    // }
    // // the node is not directly connected to the removed node
    // else {
    //     ans += maxInDegree - 1;
    // }

    ans += maxInDegree - 1;
    cout << ans << endl;
}

int main() {
    // problem: https://codeforces.com/contest/2063/problem/C
    fast_io;
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}