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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

// अहं कृत्स्नस्य जगतः प्रभवः प्रलयस्तथा ।।6।।

using namespace std;

#define IOS                       \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define endl "\n"
#define int long long
typedef unsigned long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define fi first
#define se second
#define all(arr) arr.begin(), arr.end()
#define MP make_pair
#define pb push_back
#define repeat(i, st, n) for (int i = st; i < n; i++)
const int mod = 1e9 + 7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void print(vi &v)
{
    for (auto it : v)
        cout << it << " ";
    cout << endl;
}
vector<int> temp;
vector<vector<int>> cc;
void dfs(int vertex, vector<bool> & vis,vector<pair<int, vector<int>>> &v){
    vis[vertex] = true;
    temp.pb(vertex);
    for(auto child : v[vertex].second){
        if(vis[child]) continue;
        dfs(child, vis, v);
    }
}
int connectedComponent(vector<pair<int, vector<int>>> &v, int n){
    vector<bool> vis(n + 1, false); int ans = 0;
    for(int i = 1; i <= n; i++){
        if(!vis[i]){
            ans++;
            temp.clear();
            dfs(i, vis, v);
            cc.pb(temp);
        }
    }
    return ans;
}
void solve()
{
    int n; cin >> n;
    vector<pair<int,vector<int>>> v(n + 1);
    for(int i = 0; i < n - 1; i++){
        int x, y; cin >> x >> y;
        v[x].second.pb(y);
        v[y].second.pb(x);
    }
    for(int i = 1; i <= n; i++) v[i].first = v[i].second.size();
    vector<int> degree(n + 1);
    for(int i = 1; i <= n; i++) degree[i] = v[i].first;
    // print(degree); 
    int ind = 0, deg = 0;
    for(int i = 1; i <= n; i++){
        if(v[i].first > deg){
            deg = v[i].first;
            ind = i;
        }
    }
    vector<int> visited(n + 1, 0);
    visited[ind] = 1;
    for(auto it : v[ind].second) visited[it] = 1;
    v[ind].first = 0;
    int ans = deg; 
    int deg1 = 0, deg2 = 0;
    bool flag = 0;
    for(int i = 1; i <= n; i++){\
        // cout << v[i].first << " ";
        if(visited[i] == 0){
            if(v[i].first > deg1){
                deg1 = v[i].first;
            }
        } else {
            if(v[i].first > deg2){
                deg2 = v[i].first;
            }
        }
    }
    cout << max(ans + deg1 - 1, ans + deg2 - 2) << endl;
}

signed main()
{
    IOS

#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

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

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

// अहं कृत्स्नस्य जगतः प्रभवः प्रलयस्तथा ।।6।।

using namespace std;

#define IOS                       \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define endl "\n"
#define int long long
typedef unsigned long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define fi first
#define se second
#define all(arr) arr.begin(), arr.end()
#define MP make_pair
#define pb push_back
#define repeat(i, st, n) for (int i = st; i < n; i++)
const int mod = 1e9 + 7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void print(vi &v)
{
    for (auto it : v)
        cout << it << " ";
    cout << endl;
}
void solve()
{
    int n; cin >> n;
    vector<vector<int>> v(n + 1);
    for(int i = 0; i < n - 1; i++){
        int x, y; cin >> x >> y;
        v[x].pb(y);
        v[y].pb(x);
    }
    vector<int> degree(n + 1, 0);
    for(int i = 1; i <= n; i++) degree[i] = v[i].size();
    int ind  = 0, deg = 0;
    for(int i = 1; i <= n; i++){
        if(degree[i] > deg){
            deg = degree[i];
            ind = i;
        }
    }
    vector<int> vis(n + 1, 0);
    for(auto it : v[ind]) vis[it] = 1;
    vis[ind] = 1, degree[ind] = 0;
    int deg1 = 0, deg2 = 0;
    for(int i = 1; i <= n; i++){
        if(vis[i] == 0){
            if(degree[i] > deg1){
                deg1 = degree[i];
            }
        } else {
            if(degree[i] > deg2){
                deg2 = degree[i];
            }
        }
    }
    cout << max(deg + deg1 - 1, deg + deg2 - 2) << endl;
}

signed main()
{
    IOS

#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

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