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

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

using namespace std;

#define ll long long
#define int long long
#define lld long double
#define endl "\n"
#define pb push_back
#define MOD 1000000007
#define inf 1e18
#define CHAR 256
#define all(x) x.begin(), x.end()
#define limits 100005
#define N 200005

/*---------------------------------------------------------------------------------------------------------------------------*/
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
void extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3
ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0];} //for non prime b
ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}
bool revsort(ll a, ll b) {return a > b;}
ll combination(ll n, ll r, ll m, ll *fact, ll *ifact) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}
vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;}  //only for prime m
ll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))
/*--------------------------------------------------------------------------------------------------------------------------*/


void solve(){
	int n;
	cin >> n;
	vector<set<int>> adj(n+1);
	
	for(int i=0; i<n-1; i++){
	    int u, v;
	    cin >> u >> v;
	    adj[u].insert(v);
	    adj[v].insert(u);
	}
	
	int ans=1;
	
    for(int i=0; i<2; i++){
        int node;
        vector<set<int>> temp=adj;
        vector<int> inorder;
        queue<int> que;
        for(int i=1; i<=n; i++){
            if(adj[i].size()==1){
                que.push(i);
                node=i;
                inorder.pb(i);
            }
        }
        
        while(que.size()){
            int curr=que.front();
            que.pop();
            
            for(auto it: temp[curr]){
                temp[it].erase(curr);
                if(temp[it].size()==1){
                    que.push(it);
                    inorder.pb(it);
                }
        
            }
        }
        
        for(auto i: inorder){
            if(adj[i].size()>adj[node].size())node=i;
            //cout << order[i] << " ";
        }
        
        ans+=(int)(adj[node].size())-1;
        for(auto it: adj[node])adj[it].erase(node);
        adj[node].clear();
    }
    
    cout << ans;
}
 

int32_t main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("error.txt", "w", stderr);
#endif

    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr); std::cout.tie(nullptr);

    int t;
    cin >> t;


    while(t--){
        solve();
    	cout << "\n";
    }
    return 0;
}

Solution 2 (runtime: 327 ms):
#include <stdio.h>
#include <string.h>

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

// #include <unordered_map> // NEVER USE THOSE IN CP
// #include <unordered_set> // NEVER USE THOSE IN CP

#define int long long  // Because i'm so done with integer overflow mistakes

using namespace std;

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        set<int> adj[n];
        for (int i = 0; i < n - 1; i++) {
            int u, v;
            cin >> u >> v;
            u--;
            v--;
            adj[u].insert(v);
            adj[v].insert(u);
        }
        map<int, vector<int>, greater<int>> largests;
        for (int i = 0; i < n; i++) {
            largests[adj[i].size()].push_back(i);
        }
        int ans = largests.begin()->first;  // we always greedily take the first largest node

        vector<int> firsts;
        vector<int> seconds;
        if (largests.begin()->second.size() == 1) {  // get the second largest
            auto p = largests.begin();
            firsts.push_back(p->second[0]);
            p++;  // second largest size
            seconds = p->second;
        } else {
            // 2 values is enough
            // firsts.push_back(largests.begin()->second[0]);
            // firsts.push_back(largests.begin()->second[1]);
            seconds = largests.begin()->second;
            firsts = seconds;
        }
        int second_size = 0;

        bool found = false;
        for (auto first : firsts) {
            for (auto second : seconds) {
                if (first == second) {
                    continue;
                }
                if (adj[first].count(second)) {
                    second_size = (int)adj[second].size() - 2;
                } else {
                    second_size = (int)adj[second].size() - 1;
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            }
        }
        ans += second_size;

        cout << ans << endl;
    }
    return 0;
}