Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define inf 1e18
#define fi first
#define se second
#define endl "\n"
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define pi 3.14159265359
#define TASKNAME "2063C"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; } 
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; } 
using namespace std; 
typedef pair<int,int> ii; 
typedef pair<int,ii> iii; 
typedef vector<int> vi; 
const int MAXN = 3e5 + 9;
vector<int> g[MAXN];
int n, res = 0;
int dp1[MAXN], dp2[MAXN];

void dfs1(int u, int p){
    if (p == -1) dp1[u] = g[u].size();
    else dp1[u] = g[u].size() - 1;

    for(auto v: g[u]){
        if (v == p) continue;
        dfs1(v, u);
        maximize(dp1[u],dp1[v]);
        maximize(dp1[u], (int)g[v].size());
    }
}

void update(ii &ans, int b){
    if (b > ans.fi){
        ans.se = ans.fi;
        ans.fi = b;
    }
    else if (b > ans.se){
        ans.se = b;
    }
}
void dfs2(int u, int p){

    ii mx = {0, 0};

    for(auto v: g[u]){
        if (v == p) continue;
        update(mx, dp1[v] + 1);
    }

    for(auto v: g[u]){
        if (v == p) continue;
        
        if (dp1[v] + 1 == mx.fi) maximize(dp2[v], mx.se);
        else maximize(dp2[v], mx.fi);

        maximize(dp2[v], dp2[u]);
        maximize(dp2[v], (int) g[u].size() - 1);


        if (p != -1) {
            maximize(dp2[v], (int) g[p].size());
        }
    }

    maximize(res, (int) g[u].size() + dp2[u]);
    for(auto v: g[u]){
        if (v != p) maximize(res, (int)g[u].size() + dp1[v]);
    }

    for(auto v: g[u]){
        if (v == p) continue;
        dfs2(v, u);
    }
}
void solve(){
    cin >> n;
    fill(dp1 + 1, dp1 + 1 + n, 0);
    fill(dp2 + 1, dp2 + 1 + n, 0);
    res = 0;
    FOR(i, 1, n - 1){
        int u,v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs1(1, -1);
    dfs2(1, -1);
    // FOR(i, 1, n){
    //     cout << dp1[i] << ' ' << dp2[i] << endl;
    // }

    int remaining_edge = n - 1 - res;
    // cout << remaining_edge << ' ' << res << endl;
    cout << n - 2 - remaining_edge << endl;

    FOR(i, 1, n){
        g[i].clear();
    }
}
main() 
{ 
    fast; 
   if (fopen(TASKNAME".inp","r")){
        freopen(TASKNAME".inp","r",stdin); 
        freopen(TASKNAME".out","w",stdout); 
   } 

   int t; 
   cin >> t;
   while(t--){
       solve();   
   } 
} 
/**
Warning:
- MLE / TLE?
- Gioi han mang?
- Gia tri max phai luon gan cho -INF
- long long co can thiet khong?
- tran mang.
- code can than hon
**/

Submission (verdict: Accepted):
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define inf 1e18
#define fi first
#define se second
#define endl "\n"
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define pi 3.14159265359
#define TASKNAME "2063C"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; } 
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; } 
using namespace std; 
typedef pair<int,int> ii; 
typedef pair<int,ii> iii; 
typedef vector<int> vi; 
const int MAXN = 3e5 + 9;
vector<int> g[MAXN];
int n, res = 0;
int dp1[MAXN], dp2[MAXN];

void dfs1(int u, int p){
    if (p == -1) dp1[u] = g[u].size();
    else dp1[u] = g[u].size() - 1;

    for(auto v: g[u]){
        if (v == p) continue;
        dfs1(v, u);
        maximize(dp1[u],dp1[v]);
        maximize(dp1[u], (int)g[v].size());
    }
}

void update(ii &ans, int b){
    if (b > ans.fi){
        ans.se = ans.fi;
        ans.fi = b;
    }
    else if (b > ans.se){
        ans.se = b;
    }
}
void dfs2(int u, int p){

    ii mx = {0, 0};

    for(auto v: g[u]){
        if (v == p) continue;
        update(mx, max(dp1[v], (int) g[v].size()));
    }

    for(auto v: g[u]){
        if (v == p) continue;
        
        if (max(dp1[v], (int) g[v].size()) == mx.fi) maximize(dp2[v], mx.se);
        else maximize(dp2[v], mx.fi);

        maximize(dp2[v], dp2[u]);
        maximize(dp2[v], (int) g[u].size() - 1);


        if (p != -1) {
            maximize(dp2[v], (int) g[p].size());
        }
    }

    maximize(res, (int) g[u].size() + dp2[u]);
    for(auto v: g[u]){
        if (v != p) maximize(res, (int)g[u].size() + dp1[v]);
    }
    // cout << "CHECK: " << res << ' ' << u << endl;


    for(auto v: g[u]){
        if (v == p) continue;
        dfs2(v, u);
    }
}
void solve(){
    cin >> n;
    fill(dp1 + 1, dp1 + 1 + n, 0);
    fill(dp2 + 1, dp2 + 1 + n, 0);
    res = 0;
    FOR(i, 1, n - 1){
        int u,v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs1(1, -1);
    dfs2(1, -1);
    // FOR(i, 1, n){
    //     cout << "NODE: " << i << ' ' << dp1[i] << ' ' << dp2[i] << endl;
    // }

    int remaining_edge = n - 1 - res;
    // cout << remaining_edge << ' ' << res << endl;
    cout << n - 2 - remaining_edge << endl;

    FOR(i, 1, n){
        g[i].clear();
    }
}
main() 
{ 
    fast; 
   if (fopen(TASKNAME".inp","r")){
        freopen(TASKNAME".inp","r",stdin); 
        freopen(TASKNAME".out","w",stdout); 
   } 

   int t; 
   cin >> t;
   while(t--){
       solve();   
   } 
} 
/**
Warning:
- MLE / TLE?
- Gioi han mang?
- Gia tri max phai luon gan cho -INF
- long long co can thiet khong?
- tran mang.
- code can than hon
**/