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

Solution 1 (runtime: 436 ms):
#include<bits/stdc++.h>
using namespace std;
vector<int> num;
int cmp(int a,int b)
{
    if(num[a]>num[b]) return 1;
    else return 0;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        num.resize(n+1);
        for(int i=1;i<=n;i++)
        {
            num[i] = 0;
        }
        vector<int> big(n+1);
        for(int i=1;i<=n;i++)
        {
            big[i] = i;
        }
        map<pair<int,int>,int> mp;
        for(int i=1;i<n;i++)
        {
            int u,v;
            cin >> u >> v;
            mp[{v,u}]++;
            mp[{u,v}]++;            
            num[u]++;
            num[v]++;
        }
        int ans = 1;
        sort(big.begin()+1,big.end(),cmp);
        if(num[big[1]]!=num[big[2]])
        {
            // cout <<"num "<< num[big[1]] << ' ' << num[big[2]] <<'\n';
            ans += num[big[1]]+num[big[2]]-3;
            int i = 2;
            while(i<=n&&num[big[i]]==num[big[2]])
            {
                if(!mp[{big[i],big[1]}])
                {
                    ans++;
                    break;
                }
                i++;
            }
            cout << ans <<'\n';
        }
        else
        {
            ans += 2*num[big[1]]-3;
            int k = 2;
            while(k<=n&&num[big[k]]==num[big[1]])
            {
                k++;
            }
            k--;
            if(k>=3) ans++;
            else
            {
                if(mp[{big[1],big[2]}]==0) ans++;
            }
            // int edge = 0;
            // for(auto& [a,_] : mp)
            // {
            //     if(a.first<=k&&a.second<=k) edge++;
            // }
        
            // if()
            cout << ans << '\n';
        }
    }
    return 0;
}

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

int n;

/*
First approach: select 2 vertices that have the most number of edge to a lonely vertext (lonely means degree 1)
-> NOT WORKING because of this: https://codeforces.com/blog/entry/138593?#comment-1240159



Second approach: 
di+dj−1 if i and j are not adjacent;
di+dj−2 if i and j are adjacent, because removing i will decrease deg of j by 1
.*/

 
void solve(){
    cin >> n;
    vector<int> deg(n);
    vector<unordered_set<int>> adj(n);
    vector<vector<int>> edge;
    for (int i = 0; i < n-1; i++){
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        deg[u]++;
        deg[v]++;
        adj[u].insert(v);
        adj[v].insert(u);
        edge.push_back({u, v});
    }

    if (n == 2){
        cout << 0;
        return;
    }

    vector<pair<int,int>> vec;
    for (int i = 0; i < n; i++){
        vec.push_back({deg[i], i});
    }
    sort(vec.begin(), vec.end(), [](auto &a, auto &b){
        return a.first > b.first;
    });

    int ans = 0;

    // u and v are not adjacent
    // iterate u -> get v so that deg[v] largest and not connected to u
    for (int i = 0; i < n; i++){
        int u = vec[i].second;

        for (int j = i+1; j < n; j++){
            int v = vec[j].second;

            if (adj[u].find(v) == adj[u].end()){
                ans = max(ans, deg[u] + deg[v] - 1);
                break;
            }
        }
    }

    // u and v are adjacent
    for (vector<int> e:edge){
        int u = e[0], v = e[1];

        ans = max(ans, deg[u] + deg[v]-2);
    }
    cout << ans;

}
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1; 
    cin >> t; 
    while (t--){ 
        solve();
        cout << '\n';
    } 
}