Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 26):
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
using ll=long long;

int main(){
    ios::sync_with_stdio(false);
	cin.tie(nullptr);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        vector<int> a(n+1,0);
        vector<vector<int>> adj(n+1);
        ll mx=0,cur=0;
        
        vector<int> pos;
        for(int i=1;i<n;i++){
            int u,v;
            cin >> u >> v;
            a[u]++;
            a[v]++;
            if(a[u]>cur){
                cur=a[u],mx=u;
                pos.clear();
                pos.push_back(u);
            }else if(a[u]==cur){
                pos.push_back(u);
            }
            if(a[v]>cur){
                cur=a[v],mx=v;
                pos.clear();
                pos.push_back(v);
            }else if(a[v]==cur){
                pos.push_back(v);
            }
            adj[u].push_back(v);
            adj[v].push_back(u);
        }

        ll ans=a[mx];
        vector<int> singles(pos.size(),0);
        for(int i=0;i<pos.size();i++){
            for(auto& ver:adj[pos[i]]){
                if(a[ver]==1) singles[i]++;
            }
        }
        int most=-1;
        for(int i=0;i<pos.size();i++){
            if(singles[i]>most) mx=pos[i],most=singles[i];
        }
        a[mx]=0;
        for(auto& vert:adj[mx]){
            a[vert]--;
        }
        mx=0,cur=0;
        for(int i=0;i<n+1;i++){
            if(a[i]>cur){
                cur=a[i],mx=i;
            }
        }
        ans+=cur-1;
        cout << ans << "\n";
    }
}



Submission (verdict: Accepted):
#include <iostream>
#include <cmath>
#include <vector>
#include <unordered_set>
using namespace std;
using ll=long long;
unordered_set<int> visited;
void dfs(int cur,vector<vector<int>>& adj){
    visited.emplace(cur);
    for(auto& x:adj[cur]){
        if(visited.find(x)==visited.end()) dfs(x,adj);
    }
}
int main(){
    ios::sync_with_stdio(false);
	cin.tie(nullptr);
    int t;
    cin >> t;
    while(t--){
        int n,visits=-1;
        visited.clear();
        cin >> n;
        vector<int> a(n+1,0);
        vector<vector<int>> adj(n+1);
        ll mx=0,cur=0;
        
        vector<int> pos;
        for(int i=1;i<n;i++){
            int u,v;
            cin >> u >> v;
            a[u]++;
            a[v]++;
            if(a[u]>cur){
                cur=a[u],mx=u;
                pos.clear();
                pos.push_back(u);
            }else if(a[u]==cur){
                pos.push_back(u);
            }
            if(a[v]>cur){
                cur=a[v],mx=v;
                pos.clear();
                pos.push_back(v);
            }else if(a[v]==cur){
                pos.push_back(v);
            }
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        for(int i=1;i<n+1;i++){
            if(visited.find(i)==visited.end()){
                dfs(i,adj);
                visits++;
            }
        }
        ll ans=a[mx]+visits;
        vector<int> singles(pos.size(),0);
        unordered_set<int> poses;
        for(auto&x:pos)poses.emplace(x);
        for(int i=0;i<pos.size();i++){
            for(auto& ver:adj[pos[i]]){
                if(poses.find(ver)==poses.end()) singles[i]++;
            }
        }
        int most=-1;
        for(int i=0;i<pos.size();i++){
            if(singles[i]>most) mx=pos[i],most=singles[i];
        }
        a[mx]=0;
        for(auto& vert:adj[mx]){
            a[vert]--;
        }
        mx=0,cur=0;
        for(int i=0;i<n+1;i++){
            if(a[i]>cur){
                cur=a[i],mx=i;
            }
        }
        ans+=cur-1;
        cout << ans << "\n";
    }
}