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;
#define ll long long
#define MOD 1000000007

void solve() {
      int n; cin >> n;
      int k = 2, c = 0;

      vector<vector<int>> adj(n);
      vector<int> in(n);
      priority_queue<pair<int,int>> pq;
      vector<bool> vis(n);
      int d[2];

      for (int i = 0, a, b; i < n - 1; i++) { 
            cin >> a >> b; a--, b--;

            in[a]++; in[b]++;

            adj[a].push_back(b);
            adj[b].push_back(a);
      }


      for (int i = 0; i < n; i++) {
            pq.push({in[i], i});
      }

      while(!pq.empty() && k) {
            auto p = pq.top(); pq.pop();

            if(p.first != in[p.second]) continue;
    
            in[p.second] = 0;
            for(auto it : adj[p.second]) {
                  in[it]--; 
                  pq.push({in[it], it});  
            }
 
            k--;
            d[k] = p.second;
      }
      

      auto dfs = [&](auto&& dfs, int n, int p) -> void {
            vis[n] = 1;
            for(auto it : adj[n]) 
                  if(it != p && d[0] != it && d[1] != it) 
                        dfs(dfs, it, n);  
      };

      for(int i = 0; i < n; i++) {
            if(!vis[i] && d[1] != i && d[0] != i) dfs(dfs, i, -1), c++;
      }

      cout << c << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#ifndef ONLINE_JUDGE 
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

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

void solve() {
      int n; cin >> n;
      int k = 2, c = 0, mx = 0;

      vector<vector<int>> adj(n);
      vector<int> in(n);
      priority_queue<pair<int,int>> pq;
      vector<bool> vis(n);
      map<int,int> freq;
      int d[2] = {-1, -1};
      int pre;

      for (int i = 0, a, b; i < n - 1; i++) { 
            cin >> a >> b; a--, b--;

            in[a]++; in[b]++;

            adj[a].push_back(b);
            adj[b].push_back(a);
      }


      for (int i = 0; i < n; i++) {
            pq.push({in[i], i});
            freq[in[i]]++;
            mx = max(mx, freq[i]);
      }
 

      if(freq[mx] > 2)return void(cout << mx * 2  -1 << endl);

      while(!pq.empty() && k) {
            auto p = pq.top(); pq.pop(); 
            if(p.first != in[p.second]) continue; 

            for(auto it : adj[p.second]) {
                  in[it]--; 
                  pq.push({in[it], it});  
            }
 
            k--;
            d[k] = p.second; 
      } 

      auto dfs = [&](auto&& dfs, int n, int p) -> void {
            vis[n] = 1;
            for(auto it : adj[n]) 
                  if(it != p && d[0] != it && d[1] != it) 
                        dfs(dfs, it, n);  
      };

      for(int i = 0; i < n; i++) {
            if(!vis[i] && d[1] != i && d[0] != i) dfs(dfs, i, -1), c++;
      } 
      cout << c << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#ifndef ONLINE_JUDGE 
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}