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 float long double
#define in2(x,y);  long long x,y; cin>>x>>y;
#define in3(x,y,z);  long long x,y,z; cin>>x>>y>>z;
#define fi(a,n) for(int i=a;i<n;i++)
#define fj(a,n) for(int j=a;j<n;j++)
#define fk(a,n) for(int k=a;k<n;k++)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back

using namespace std;

signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<set<int>> graph(n+1);
        fi(0,n-1)
        {
            in2(x,y);
            graph[x].insert(y);
            graph[x].insert(x);
            graph[y].insert(x);
            graph[y].insert(y);
        }
        int maxi=0;
        int ind=-1;
        vi indices;
        fi(1,n+1)
        {
            if(maxi<=graph[i].size())
           { 
            maxi = max(maxi , (int)graph[i].size());
                ind = i;
                indices.pb(i);
           }
            
        }
        int count;
        // cout<<indices.size()<<endl;
        if(indices.size()==3)
        {
            fi(0,3)
            {
                fj(0,3)
                {
                    if(graph[indices[j]].find(indices[i]) != graph[indices[j]].end())
                    count++;
                }
                if(count==2)
                {
                    ind = i;
                    break;
                }
            }
        }
        graph[ind].clear();
        fi(1,n+1)
        {
            graph[i].erase(ind);
        }
        maxi=0;
        indices.clear();
        count=0;
        fi(1,n+1)
        {
            //cout<<i<< " "<<graph[i].size()<<endl;
            if(maxi<=graph[i].size())
           { maxi = max(maxi , (int)graph[i].size());
            ind = i;
            indices.pb(i);
           }
            
        }
         if(indices.size()==3)
        {
            fi(0,3)
            {
                fj(0,3)
                {
                    if(graph[indices[j]].find(indices[i]) != graph[indices[j]].end())
                    count++;
                }
                if(count==2)
                {
                    ind = i;
                    break;
                }
            }
        }
        graph[ind].clear();
        fi(1,n+1)
        {
            graph[i].erase(ind);
        }
        // fi(0,n+1)
        // {
        //     for(auto value:graph[i])
        //     cout<<value<<" ";
        //     cout<<endl;
        // }
        queue<int> q;
        map<int,int> vis;
        int conn=0;
        int k=0;
        for(int i=1;i<=n ; i++)
        {
            if(vis[i]==0 && !graph[i].empty())
            {
            q.push(i);
            }
            else
            continue;

            vis[i]=1;
            while(!q.empty())
            {
                int first = q.front();
                q.pop();
                
                if(!graph[first].empty())
                {
                    for(auto value:graph[first])
                    {
                        if(vis[value]==0)
                        q.push(value);
                        vis[value]=1;
                    }
                    k=1;
                }
                
            }
            conn+=k;
            k=0;
        }
        cout<<conn<<endl;
    }
    return 0;
}


Submission (verdict: Accepted):
#include<bits/stdc++.h>
#define int long long 
#define float long double
#define in2(x,y);  long long x,y; cin>>x>>y;
#define in3(x,y,z);  long long x,y,z; cin>>x>>y>>z;
#define fi(a,n) for(int i=a;i<n;i++)
#define fj(a,n) for(int j=a;j<n;j++)
#define fk(a,n) for(int k=a;k<n;k++)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back

using namespace std;

signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<set<int>> graph(n+1);
        fi(0,n-1)
        {
            in2(x,y);
            graph[x].insert(y);
            graph[x].insert(x);
            graph[y].insert(x);
            graph[y].insert(y);
        }
        int maxi=0;
        int ind=-1;
        vi indices;
        fi(1,n+1)
        {
            if(maxi<=graph[i].size())
           { 
                maxi = max(maxi , (int)graph[i].size());
                ind = i;
                //indices.pb(i);
           }
            
        }
        fi(1,n+1)
        {
            if(maxi== graph[i].size())
            indices.pb(i);
        }
        int count=0;
        // cout<<indices.size()<<endl;
        if(indices.size()==3)
        {
            fi(0,3)
            {
                fj(0,3)
                {
                    if(graph[indices[j]].find(indices[i]) != graph[indices[j]].end())
                    count++;
                }
                if(count<=2)
                {
                    ind = indices[i];
                    break;
                }
            }
        }
        //cout<<ind<<"-"<<endl;
        graph[ind].clear();
        fi(1,n+1)
        {
            graph[i].erase(ind);
        }
        maxi=0;
        indices.clear();
        count=0;
        fi(1,n+1)
        {
            //cout<<i<< " "<<graph[i].size()<<endl;
            if(maxi<=graph[i].size())
           { maxi = max(maxi , (int)graph[i].size());
            ind = i;
            // indices.pb(i);
           }
            
        }
        fi(1,n+1)
        {
            if(maxi== graph[i].size())
            indices.pb(i);
        }
        //cout<<indices.size()<< " i"<<endl;
         if(indices.size()==3)
        {
            fi(0,3)
            {
                fj(0,3)
                {
                    if(graph[indices[j]].find(indices[i]) != graph[indices[j]].end())
                    count++;
                }
                if(count==2)
                {
                    ind = indices[i];
                    break;
                }
            }
        }
        //cout<<ind<<"--"<<endl;
        graph[ind].clear();
        fi(1,n+1)
        {
            graph[i].erase(ind);
        }
        // fi(0,n+1)
        // {
        //     for(auto value:graph[i])
        //     cout<<value<<" ";
        //     cout<<endl;
        // }
        queue<int> q;
        map<int,int> vis;
        int conn=0;
        int k=0;
        for(int i=1;i<=n ; i++)
        {
            if(vis[i]==0 && !graph[i].empty())
            {
            q.push(i);
            }
            else
            continue;

            vis[i]=1;
            while(!q.empty())
            {
                int first = q.front();
                q.pop();
                
                if(!graph[first].empty())
                {
                    for(auto value:graph[first])
                    {
                        if(vis[value]==0)
                        q.push(value);
                        vis[value]=1;
                    }
                    k=1;
                }
                
            }
            conn+=k;
            k=0;
        }
        cout<<conn<<endl;
    }
    return 0;
}