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

Solution 1 (runtime: 280 ms):
#include <bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

using namespace std;

// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
#define int long long
#define pb push_back
#define vi vector<int>
#define loop(i, n1, n) for (int i = n1; i < n; i++)
#define revloop(i, n, n1) for (int i = n; i >= n1; i--)
#define endl "\n"
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define sortv(v) sort(v.begin(), v.end())
#define pii pair<int, int>
#define vinp(v, n)              \
    for (int i = 0; i < n; i++) \
    {                           \
        int k;                  \
        cin >> k;               \
        v.pb(k);                \
    }
#define mii map<int, int>
// #define m 1e9+7
#define nl cout << endl
#define vdebug(v)         \
    for (auto i : v)      \
        cout << i << " "; \
    nl

#define vall(v) v.begin(), v.end()
#define vsort(v) sort(vall(v))
#define print(v) cout << v << endl;
double pi = 2 * acos(0.0);
int max(int a, int b)
{
    if (a > b)
        return a;
    else
        return b;
}
int min(int a, int b)
{
    if (a > b)
        return b;
    else
        return a;
}
bool isPalindrome(string S)
{

    string P = S;

    reverse(P.begin(), P.end());

    if (S == P)
    {

        return 1;
    }

    else
    {

        return 0;
    }
}
// void build(vector<int> v){
//     int n=v.size();
//     vector<vector<int>> s(n,vector<int>(17));
//     for(int i=0;i<n;i++){
//         s[i][0]=v[i];
//     }
//     for(int j=1;(1<<j)<=n;j++){
//         for(int i=0;i+(1<<j)<=n;i++){
//             s[i][j]=s[i][j-1] | s[i+ (1<<(j-1))][j-1];
//         }
//     }
//     //query
//     int lo=0,hi=5;
//     int len=hi-lo+1;
//     int k=log2(len);
//     int query=(s[lo][k] | s[hi-(1<<k)+1][k]);

// }

int binarySearch(vector<int> &arr, int target)
{
    int left = 0, right = arr.size() - 1;
    while (left <= right)
    {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target)
        {
            return mid;
        }
        else if (arr[mid] < target)
        {
            left = mid + 1;
        }
        else
        {
            right = mid - 1;
        }
    }
    return -1;
}

int RSB(int n)
{
    return log2(n & -n);
}

int factorial(int b)
{
    if (b == 1 || b == 0)
        return 1;
    return (b)*factorial(b - 1);
}

void multiply(int a[3][3], int b[3][3])
{
    int mul[3][3];
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            mul[i][j] = 0;
            for (int k = 0; k < 3; k++)
            {
                mul[i][j] += a[i][k] * b[k][j];
            }
        }
    }
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            a[i][j] = mul[i][j];
        }
    }
}

int power(int F[3][3], int n)
{
    int M[3][3] = {{1, 1, 0}, {1, 0, 0}, {0, 1, 0}};
    if (n == 1)
        return F[0][0] + F[0][1];
    power(F, n / 2);
    multiply(F, F);
    if (n % 2 != 0)
        multiply(F, M);
    return F[0][0] + F[0][1];
}

int fibo(int n)
{
    --n;
    int F[3][3] = {{1, 1, 0}, {1, 0, 0}, {0, 1, 0}};
    if (n == 0)
        return 0;
    if (n == 1 || n == 2)
        return 1;
    return power(F, n - 2);
}
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int lcm(int a, int b)
{
    return (a * b / gcd(a, b));
}
int prime(int n)
{
    int x = 2;

    while (x <= sqrt(n))
    {
        if ((n % x) == 0)
        {

            return x;
        }
        x++;
    }
    return -1;
}

double codist(int x1, int y1, int x2, int y2)
{
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
int NC2(int x)
{
    return max(x * (x - 1) / 2, 0);
}
int NC3(int x)
{
    return max(x * (x - 1) * (x - 2) / 6, 0);
}
void fact(int n, vector<int> &v)
{
    for (int i = 1; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
            v.pb(i);
            v.pb(n / i);
        }
    }
}

bool check(int k, int n, const vector<vector<int>> &op)
{
    int chec = -1;
    for (int i = 0; i <= n - k; ++i)
    {
        int j = log2(k);
        int o = op[i][j] | op[i + k - (1 << j)][j];
        if (chec == -1)
            chec = o;
        if (chec != o)
            return false;
    }
    return true;
}

void solve()
{
    int n;
    cin >> n;
    vector<int> g[n + 1];
    for (int i = 0; i < n - 1; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].pb(y);
        g[y].pb(x);
    }
    int ans = 0, mx = 0, id;
    for (int i = 1; i <= n; i++)
    {
        int sz = g[i].size();
        mx = max(mx, sz);
        if (g[i].size() == mx)
            id = i;
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++)
    {
        int sz = g[i].size();
        if (sz == mx)
            cnt++;
    }
    if (cnt >= 3)
    {
        cout << 2 * mx - 1 << endl;
        return;
    }
    ans += mx;
    set<int> se;
    mx = 0;
    for (auto x : g[id])
        se.insert(x);
    for (int i = 1; i <= n; i++)
    {
        if (i == id)
            continue;
        int sz = g[i].size();
        if (se.find(i) != se.end())
        {
            mx = max(mx, sz - 1);
        }
        else
            mx = max(mx, sz);
    }

    ans += (mx - 1);
    cout << ans << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int T;
    cin >> T;
    // T=1;
    while (T--)
    {
        solve();
    }
    return 0;
}


Solution 2 (runtime: 327 ms):
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
bool cmp(pair<ll,ll>a,pair<ll,ll>b){
return a.second>b.second;
}
void solve(ll tc)
{
    ll n,i,x,y,s=0,qq,ww,s2=0,ee,m;
    cin>>n;
    vector<pair<ll,ll>>p(n+9);
    for(i=1;i<=n;i++) p[i].first=i,p[i].second=0;
    vector<set<ll>> ps(n+3);
    for(i=1;i<n;i++){
        cin>>x>>y;
        p[x].second++;
        p[y].second++;
        ps[x].insert(y);
        ps[y].insert(x);
    }
    sort(p.begin(),p.end(),cmp);

    s=p[0].second;
    qq=p[0].first;
    m=min(n,s+2);
    for(i=1;i<m;i++){
        ww=p[i].second;
        ee=p[i].first;
        if(ps[ee].count(qq)) ww--;
        s2=max(s2,ww);

    }
    ll ans=s+s2-1;
    s=p[1].second;
    qq=p[1].first;
    m=min(n,s+2);
    for(i=2;i<m;i++){
        ww=p[i].second;
        ee=p[i].first;
        if(ps[ee].count(qq)) ww--;
        s2=max(s2,ww);

    }
    ans=max(ans,s+s2-1);
    cout<<ans<<endl;






}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tc=1;
    cin>>tc;
    for(ll i=1; i<=tc; i++)
    {
        solve(i);
    }
}