Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 3):
//JAI SHREE RAM
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
typedef long long ll;
#define inf 1e18

/*
-----------------------------ordered set--------------------
#include <ext/pb_ds/assoc_container.hpp> // Common file  
#include <ext/pb_ds/tree_policy.hpp>  
#include <functional> // for less  
#include <iostream>  
using namespace __gnu_pbds;  
using namespace std;  
  
typedef tree<int, null_type, less<int>, rb_tree_tag,  
            tree_order_statistics_node_update>  
    ordered_set;  
*/

/*-----------------PRIME FACTOR-------------
for (int i = 2; i*i <= n; i = i+2)
{
    // While i divides n, print i and divide n
    while (n%i == 0)
    {
        printf("%d ", i);
        n = n/i;
    }
}
*/

/*-----------------------sieve----------------
vector<bool> prime(MAXN, false);
vector<bool> used(MAXN, false);
void precalc() {
    for (int i = 2; i < MAXN; ++i) {
        if (!used[i]) {
            prime[i] = true;
            for (int j = i; j < MAXN; j += i) {
                used[j] = true;
            }
        }
    }
}
*/

/*--------------------------------sqrt function------------------------
ll my_sqrt(ll a)
{
    ll l=0,r=5000000001;
    while(r-l>1)
    {
        ll mid=(l+r)/2;
        if(1ll*mid*mid<=a)l=mid;
        else r=mid;
    }
    return l;
}

ll getclosest(ll x)
{
    ll n = sqrt(x);
    if(n*n==x)
    {
        return 0;
    }
    else
    {
        return (n+1)*(n+1) - x;
    }
}
ll cur = 0;
    for(ll i=1;i<n;i++)
    {
        ll a,x;
        cin>>a>>x;
        cur+=x;
        cout<<cur+w*(n-i)-cur*(n-i-1)<<" ";
    }
    cout<<endl;

const int MAXN = 1e5+5;
vector<vector<int>>divisors(MAXN);
void precomp()
{
    for(int i=2;i<MAXN;i++)
    {
        for(int j = i;j<MAXN;j+=i)
        {
            divisors[j].push_back(i);
        }
    }
}
*/#include <bits/stdc++.h>
using namespace std;

void solve() 
{
    int n;
    cin >> n;
    
    vector<vector<int>> graph(n + 1);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    vector<int> degree(n + 1);
    multiset<int> store;
    
    for (int i = 1; i <= n; i++) {
        degree[i] = graph[i].size();
        store.insert(degree[i]);
    }

    int comp = 0;

    for (int i = 1; i <= n; i++) 
    {
        multiset<int> work = store;
        int cur = 0;
        work.erase(work.find(degree[i]));
        cur += degree[i];

        for (auto v : graph[i]) {
            work.erase(work.find(degree[v]));
            work.insert(degree[v] - 1);
        }

        if (!work.empty()) {
            cur += (*work.rbegin())-1;  
        }

        comp = max(comp, cur);
    }

    cout << comp << endl;
}

int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;  
    cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}


Submission (verdict: Accepted):
//JAI SHREE RAM
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
typedef long long ll;
#define inf 1e18

/*
-----------------------------ordered set--------------------
#include <ext/pb_ds/assoc_container.hpp> // Common file  
#include <ext/pb_ds/tree_policy.hpp>  
#include <functional> // for less  
#include <iostream>  
using namespace __gnu_pbds;  
using namespace std;  
  
typedef tree<int, null_type, less<int>, rb_tree_tag,  
            tree_order_statistics_node_update>  
    ordered_set;  
*/

/*-----------------PRIME FACTOR-------------
for (int i = 2; i*i <= n; i = i+2)
{
    // While i divides n, print i and divide n
    while (n%i == 0)
    {
        printf("%d ", i);
        n = n/i;
    }
}
*/

/*-----------------------sieve----------------
vector<bool> prime(MAXN, false);
vector<bool> used(MAXN, false);
void precalc() {
    for (int i = 2; i < MAXN; ++i) {
        if (!used[i]) {
            prime[i] = true;
            for (int j = i; j < MAXN; j += i) {
                used[j] = true;
            }
        }
    }
}
*/

/*--------------------------------sqrt function------------------------
ll my_sqrt(ll a)
{
    ll l=0,r=5000000001;
    while(r-l>1)
    {
        ll mid=(l+r)/2;
        if(1ll*mid*mid<=a)l=mid;
        else r=mid;
    }
    return l;
}

ll getclosest(ll x)
{
    ll n = sqrt(x);
    if(n*n==x)
    {
        return 0;
    }
    else
    {
        return (n+1)*(n+1) - x;
    }
}
ll cur = 0;
    for(ll i=1;i<n;i++)
    {
        ll a,x;
        cin>>a>>x;
        cur+=x;
        cout<<cur+w*(n-i)-cur*(n-i-1)<<" ";
    }
    cout<<endl;

const int MAXN = 1e5+5;
vector<vector<int>>divisors(MAXN);
void precomp()
{
    for(int i=2;i<MAXN;i++)
    {
        for(int j = i;j<MAXN;j+=i)
        {
            divisors[j].push_back(i);
        }
    }
}
*/#include <bits/stdc++.h>
using namespace std;

void solve() 
{
    int n;
    cin >> n;
    
    vector<vector<int>> graph(n + 1);
    vector<int> degree(n + 1);
    for (int i = 0; i < n - 1; i++) 
    {
        int u, v;
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
        degree[u]++;
        degree[v]++;
    }
    multiset<int> work;  
    for (int i = 1; i <= n; i++) 
    {
        work.insert(degree[i]);
    }
    int comp = 0;
    for (int i = 1; i <= n; i++) 
    {
        vector<int>in,rem;
        int cur = 0;
        work.erase(work.find(degree[i]));
        in.push_back(degree[i]);
        cur += degree[i];
        for (auto v : graph[i]) {
            work.erase(work.find(degree[v]));
            work.insert(degree[v] - 1);
            rem.push_back(degree[v]-1);
            in.push_back(degree[v]);
        }
        if (!work.empty()) 
        {
            cur += (*work.rbegin())-1;  
        }
        for(auto p:in)
        {
            work.insert(p);
        }
        for(auto p:rem)
        {
            work.erase(work.find(p));
        }
        comp = max(comp, cur);
    }

    cout << comp << endl;
}

int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;  
    cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}