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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
void dfs(int node , vector<vector<int>>& adj , vector<int>& vis , int del1 , int del2){
    vis[node]=1;
    for(auto it : adj[node]){
        if(!vis[it] && it!=del1 && it!=del2){
            dfs(it,adj,vis,del1,del2);
        }
    }
}
void solve() {
    int n;
    cin>>n;
    vector<vector<int>> adj(n+1);
    vector<int> deg(n+1,0);
    for(int i=0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
        deg[b]++;
        deg[a]++;
    }
    int maxDeg = 0;
    int node = 1;
    // for(auto it : deg) cout<<it<<" ";
    // cout<<endl;
    priority_queue<pair<int,int>> q; 
    for(int i=1;i<=n;i++){
            q.push({deg[i],i});
        if(maxDeg<=deg[i]){
            node = i;
            maxDeg=deg[i];
        }
    }
    // cout<<maxDeg<<" ";
    // cout<<"size="<<q.size()<<" ";
    int node2;
    while(!q.empty()){
        int nod = q.top().second;
        // cout<<nod<<" ";
        q.pop();
        if(nod==node) continue;
        node2= nod;
        bool flag = 0;
        for(auto it : adj[nod]){
            if(it==node){
                flag = 1;
                continue;
            }
        }
        if(flag) continue;
        for(auto it : adj[node]){
            if(it==nod){
                flag = 1;
                continue;
            }
        }
        if(flag) continue;
        node2 = nod;
        break;
    }
    // cout<<endl;
    // deg[node]=0;
    // maxDeg=0;
    // for(int i=1;i<=n;i++){
    //     if(maxDeg<=deg[i] && i!=node){
    //         node2 = i;
    //         maxDeg=deg[i];
    //     }
    // }
    // cout<<node<<" "<<node2<<endl;
    vector<int> vis(n+1,0);
    int ans = 0;
    for(int i=1;i<=n;i++){
        if(!vis[i] && i!=node && i!=node2){
            ans++;
            dfs(i,adj,vis,node,node2);
        }
    }
    cout<<ans<<endl;
    
}

int main()
{
    ios_base::sync_with_stdio(false);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cin.tie(NULL);
 
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
 
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
void dfs(int node , vector<vector<int>>& adj , vector<int>& vis , int del1 , int del2){
    vis[node]=1;
    for(auto it : adj[node]){
        if(!vis[it] && it!=del1 && it!=del2){
            dfs(it,adj,vis,del1,del2);
        }
    }
}
void solve() {
    int n;
    cin>>n;
    // vector<vector<int>> adj(n+1);
    set<pair<int,int>> adj;
    vector<int> deg(n+1,0);
    for(int i=0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        adj.insert({a,b});
        adj.insert({b,a});
        deg[b]++;
        deg[a]++;
    }
    int ans = 0;
    vector<pair<int,int>> arr;
    for(int i=1;i<=n;i++){
        arr.push_back({deg[i],i});
    }
    sort(arr.begin(),arr.end(),greater<pair<int,int>>());
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(adj.find({arr[i].second , arr[j].second})==adj.end()){
                ans = max(ans , arr[j].first + arr[i].first - 1);
                break;
            }
        }
    }
    for(auto it : adj){
        ans = max(ans , deg[it.first] +deg[it.second] - 2);
    }
    cout<<ans<<endl;
    
}

int main()
{
    ios_base::sync_with_stdio(false);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cin.tie(NULL);
 
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
 
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}