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

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

typedef long long ll;
typedef pair<int , int> PII;

const int INF = 0x3f3f3f3f;
const int N = 2e5 + 9;

int n;
vector<int> node[N];
int in[N];
bool flag[N];

struct Node
{
    int no , val;
    bool operator < (const Node temp) const
    {
        return val > temp.val;
    }
}a[N];

void solve()
{
    cin >> n;
    for(int i = 1 ; i <= n ; i++)
    {
        node[i].clear();
        in[i] = 0;
        flag[i] = false;
    }
    for(int i = 1 ; i < n ; i++)
    {
        int x , y;
        cin >> x >> y;
        node[x].push_back(y);
        node[y].push_back(x);
        in[x]++ , in[y]++;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        a[i] = {i , in[i]};
    }
    sort(a + 1 , a + 1 + n);    
    int ans = 0 , cnt = 0;
    for(int i = 1 ; i <= n ; i ++)
    {
        if(a[i].val == a[1].val)
        {
            cnt++;
        }
        else
        {
            break;
        }
    }
    if(cnt <= 2)
    {
        int flag = 0;
        for(auto to : node[a[1].no])
        {
            if(to == a[2].no)
            {
                flag = -1;
            }
        }
        cout << a[1].val + a[2].val + flag - 1 << "\n";
    }
    else 
    {
        cout << a[1].val + a[2].val - 1 << "\n";
    }
}

int main()
{
    cin.tie(0) -> sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int , int> PII;

const int INF = 0x3f3f3f3f;
const int N = 2e5 + 9;

int n;
vector<int> node[N];
int in[N];
bool flag[N];

struct Node
{
    int no , val;
    bool operator < (const Node temp) const
    {
        return val > temp.val;
    }
}a[N];

void solve()
{
    cin >> n;
    for(int i = 1 ; i <= n ; i++)
    {
        node[i].clear();
        in[i] = 0;
        flag[i] = false;
    }
    for(int i = 1 ; i < n ; i++)
    {
        int x , y;
        cin >> x >> y;
        node[x].push_back(y);
        node[y].push_back(x);
        in[x]++ , in[y]++;
    }
    for(int i = 1 ; i <= n ; i++)
    {
        a[i] = {i , in[i]};
    }
    sort(a + 1 , a + 1 + n);    
    int ans = 0 , cnt = 0;
    for(int i = 1 ; i <= n ; i ++)
    {
        if(a[i].val == a[1].val)
        {
            cnt++;
        }
        else
        {
            break;
        }
    }
    if(cnt == 1)
    {
        int tag = 0;
        for(auto to : node[a[1].no])
        {
            flag[to] = true;
        }
        for(int i = 2 ; i <= n ; i++)
        {
            if(!flag[a[i].no])
            {
                tag = i;
                break;
            }
        }
        cout << max(a[1].val + a[2].val - 1 , a[1].val + a[tag].val) - 1 << '\n';
    }
    else if(cnt == 2)
    {
        int flag = 0;
        for(auto to : node[a[1].no])
        {
            if(to == a[2].no)
            {
                flag = -1;
            }
        }
        cout << a[1].val + a[2].val + flag - 1 << "\n";
    }
    else 
    {
        cout << a[1].val + a[2].val - 1 << "\n";
    }
}

int main()
{
    cin.tie(0) -> sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}