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

Solution 1 (runtime: 1093 ms):
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace chrono;

using namespace __gnu_pbds;


template<typename T>
using orderedSet = tree<T , null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using orderedMultiset = tree<T , null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) -> iterator at k
// order_of_key(k) -> number of items that are strictly smaller than k


#define FASTIO() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define MOD1 1000000007
#define MOD9 998244353
#define INF 1e9
#define INF1 1e18
#define endl "\n"
#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define lb lower_bound
#define ub upper_bound
#define fcout cout<<fixed<<setprecision(10)
#define YES  cout<<"YES"<<endl
#define NO cout<<"NO"<<endl

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

// ---------------------------------- DEFINES ---------------------------------------
typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <vector <int> > vvi;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
template<typename T> using PQ = priority_queue<T>;
template<typename T> using QP = priority_queue<T, vector<T>, greater<T>>;
// ----------------------------------------------------------------------------------

// ---------------------------------- OPERATOR OVERLOADING ---------------------------------------
template<typename T>
istream& operator>>(istream &istream, vector<T> &v) {for (auto &x : v)cin >> x; return istream;}
template<typename T>
ostream& operator<<(ostream &ostream, const vector<T> &v) { for (auto &x : v) cout << x << " "; return ostream; }
template<typename T1, typename T2>
istream& operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.ff >> p.ss); }
template<typename T1, typename T2>
ostream& operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.ff << " " << p.ss); }
template<typename T1, typename T2>
istream& operator>>(istream &istream, vector<pair<T1, T2>> &v) {for (auto &x : v)cin >> x; return istream;}
template<typename T1, typename T2>
ostream& operator<<(ostream &ostream, const vector<pair<T1, T2>> &v) { for (auto &x : v) cout << x << endl; return ostream; }
template<typename T>
istream& operator>>(istream &istream, vector<vector<T>> &v) {for (auto &x : v)cin >> x; return istream;}
template<typename T>
ostream& operator<<(ostream &ostream, const vector<vector<T>> &v) { for (auto &x : v) cout << x << endl; return ostream; }
// ------------------------------------------------------------------------------------------------


// ------------------------------------------ DEBUG ------------------------------------------------
#ifndef ONLINE_JUDGE
#define debug(x...) cerr <<__func__<<":"<<__LINE__<<" [" << #x << "] = "; _print(x); cerr << endl;
#define HERE cerr <<__func__<<":"<<__LINE__<<" [ here ] "; cerr << endl;
#else
#define debug(x...)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(vector <vector <T> > v) {cerr << "{ "; for (vector<T> i : v) {cerr << "[ "; for (T j : i) {_print(j); cerr << " ";}; cerr << "] ";} cerr << "}";}
template <class T, class V> void _print(vector < pair <T , V> > vp) {cerr << "{ "; for (pair<T, V> i : vp) {cerr << "[ "; "{"; _print(i.ff); cerr << ","; _print(i.ss); cerr << " ] ";} cerr << "}";}
template <class T> void _print(stack <T> st) {cerr << "[ "; while (!st.empty()) {_print(st.top()); st.pop(); cerr << " ";} cerr << "]";}
template <class T> void _print(queue <T> q) {cerr << "[ "; while (!q.empty()) {_print(q.front()); q.pop(); cerr << " ";} cerr << "]";}
template <class T> void _print(deque <T> dq) {cerr << "[ "; while (!dq.empty()) {_print(dq.front()); dq.pop_front(); cerr << " ";} cerr << "]";}
template <class T> void _print(PQ <T> pq) {cerr << "[ "; while (!pq.empty()) {_print(pq.top()); pq.pop(); cerr << " ";} cerr << "]";}
template <class T> void _print(QP <T> pq) {cerr << "[ "; while (!pq.empty()) {_print(pq.top()); pq.pop(); cerr << " ";} cerr << "]";}
//---------------------------------------------------------------------------------------------------


void solve() {

	//solution starts from here
	int n;
	cin>>n;

	vvi adj(n);
	vi deg(n);

	for (int i = 0; i < n-1; ++i)
	{
		int u,v;
		cin>>u>>v;
		u--,v--;
		adj[u].pb(v);
		adj[v].pb(u);
		deg[u]++;
		deg[v]++;
	}

	set<pii> st;
	for(int i=0;i<n;i++){
		st.insert({deg[i],i});
	}

	int ans = 0;
	for (int i = 0; i < n; ++i)
	{
		int first = deg[i];
		st.erase({deg[i],i});
		vpii store;
		for(auto child : adj[i]) {
			st.erase({deg[child],child});
			st.insert({deg[child] -1,child});
			store.pb({deg[child],child});
		}
		int second = (*prev(st.end())).ff;
		ans = max(ans,first + second -1);
		st.insert({deg[i],i});
		for(auto [count, val] : store) {
			st.erase({count-1,val});
			st.insert({count,val});
		}
	}
	cout<<ans<<endl;
}

int main() {

#ifndef ONLINE_JUDGE
	freopen("error.txt", "w", stderr);
#endif

	FASTIO();
	auto start1 = high_resolution_clock::now();
	int t = 1;
	cin >> t;
	int T = t;
	while (t--)
	{
	   #ifndef ONLINE_JUDGE
	   	cerr<<"Test "<<(T-t)<<" :"<<endl;
	   #endif	
	   solve();
	   #ifndef ONLINE_JUDGE
	   	cerr<<"--------------------------"<<endl<<endl;
	   #endif	
	}
	auto stop1 = high_resolution_clock::now();
	auto duration = duration_cast<microseconds>(stop1 - start1);
#ifndef ONLINE_JUDGE
	cerr << "Time elapsed : " << (duration . count() / 1000000) << '.' << (duration . count() % 1000000) << endl;
#endif

}

Solution 2 (runtime: 514 ms):
#include <bits/stdc++.h>
#define lli long long int
const int N = 1e5 + 10;
#define pb push_back
using namespace std;
#include <bits/extc++.h>
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void dfs(vector<lli> &vis, vector<set<lli>> &adj, lli node)
{
    if (vis[node])
        return;
    vis[node] = 1;
    for (auto x : adj[node])
    {
        dfs(vis, adj, x);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lli t;
    cin >> t;
    while (t--)
    {
        lli n;
        cin >> n;

        vector<lli> incidents(n + 1, 0);
        map<pair<lli, lli>, lli> mp;

        for(lli i=0; i<n-1; i++)
        {
            lli u, v;
            cin >> u >> v;
            incidents[u]++;
            incidents[v]++;

            mp[{u, v}] = 1;
            mp[{v, u}] = 1;
        }

        vector<pair<lli, lli>> sorted;
        for (int i = 1; i <= n; i++)
        {
            sorted.pb({incidents[i], i});
        }

        sort(sorted.rbegin(), sorted.rend());

        lli res = 0;
        for (int i = 0; i < n; i++)
        {
            lli temp = 0;
            temp += (sorted[i].first);

            int j = 0;
            lli cnt = -1;
            while (j < n)
            {
                if (j == i)
                {
                    j++;
                    continue;
                }
                else
                {
                    if (mp.find({sorted[i].second, sorted[j].second}) != mp.end())
                    {
                        cnt = max(cnt, sorted[j].first - 2);
                    }
                    else
                    {
                        cnt = max(sorted[j].first - 1, cnt);
                        break;
                    }
                }
                j++;
            }
            res = max(res, temp + cnt);
        }
        cout << res << endl;
    }
    return 0;
}