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 int long long

void dfs(int node, int pp , vector<int>&vis, vector<int>graph[]){
    
    vis[node] = 1;
    for(auto ch : graph[node]){
        if(ch == pp)continue ;
        if(!vis[ch]){
            dfs(ch,node,vis,graph);
        }
    }
}

signed main(){
    int t;
    cin>>t;
    
    while(t--){
        
       int n ; cin>>n ;
       
       vector<int>graph[n+1];
       vector<int>indegree(n+1,0);
       vector<pair<int,int>>edges;
       
       for(int i = 1 ; i <= (n-1); i++){
           int u,v ;
           cin>>u>>v;
           indegree[u]++;
           indegree[v]++;
           edges.push_back({u,v});
       }
       
       int maxnode = -1 ; int maxvalue = -1 ;
       
       for(int i =1 ; i<= n ; i++){
           if(maxvalue < indegree[i]){
               maxvalue = indegree[i];
               maxnode = i ;
           }
       }
       
       int node1 = maxnode ;
       
       for(auto v : graph[maxnode]){
           indegree[v]--;
       }
       
       maxnode = -1 ; maxvalue = - 1;
       
       for(int i = 1 ; i<= n ; i++){
           
           if(i == node1)continue ;
           if(maxvalue < indegree[i]){
               maxvalue = indegree[i];
               maxnode = i ;
           }
       }
       
       int node2 = maxnode ;
       
       for(auto y : edges){
           
           if(y.first == node1 || y.second == node1){
               continue ;
           }
           
           if(y.first == node2 || y.second == node2){
               continue ;
           }
           
           graph[y.first].push_back(y.second);
           graph[y.second].push_back(y.first);
       }
       
    //   cout<<node1<<" "<<node2<<endl;
       
       int cc = 0 ;
       vector<int>vis(n+1,0);
       
      for(int i = 1 ; i <= n ; i++){
          if(i == node1 || i == node2){
              continue ;
          }
          if(vis[i] == 0){
              dfs(i,i,vis,graph);
              cc++;
          }
      }
       
       cout<<cc<<endl;
       
       
    }
}


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long

void dfs(int node, int pp , vector<int>&vis, vector<int>graph[]){
    
    vis[node] = 1;
    for(auto ch : graph[node]){
        if(ch == pp)continue ;
        if(!vis[ch]){
            dfs(ch,node,vis,graph);
        }
    }
}

signed main(){
    int t;
    cin>>t;
    
    while(t--){
        
       int n ; cin>>n ;
       
     
       vector<int>indegree(n+1,0);
       
       priority_queue<int>pq;
       for(int i = 1 ; i <= (n-1); i++){
           int u,v ;
           cin>>u>>v;
           indegree[u]++;
           indegree[v]++;
       }
       
       for(int i =1 ; i<=n ; i++){
           pq.push(indegree[i]);
       }
       
       if(n == 2){
           cout<<0<<endl;
       }
       else{
           int a = pq.top();pq.pop();
           int b = pq.top();pq.pop();
           int ans = a+b-1 ;
           if(indegree[b] == 1){
               ans--;
           }
           cout<<ans<<endl;
       }
       
       
    }
}