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

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

using namespace std;

#define ll long long
#define nl '\n'
#define all(a) a.begin(), a.end()
#define alli(a, r) a.begin() + r, a.end()
#define allr(a) a.rbegin(), a.rend()
const ll N = 2e5 + 7, MOD = 1e9 + 7, INF = 1e9, LG = 18;

int n, m;

vector<int>adj[N];
vector<int>parentOfLeaves(N);
void dfs(int node ,int p)
{
    if(adj[node].size()==1)
    {
        parentOfLeaves[p]++;
        return;
    }
    for(auto&i:adj[node])
        if(p!=i)dfs(i,node);
}
void sol(int tc) {

    cin>>n;
    int node;
    for(int i =1 ;i<=n;i++)
    {
        adj[i].clear();
        parentOfLeaves[i] = 0;
    }
    for(int i = 2;i<=n;i++)
    {
        int u,v;cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        if(adj[u].size()>1)node = u;
        if(adj[v].size()>1)node = v;
    }
    if(n==2)return void(cout<<0);

    dfs(node , -1);
    multiset<pair<int,int>, greater<>>ms;
    for(int i =1;i<=n;i++)
    {
        if(parentOfLeaves[i])
            ms.insert({parentOfLeaves[i] , i});
    }

   if(ms.size()==1)
       cout<<n-2;
   else
   {
       auto it = ms.begin();
       int x = it->second;
       it++;
       int y = it->second;
       if(find(all(adj[x]),y)!=adj[x].end())
       {
           cout<<adj[x].size() -1 + adj[y].size()-1;
       }else
       {
           cout<<adj[x].size() -1 + adj[y].size();

       }

   }



}

void file() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    // file();

    int tests = 1;
    cin >> tests;
    for (int tes = 1; tes <= tests; ++tes) {

        sol(tes);
        cout<<nl;

    }
    return 0;
}


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

using namespace std;

#define ll long long
#define nl '\n'
#define all(a) a.begin(), a.end()
#define alli(a, r) a.begin() + r, a.end()
#define allr(a) a.rbegin(), a.rend()
const ll N = 2e5 + 7, MOD = 1e9 + 7, INF = 1e9, LG = 18;

int n, m;

vector<int>adj[N];
void sol(int tc) {

    cin>>n;

    for(int i =1 ;i<=n;i++)
    {
        adj[i].clear();
    }
    for(int i = 2;i<=n;i++)
    {
        int u,v;cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    multiset<int>ms;
    for(int i = 1;i<=n;i++)
    {
        ms.insert(adj[i].size());
    }

    int ans = 0;
    for(int i = 1;i<=n;i++)
    {
        int sz = adj[i].size();
        ms.erase(ms.find(sz));
        for(auto&x:adj[i])
        {
            int sz2  = adj[x].size();
            ms.erase(ms.find(sz2));
            ans = max(sz + sz2 -2 ,ans);
        }

        ans = max(sz + (ms.empty() ? -sz : *ms.rbegin() -1 ) , ans);
        for(auto&x:adj[i])
        {
            int sz2  = adj[x].size();
            ms.insert(sz2);
        }
        ms.insert(sz);
    }
    cout<<ans;



}

void file() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    // file();

    int tests = 1;
    cin >> tests;
    for (int tes = 1; tes <= tests; ++tes) {

        sol(tes);
        cout<<nl;

    }
    return 0;
}