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

Previous submission (verdict: Wrong answer on test 4):
#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;

const int32_t N = 200006; 
vector<int>A[N];

void remove_node(int node) {
    if(node == -1) return;
    for(int i=0;i<A[node].size();i++){
        for(int j=0;j<A[A[node][i]].size();j++){
            if(A[A[node][i]][j] == node) {
                A[A[node][i]].erase(A[A[node][i]].begin() + j);
                break;
            }
        }
    }
    A[node].clear();
}

void recur(int node, vector<int>&vis) {
    vis[node] = 1;
    for(auto e:A[node]) {
        if(vis[e]) continue;
        recur(e, vis);
    }
}

void solve(){
    int n; cin >> n;
    for(int i=1;i<=n;i++) A[i].clear();
    for(int i=0;i<n-1;i++){
        int x,y; cin >> x >> y;
        A[x].push_back(y);
        A[y].push_back(x);
    }
    int firstNumber=0, secondNumber=0;
    int node = -1, times = 0;
    for(int i=1;i<=n;i++){
        // cout << "i: " << i << " -->> ";
        // for(auto e:A[i]) {
        //     cout << e << " ";
        // } cout << '\n';
        // watch(A[i].size());
        // cout << times << ":" << A[i].size() << nline;
        if(times <= A[i].size()) {
            node = i;
            times = A[i].size();
        }
    }
    firstNumber = node;
    // cout << node << " " << times << '\n';
    remove_node(node);
    node = -1, times = 0;
    for(int i=1;i<=n;i++){
        //  cout << "i: " << i << " -->> ";
        // for(auto e:A[i]) {
        //     cout << e << " ";
        // } cout << '\n';
        if(times <= A[i].size() && firstNumber != i) {
            node = i;
            times = A[i].size();
        }
    }
    secondNumber = node;
    remove_node(node);
    // cout << firstNumber << " @ " << secondNumber << nline;
    // cout << node << " " << times << '\n';
    vector<int>visited(n+2,0);
    int cnt = 0;
    for(int i=1;i<=n;i++){
        if(firstNumber == i || secondNumber == i || visited[i]){
            continue;
        }
        // cout <<"i:" << i << " -> ";
        // for(auto e:A[i]) cout << e <<  " "; cout << nline;
        cnt++;
        recur(i, visited);
    }
    cout << cnt << nline;
}

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;
} 

Submission (verdict: Wrong answer on test 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;

    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-1) - vis[p.second]); 
        ans = max(ans, cnt);
    }
    cout << ans << '\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;
}