Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 9):
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<climits> 
#include<cstring>
#include<ctime>
#include<chrono>
#include<cstdlib>
#include<cmath>
#include<fstream>
#include<iterator>
#include<map>
#include<numeric>
#include<queue>
#include<string>
#include<stack>
#include<set>
#include<utility>
#include<unordered_set>
#include<unordered_map>
#include<vector>





#define mp              make_pair
#define pb              push_back
#define fi              first
#define se              second
#define maxl2(a,b)      ((a>b)?a:b)
#define minl2(a,b)      ((a>b)?b:a)
#define maxl3(a,b,c)    ((a>b)?((a>c)?a:c):((b>c)?b:c))
#define minl3(a,b,c)    ((a<b)?((a<c)?a:c):((b<c)?b:c))
#define mem(a,b)        memset(a,b,sizeof(a))
#define all(x)          x.begin(),x.end()
#define SP(y)           fixed<<setprecision(y)


typedef long long           ll;
typedef long double         ld;

typedef std::vector<int>    VI;
typedef std::vector<ll>     VL;
typedef std::vector<bool>   VB;

typedef std::pair<int,int>  PII;
typedef std::pair<ll,ll>    PLL;
typedef std::pair<int,ll>   PIL;
typedef std::pair<ll,int>   PLI;
typedef std::pair<char,ll>  PCL;
typedef std::pair<ll,char>  PLC;



const                       ll MOD = 1e9+7;
const                       ll N = 2e5+10;
const                       double PI = 3.14159265358979323846;

#define fo(i,a,b) for(ll i = a; i < b; ++i)
#define fd(i,a,b) for(ll i = a ;i >= b ;--i)

using namespace std;

VI fact;
void initializeFactorials(int n,ll mod = MOD)
{
    fact.push_back(1);
    for(int i = 1;i <= n;++i) fact.push_back((1ll*fact[i-1]*i) % mod);
}

ll exp(ll x,ll n,ll mod = MOD)
{
    ll ans = 1ll;
    while(n > 0)
    {
        if(n % 2 != 0) ans = (ans * x) % mod;
        x = (x*x) % mod;
        n >>= 1;
    }
    return ans % mod;
}

ll mod(ll a,ll b){ ll r = ((a%b)+b)%b;  return r;}

ll modInv_1(ll x,ll mod = MOD)  {return exp(x,mod-2);} //! Used only if mod is PRIME.

ll nCr_1(int n,int r,ll mod = MOD) //!Used only if mod is PRIME
{
    return (1ll * fact[n] * modInv_1(fact[n-r],mod) % mod * 1ll * modInv_1(fact[r],mod) % mod);
}

VL prefixCreation(VL v)
{
    VL ans;
    ll sum;
    ans.pb(0);
    for(auto i : v)
    {
        ans.pb(*(ans.end()-1) + i);
    }
    return ans;
}

void solve()
{
    //cout.setf(ios::fixed);
    //cout.precision(3); //!Any number of decimal places required
    int n, x, y;
    cin>>n;
    vector<set<int>> adj(n);
    for(int i = 0; i < n - 1; ++i)
    {
        cin>>x>>y;
        --x;--y;
        adj[x].insert(y);
        adj[y].insert(x);
    }

    int ans = 0, maxi = -1;

    set<pair<int, int>> degs;
    for(int i = 0; i < n; ++i)
    {
        degs.insert({adj[i].size(), i});
    }

    auto maxDegItr = degs.rbegin();
    int maxDeg = maxDegItr->first;
    while(maxDegItr != degs.rend() && maxDegItr -> first == maxDeg)
    {
        set<int> temp;
        for(int i = 0; i < n; ++i)
        {
            if(i == maxDegItr->second) continue;
            else
            {
                temp.insert((adj[i].count(maxDegItr->second) == 1 ? adj[i].size() - 1 : adj[i].size()));
            }
        }
        ans = max(ans, maxDeg + *temp.rbegin() - 1);
        ++maxDegItr;
    } 
    

    cout<<ans<<endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    t = 1;
    cin>>t;

    while(t--)
    {
        solve();
    }
   

    return 0;
}

