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

Code 1:
#include<bits/stdc++.h>
#define ll                long long
#define ld                long double
#define fi                first
#define se                second
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front 
#define nline             "\n"
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define srt(x)            sort(all(x))
#define mem(a,val)        memset(a, val, sizeof(a))
#define Up(v,n)           upper_bound(v.begin(),v.end(),n)
#define Low(v,n)          lower_bound(v.begin(),v.end(),n)
#define PRE(x)            fixed << setprecision(x)
#define watch(x)          cout << (#x) << " is " << (x) << endl
using namespace std;

long long powM(long long a, long long b, long long m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

// GCD(A,B) = GCD(B,A-B) -> here A>=B or GCD(A,B) = GCD(B-A,A) -> here A<=B
ll gcd(ll a, ll b) { if (b == 0) {return a;} return gcd(b, a % b);}
ll modinv(ll a, ll m) { return powM(a, m - 2, m);}
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 moddiv(ll a, ll b, ll m) {a = a % m; b = b % m; 
return (mod_mul(a, modinv(b, m), m) + m) % m;}//m is prime

const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;

void solve(){
    int n; cin >> n;
    vector<int>a[n+1];
    priority_queue<pair<int,int>>pq;
    int ans = 0, maxi = 0, maxi_cnt = 0, tot = 0;

    for(int i=0;i<n-1;i++){
        int u,v; cin >> u >> v;
        a[u].push_back(v);  a[v].push_back(u);
    }
    for(int i=0;i<=n;i++) maxi = max(maxi, (int)a[i].size());
    for(int i=0;i<=n;i++) {
        if(a[i].size() == maxi) maxi_cnt++;
        pq.push({a[i].size(), i});
    }
    if(maxi_cnt > 2) {
        cout << pq.top().first*2-1 << '\n';
        return;
    }
    vector<int>vis(n+1,0);
    // removed one element
    ans = (pq.top().fi) - 1;
    for(auto e:a[pq.top().second]) vis[e]++;
    pq.pop();
    while(!pq.empty()) {
        int cnt = ans;
        pair<int,int>p = pq.top(); pq.pop();
        cnt = (cnt + (p.first) - vis[p.second]); 
        tot = max(tot, cnt);
    }
    cout << tot << '\n';
}

signed main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
    cout.precision(20), cout.setf(ios::fixed);
    int t=1;  
    cin>>t;
    while(t--){
        solve();
    }
    cerr << setprecision(4) << fixed;
    cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}



// 1 2
// 3

Code 2:
#include<bits/stdc++.h>
#define ll                long long
#define ld                long double
#define fi                first
#define se                second
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front 
#define nline             "\n"
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define srt(x)            sort(all(x))
#define mem(a,val)        memset(a, val, sizeof(a))
#define Up(v,n)           upper_bound(v.begin(),v.end(),n)
#define Low(v,n)          lower_bound(v.begin(),v.end(),n)
#define PRE(x)            fixed << setprecision(x)
#define watch(x)          cout << (#x) << " is " << (x) << endl
using namespace std;

long long powM(long long a, long long b, long long m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

// GCD(A,B) = GCD(B,A-B) -> here A>=B or GCD(A,B) = GCD(B-A,A) -> here A<=B
ll gcd(ll a, ll b) { if (b == 0) {return a;} return gcd(b, a % b);}
ll modinv(ll a, ll m) { return powM(a, m - 2, m);}
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 moddiv(ll a, ll b, ll m) {a = a % m; b = b % m; 
return (mod_mul(a, modinv(b, m), m) + m) % m;}//m is prime

const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;

void solve(){
    int n; cin >> n;
    vector<int>a[n+1];
    priority_queue<pair<int,int>>pq;
    int ans = 0, maxi = 0, maxi_cnt = 0, tot = 0;

    for(int i=0;i<n-1;i++){
        int u,v; cin >> u >> v;
        a[u].push_back(v);  a[v].push_back(u);
    }
    for(int i=0;i<=n;i++) maxi = max(maxi, (int)a[i].size());
    for(int i=0;i<=n;i++) {
        if(a[i].size() == maxi) maxi_cnt++;
        pq.push({a[i].size(), i});
    }
    if(maxi_cnt > 2) {
        cout << pq.top().first*2-1 << '\n';
        return;
    }
    vector<int>vis(n+1,0);
    // removed one element
    ans = (pq.top().fi) - 1;
    for(auto e:a[pq.top().second]) vis[e]++;
    pq.pop();
    while(!pq.empty()) {
        int cnt = ans;
        pair<int,int>p = pq.top(); pq.pop();
        cnt = (cnt + (p.first) - vis[p.second]); 
        tot = max(tot, cnt);
    }
    cout << tot << '\n';
}

signed main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
    cout.precision(20), cout.setf(ios::fixed);
    int t=1;  
    cin>>t;
    while(t--){
        solve();
    }
    cerr << setprecision(4) << fixed;
    cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}



1 2
3

Answer: 1