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

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

using namespace std;

int main() {
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<vector<int>> adjs(n);
		vector<pair<int, int>> edgs(n, pair<int, int>(0, 0));
		for (int i = 0; i < n; i++) edgs[i].second = i;
		for (int i = 0; i < n-1; i++) {
			int u, v; cin >> u >> v; u--; v--;
			adjs[u].push_back(v); adjs[v].push_back(u);
			edgs[u].first++; edgs[v].first++;
		}
		sort(edgs.begin(), edgs.end());
		if (edgs[n-1].first != edgs[n-2].first) {
				int i = 0;
				int ss = edgs[n-2].first;
				while ((n-i-2) >= 0 && edgs[n-i-2].first == ss) i++;
				auto& _ = adjs[edgs[n-1].second];
				sort(_.begin(), _.end());
				bool found = false;
				for (int j = n-2; j > (n-2-i); j--) {
					if (!binary_search(_.begin(), _.end(), edgs[j].second)) {
						found = true;
						cout << (edgs[n-1].first + edgs[n-2].first - 1) << '\n';
						break;
					}
				}
				if (!found) cout << (edgs[n-1].first + edgs[n-2].first - 2) << '\n';
		} else {
			int i = 0;
			int ss = edgs[n-1].first;
			while ((n-i-1) >= 0 && edgs[n-i-1].first == ss) i++;
			if (i >= 3) {
				cout << (ss + ss - 1) << '\n';
			} else {
				auto& _ = adjs[edgs[n-1].second];
				if (find(_.begin(), _.end(), edgs[n-2].second) == _.end()) {
					cout << (edgs[n-1].first + edgs[n-2].first - 1) << '\n';
				} else {
					cout << (edgs[n-1].first + edgs[n-2].first - 2) << '\n';
				}
			}

		}
	}

	return 0;
}


Solution 2 (runtime: 640 ms):
// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;

#define ll long long

void get(ll ind,vector<vector<ll>> &mad,vector<bool> &vis,priority_queue<pair<ll,ll>> &lad,int count)
{
    for(int i=0;i<mad[ind].size();i++)
    {
        if(vis[mad[ind][i]]==false)
        {
            vis[mad[ind][i]]=true;
            get(mad[ind][i],mad,vis,lad,1);
            count++;
        }
    }
    lad.push({count,ind});
}

void get1(ll ind,vector<vector<ll>> &mad,vector<bool> &vis)
{
    for(int i=0;i<mad[ind].size();i++)
    {
        if(vis[mad[ind][i]]==false)
        {
            vis[mad[ind][i]]=true;
            get1(mad[ind][i],mad,vis);
        }
    }
}

int main() {
    // Write C++ code here
    ll t,n,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<vector<ll>> mad(n+1);
        for(int i=1;i<n;i++)
        {
            cin>>x>>y;
            mad[x].push_back(y);
            mad[y].push_back(x);
        }
        
        if(n<=2)
        {
            cout<<0<<endl;
            continue;
        }
        
        vector<bool> vis(n+1);
        ll a=mad[1].size(),b=1,c=1;
        for(int i=2;i<=n;i++)
        {
            if(mad[i].size()>a)
            {
                a=mad[i].size();
                b=i;
                c=1;
            }
            else if(mad[i].size()>=a)
            {
                c++;
            }
        }
        if(c==1)
        {
            
        vis[b]=true;
        vector<bool> vis2(n+1);
        vis2[b]=true;
        priority_queue<pair<ll,ll>> lad;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==false)
            {
                vis[i]=true;
                get(i,mad,vis,lad,0);
            }
        }
        
        vis2[lad.top().second]=true;
        ll count=0;
        for(int i=1;i<=n;i++)
        {
            if(vis2[i]==false)
            {
                vis2[i]=true;
                get1(i,mad,vis2);
                count++;
            }
        }
        cout<<count<<endl;
        }
        else{
            vector<ll> sad;
            for(int i=1;i<=n;i++)
            {
                if(mad[i].size()==a)
                {
                    sad.push_back(i);
                }
            }
            
            if(sad.size()==2)
            {
                vector<bool> vis2(n+1);
                vis2[sad[0]]=true;
                vis2[sad[1]]=true;
                ll count=0;
        for(int i=1;i<=n;i++)
        {
            if(vis2[i]==false)
            {
                vis2[i]=true;
                get1(i,mad,vis2);
                count++;
            }
        }
        cout<<count<<endl;
            }
            else{
                ll ans=0;
                for(int i=0;i<3;i++)
                {
                    vector<bool> vis2(n+1);
                vis2[sad[i]]=true;
                vis2[sad[(i+1)%3]]=true;
                ll count=0;
                    for(int i=1;i<=n;i++)
                    {
                        if(vis2[i]==false)
                        {
                            vis2[i]=true;
                            get1(i,mad,vis2);
                            count++;
                        }
                    }
                    if(count>ans)
                    ans=count;
                }
                cout<<ans<<endl;
            }
        }
        
        
    }

    return 0;
}