Submission (verdict: Accepted):
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<climits> 
#include<cstring>
#include<ctime>
#include<chrono>
#include<cstdlib>
#include<cmath>
#include<fstream>
#include<iterator>
#include<map>
#include<numeric>
#include<queue>
#include<string>
#include<stack>
#include<set>
#include<utility>
#include<unordered_set>
#include<unordered_map>
#include<vector>





#define mp              make_pair
#define pb              push_back
#define fi              first
#define se              second
#define maxl2(a,b)      ((a>b)?a:b)
#define minl2(a,b)      ((a>b)?b:a)
#define maxl3(a,b,c)    ((a>b)?((a>c)?a:c):((b>c)?b:c))
#define minl3(a,b,c)    ((a<b)?((a<c)?a:c):((b<c)?b:c))
#define mem(a,b)        memset(a,b,sizeof(a))
#define all(x)          x.begin(),x.end()
#define SP(y)           fixed<<setprecision(y)


typedef long long           ll;
typedef long double         ld;

typedef std::vector<int>    VI;
typedef std::vector<ll>     VL;
typedef std::vector<bool>   VB;

typedef std::pair<int,int>  PII;
typedef std::pair<ll,ll>    PLL;
typedef std::pair<int,ll>   PIL;
typedef std::pair<ll,int>   PLI;
typedef std::pair<char,ll>  PCL;
typedef std::pair<ll,char>  PLC;



const                       ll MOD = 1e9+7;
const                       ll N = 2e5+10;
const                       double PI = 3.14159265358979323846;

#define fo(i,a,b) for(ll i = a; i < b; ++i)
#define fd(i,a,b) for(ll i = a ;i >= b ;--i)

using namespace std;

VI fact;
void initializeFactorials(int n,ll mod = MOD)
{
    fact.push_back(1);
    for(int i = 1;i <= n;++i) fact.push_back((1ll*fact[i-1]*i) % mod);
}

ll exp(ll x,ll n,ll mod = MOD)
{
    ll ans = 1ll;
    while(n > 0)
    {
        if(n % 2 != 0) ans = (ans * x) % mod;
        x = (x*x) % mod;
        n >>= 1;
    }
    return ans % mod;
}

ll mod(ll a,ll b){ ll r = ((a%b)+b)%b;  return r;}

ll modInv_1(ll x,ll mod = MOD)  {return exp(x,mod-2);} //! Used only if mod is PRIME.

ll nCr_1(int n,int r,ll mod = MOD) //!Used only if mod is PRIME
{
    return (1ll * fact[n] * modInv_1(fact[n-r],mod) % mod * 1ll * modInv_1(fact[r],mod) % mod);
}

VL prefixCreation(VL v)
{
    VL ans;
    ll sum;
    ans.pb(0);
    for(auto i : v)
    {
        ans.pb(*(ans.end()-1) + i);
    }
    return ans;
}

void solve()
{
    //cout.setf(ios::fixed);
    //cout.precision(3); //!Any number of decimal places required
    int n, x, y;
    cin>>n;
    vector<set<int>> adj(n);
    for(int i = 0; i < n - 1; ++i)
    {
        cin>>x>>y;
        --x;--y;
        adj[x].insert(y);
        adj[y].insert(x);
    }

    int ans = 0, maxi = -1;

    set<pair<int, int>> degs, degs1;
    for(int i = 0; i < n; ++i)
    {
        degs.insert({adj[i].size(), i});
        degs1.insert({adj[i].size(), i});
    }

    auto maxDegItr = degs.rbegin();
    int maxDeg = maxDegItr->first;
    while(maxDegItr != degs.rend() && maxDegItr -> first == maxDeg)
    {
        degs1.erase({maxDegItr->first, maxDegItr->second});
        for(auto i : adj[maxDegItr->second])
        {
            degs1.erase({adj[i].size(),i});
            degs1.insert({adj[i].size()-1,i});
        }
        ans = max(ans, maxDeg + degs1.rbegin()->first - 1);

        for(auto i : adj[maxDegItr->second])
        {
            degs1.insert({adj[i].size(),i});
            degs1.erase({adj[i].size()-1,i});
        }
        degs1.insert({maxDegItr->first, maxDegItr->second});
        ++maxDegItr;
    } 
    

    cout<<ans<<endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    t = 1;
    cin>>t;

    while(t--)
    {
        solve();
    }
   

    return 0;
}