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

Solution 1 (runtime: 468 ms):
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--){
        int n;
        cin >> n;
        vector<pair<int,int>> edges;
        int counting[200015]; // counting[i] denotes the number of edges that connects to edge i
        bool adj[200015]; // adj[i] denotes whether node i is adjacent to the maxnode
        for (int i=1; i<=n; i++){ // reset counting and adj
            counting[i] = 0;
            adj[i] = false;
        }
        vector<pair<int,int>> cnt; // counting, index
        int dem = 1; // number of connected components
        vector<int> maxnode2;

        for (int i=1; i<n; i++){
            int x, y;
            cin >> x >> y;
            edges.push_back({x,y});
            counting[x]++;
            counting[y]++;
        }

        for (int i=1; i<=n; i++) cnt.push_back({counting[i], i});
        sort(cnt.begin(),cnt.end());

        int maxnode = cnt.back().second;
        for (auto edge: edges){
            int x = edge.first;
            int y = edge.second;

            if (x == maxnode || y == maxnode) adj[x + y - maxnode] = true;
        }

        for (int i=cnt.size() - 2; i >= 0; i--){
            if (cnt[i].first == cnt[cnt.size() - 2].first) maxnode2.push_back(cnt[i].second);
        }

        dem = cnt.back().first + counting[maxnode2[0]] - 1;

        if (cnt.back().first == counting[maxnode2[0]] && maxnode2.size() > 1) dem += 0; // more than 3 max nodes so ignore updates
        else{
            bool check = false; // true = there is a node in maxnode2 that doesnt directly connect to maxnode

            for (auto x : maxnode2){
                if (adj[x] == false) check = true;
            }

            if (!check) dem--;
        }

        // clearing for next test case
        cnt.clear();
        edges.clear();
        maxnode2.clear();

        cout << dem << endl;
    }

    return 0;
}


Solution 2 (runtime: 265 ms):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 0x3f3f3f3f;
const int MAXN = 100;
const int MOD = 1000000007;
const double eps = 1e-8;
const double PI = acos(-1.0);
// ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
// ll qpow(ll a,ll n,ll p=MOD) {ll r=1%p;for (a%=p;n;a=a*a%p,n>>=1)if(n&1)r=r*a%p;return r;}
// ll inv(ll x,ll p=MOD){return x<=1?1:inv(p%x,p)*(p-p/x)%p;}

int ans;

vector<int> g[200005];

int dfs(int fa,int now){
    vector<int> deg;
    for(int nxt:g[now]){
        if(nxt==fa){
            continue;
        }
        ans=max(ans,int(g[now].size()+g[nxt].size()-2));
        int nxt_deg=dfs(now,nxt);
        ans=max(ans,int(g[now].size()+nxt_deg-1));
        deg.emplace_back(max(nxt_deg,int(g[nxt].size())));
    }
    sort(deg.begin(),deg.end(),greater<int>());
    if(deg.size()>1){
        ans=max(ans,int(deg[0]+deg[1]-1));
    }
    if(deg.size()){
        return deg[0];
    }
    else{
        return 0;
    }

}
void solve(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        g[i].clear();
    }
    ans=0;
    for(int i=1;i<n;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    dfs(0,1);
    printf("%d\n",ans);
}

int main(int argc, char *argv[]){
    // ios_base::sync_with_stdio(0);
    // cin.tie(0);
    // cout.tie(0);

    int t = 1;
    scanf("%d", &t);
    while (t--)
    {
        solve();
    }
    return 0;
}