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

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

typedef long long ll;

class DSU {
public: 
    vector<int> rank, par; 
    DSU(int n) 
    {
        rank.resize(n+1, 0); 
        par.resize(n+1);
        for(int i=0; i<=n; i++) par[i] = i;
    }

    int findUPar(int node) 
    {
        if(node==par[node]) return node; 
        return par[node] = findUPar(par[node]); 
    }

    void unionByRank(int u, int v) 
    {
        int ulp_u = findUPar(u); 
        int ulp_v = findUPar(v); 

        if(ulp_u==ulp_v) return;

        if(rank[ulp_u]<rank[ulp_v]) par[ulp_u] = ulp_v; 
        else if(rank[ulp_v]<rank[ulp_u])  par[ulp_v] = ulp_u;
        else 
        {
            par[ulp_v] = ulp_u; 
            rank[ulp_u]++; 
        }
    }
}; 

int main()
{
    int test;
    cin >> test;

    while(test--)
    {
		int n;
		cin >> n;
		vector<pair<int,int>> g1;
        vector<pair<int,int>> indegree(n+1);

        for(int i=1; i<=n; i++) indegree[i].second = i;

		for(int i=0; i<n-1; i++)
		{
			int u,v;
			cin >> u >> v;
			g1.push_back({u,v});
            indegree[u].first++;
            indegree[v].first++;
		}

        if(n==2) cout << 0;
        else if(n==3) cout << 1;
        else
        {
            sort(indegree.begin(), indegree.end());
            int v1 = indegree[n].second;

            vector<pair<int,int>> g2;
            vector<pair<int,int>> indegree1(n+1);
            for(int i=1; i<=n; i++) indegree1[i].second = i;

            for(auto it:g1)
            {
                int u = it.first;
                int v = it.second;

                if(u==v1 || v==v1) continue;
                else 
                {
                    g2.push_back({u,v});
                    indegree1[u].first++;
                    indegree1[v].first++;
                }
            }

            sort(indegree1.begin(), indegree1.end());
            int v2 = indegree1[n].second;

            int ans=0;
            DSU ds1(n);
            for(auto it:g2)
            {
                int u = it.first;
                int v = it.second;

                if(u==v2 || v==v2) continue;
                else ds1.unionByRank(u,v);
            }

            for(int i=1; i<=n; i++)
            {
                if(!(i==v1 || i==v2) && ds1.par[i]==i) ans++;
            }
            
            if(g2.size()) cout << ans;
            else cout << n-2;
        }
        if(test) cout << '\n';
    }
    return 0;
}

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

typedef long long ll;

int main()
{
    int test;
    cin >> test;

    while(test--)
    {
		int n;
		cin >> n;
		map<int,int> g[n+1];
        vector<pair<int,int>> indegree(n+1);

        for(int i=1; i<=n; i++) indegree[i].second = i;

		for(int i=0; i<n-1; i++)
		{
			int u,v;
			cin >> u >> v;
            g[u][v] = 1;
            g[v][u] = 1;
            indegree[u].first++;
            indegree[v].first++;
		}

        sort(indegree.begin()+1, indegree.end(), greater<>());

        int ans=0;
        for(int i=1; i<=min(n, 3); i++)
        {
            for(int j=i+1; j<=n; j++)
            {
                int d1 = indegree[i].first;
                int u = indegree[i].second;
                int d2 = indegree[j].first;
                int v = indegree[j].second;
                ans = max(ans, d1+d2-1-g[u][v]);
            }
        }

        cout << ans;

        if(test) cout << '\n';
    }
    return 0;
}