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

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

void solve() {
    int n;
    cin >> n;

    vector<int> indegree(n + 1, 0);
    set<pair<int, int>> st;
    int max_deg = 0;

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;

        indegree[u]++;
        indegree[v]++;
        max_deg = max({max_deg, indegree[u], indegree[v]});

        st.insert({u, v});
        st.insert({v, u});
    }

    vector<int> max_deg_nodes;
    for (int i = 1; i <= n; i++) {
        if (indegree[i] == max_deg) {
            max_deg_nodes.push_back(i);
        }
        if ((int)max_deg_nodes.size() == 2) break;
    }

    int ans = 0;
    for (int node : max_deg_nodes) {
        for (int i = 1; i <= n; i++) {
            if (i == node) continue;

            int count = max_deg + indegree[i] - 1;
            if (st.find({node, i}) != st.end()) {
                count--;
            }

            ans = max(ans, count);
        }
    }

    cout << ans << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t;
    cin >> t;

    while (t--) {
        solve();
    }
    return 0;
}


Solution 2 (runtime: 452 ms):
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cmath>
#include <queue>
#include <algorithm>
#include <sstream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <climits>

using namespace std;

#define pb push_back
#define int long long
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define LOG(n) 31 - __builtin_clz(n)
#define nl  "\n"
#define ok  cout<<"OK\n"
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define free freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define ff first
#define ss second
#define vi vector<int>
#define vvi vector<vector<int> >
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef pair<int,int> pii;
typedef long double ld;
const long long INF = 1ll << 32;
const long long MAX_N = 1e12 + 5;
const long long MOD = 1e9 + 7;
const long long N = 9999999;
long double pi = 3.1415926535;

void show(vector<int> a) {for(auto it : a) cout<<it<<","; cout<<nl;}
int gcd(int a, int b){if(a == 0) return b; if(b == 0) return a;int r = min(a, b);while(r>0){if(a%r==0 && b%r==0){break;}r--;}return r;}

int sq(int n){if(n==0)return 0LL;int l=1,r=n;while(l<=r){int m=l+(r-l)/2;int p=m*m;if(p==n)return m;if(p<n)l=m+1;else r=m-1;}return l;}
int I;

void solve() {
    int n;
    cin>>n;
    
    vector<vector<int> > adj(n + 1);
    vector<int> deg(n + 1, 0);
    int element = -1, mxDegree = 0;
    for (int i = 0 ; i < n - 1 ; i++) {
        int u, v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        deg[u]++;
        deg[v]++;
    }
    
    multiset<int> maxDegree;
    for (int i = 1 ; i <= n ; i++) {
        maxDegree.insert(deg[i]);
        if (mxDegree < deg[i]) {
            mxDegree = deg[i];
            element = i;
        }
    }
    int maxComponents = 0;
    maxDegree.erase(--maxDegree.end());
    for (int i = 1 ; i <= n ; i++) {
        if (deg[i] != mxDegree) continue;
        
        for (int node : adj[i]) {
            maxDegree.erase(maxDegree.find(deg[node]));
            maxDegree.insert(deg[node] - 1);
        }
        maxComponents = max(maxComponents, mxDegree + *maxDegree.rbegin() - 1);
        for (int node : adj[i]) {
            maxDegree.erase(maxDegree.find(deg[node] - 1));
            maxDegree.insert(deg[node]);
        }
    }
    
    cout<<maxComponents<<endl;
    
}

int32_t main(){
    ios;
//    freopen("visitfj.in","r",stdin);
//    freopen("visitfj.out","w",stdout);
    int Test = 1;
    cin>>Test;
    for(I = 1 ; I <= Test ; I++)
    {
        solve();
    }
 
 
}