Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 1):
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define _mp make_pair
#define vt vector
#define MAXN 200002
using namespace std;
typedef long long LL;
ll mod=1e9+7;
//     f[0]=f[1]=inv[0]=inv[1]=1;
// for(int i=2;i<MAXN;i++) f[i]=(LL)f[i-1]*i%mod,inv[i]=(LL)(mod-mod/i)*inv[mod%i]%mod;
// for(int i=1;i<MAXN;i++) inv[i]=(LL)inv[i-1]*inv[i]%mod;
ll f[MAXN],inv[MAXN];

ll C(const int n,const int m){return n<m?0:(LL)f[n]*inv[m]%mod*inv[n-m]%mod;}

ll power(ll num, ll p)
{   if(p<0)return 0;
    if (p == 0)
        return 1;
    if (p == 1)
        return num;
    ll n = power(num, p / 2);
    if (p % 2 == 0)
        return (n * n)%(1000000007) ;
    return (num*((n*n)%(1000000007)))%(1000000007);
}
ll fact(ll val)
{
    if (val <= 1)
        return 1;
    return (val * fact(val - 1));
}
ll ncr(ll n, ll r)
{
    if (n < r)
        return 0;
    ll p = 1, k = 1;
    if ((n - r) < r)
        r = n - r;

    if (r != 0)
    {
        while (r)
        {
            p = (p * n)%mod;
            k = (k * r)%mod;
            ll m = __gcd(p, k);
            p /= m;
            k /= m;
            n--;
            r--;
        }
    }
    else
        p = 1;
    return p;
}
// bool cmp(pair<int,int> v1, pair<int,int> v2)
// {
//     if(v1.first==v2.first)return v1.second>v2.second;
//     return v1.first<v2.first;
// }
bool isFact(ll n)
{
  for (ll i = 1;; i++) {
    if (n % i == 0) {
      n /= i;
    }
    else {
      break;
    }
  }
 
  if (n == 1LL) {
    return true;
  }
  else {
    return false;
  }
}
int lcs(string s, string t, int m, int n)
{
    if (m==0||n==0)return 0;
    if (s[m-1]==t[n-1])return 1 + lcs(s, t, m - 1, n - 1);
    else return max(lcs(s, t, m, n - 1),lcs(s, t, m - 1, n));
}
pair<ll,ll> maxSubArraySum(vector<ll>&a, int size)
{
    ll max_so_far = 0, max_ending_here = 0;
     int num=0,mx_len=0;
    for (int i = 0; i < size; i++) {
        max_ending_here = (max_ending_here + a[i]);
        num++;
        if (max_so_far < max_ending_here)
            {max_so_far = max_ending_here;mx_len=num;}
        else{
            max_ending_here=0;num=0;
        }
    }
    if(mx_len==size)max_so_far=0;
    return {max_so_far,mx_len};
}
bool isPrime(ll N){
    int flag = 1;

    double sqroot = sqrt(N);

    // Iterate from 2 to sqrt(n)
    for (int i = 2; i <= sqroot; i++) {

        // If n is divisible by any number between 2 and
        // sqrt(n), it is not prime
        if (N % i == 0) {
            flag = 0;
            break;
        }
    }
    return flag;
}
void rev(int start,int ind,int end,vt<int>&vec){
int val=ind+1;
while(val>0){
    for(int i=start;i<end;i++)swap(vec[i],vec[i+1]);
    val--;
}
}
int gcdExtended(ll a, ll b, ll* x, ll* y)
{

    // Base Case
    if (a == 0) {
        *x = 0, *y = 1;
        return b;
    }

    // To store results of recursive call
    ll x1, y1;
    int gcd = gcdExtended(b % a, a, &x1, &y1);

    // Update x and y using results of recursive
    // call
    *x = y1 - (b / a) * x1;
    *y = x1;

    return gcd;
}
ll modInverse(ll A, ll M)
{
    ll x, y;
    ll g = gcdExtended(A, M, &x, &y);
    if (g != 1)
        return -1;
    else {

        // m is added to handle negative x
        ll res = (x % M + M) % M;
        return res;
    }
}



// bool fun(int a,int b,int X,int Y){
//     double dis1=
// }


int countAndMerge(vector<int>& arr, int l, int m, int r) {
  
    // Counts in two subarrays
    int n1 = m - l + 1, n2 = r - m;

    // Set up two vectors for left and right halves
    vector<int> left(n1), right(n2);
    for (int i = 0; i < n1; i++)
        left[i] = arr[i + l];
    for (int j = 0; j < n2; j++)
        right[j] = arr[m + 1 + j];

    // Initialize inversion count (or result) and merge two halves
    int res = 0;
    int i = 0, j = 0, k = l;
    while (i < n1 && j < n2) {

        // No increment in inversion count if left[] has a 
        // smaller or equal element
        if (left[i] <= right[j]) 
            arr[k++] = left[i++];
      
        // If right is smaller, then it is smaller than n1-i 
          // elements because left[] is sorted
        else {
            arr[k++] = right[j++];
            res += (n1 - i);
        }
    }

    // Merge remaining elements
    while (i < n1)
        arr[k++] = left[i++];
    while (j < n2)
        arr[k++] = right[j++];

    return res;
}

// Function to count inversions in the array
ll countInv(vector<int>& arr, int l, int r){
    ll res = 0;
    if (l < r) {
        int m = (r + l) / 2;

        // Recursively count inversions in the left and 
        // right halves
        res += countInv(arr, l, m);
        res += countInv(arr, m + 1, r);

        // Count inversions such that greater element is in 
          // the left half and smaller in the right half
        res += countAndMerge(arr, l, m, r);
    }
    return res;
}

ll inversionCount(vector<int>& arr) {
      int n = arr.size();
      return countInv(arr, 0, n-1);
}
ll p(ll x,ll y){
    if(y==0)return 1;
    if(y==1)return x;
    ll z=p(x,y/2);
    ll r=(z*z)%(998244353);
    if(y%2)r=(r*x)%(998244353);
    return r;
}

vt<int>graph[1000+1];
bool cmp(pair<int,int>a,pair<int,int>b){
    if(a.first==b.first){
       return a.second>b.second;
    }
    else return a.first<b.first;
}
ll check(ll cnt){
    ll n=pow(2,cnt)-1;
    return n*(n+1)/2;
}
int fun(int val,int op){
    if(op==0)return val;
    if(op==1)
    {
    int sum=0;
    while(val>0){
        sum+=val%10;
        val/=10;
    }
    return sum;
    }
    else{
        int sum=0;
        while(val>0){
            sum+=val%10;
            val/=10;
        }
        int sum2=0;
        while(sum>0){
            sum2+=(sum)%10;
            sum/=10;
        } 
        return sum2;
    }
}
bool dfs(vt<int>&vis,int n,set<int>graph[]){
    vis[n]=1;
    for(auto it:graph[n]){
        if(vis[it]==0)dfs(vis,it,graph);
    }
}
void _solve1()
{
    int t;cin>>t;
    while(t--){
      int n;cin>>n;
      set<int>graph[n+1];
      for(int i=0;i<n-1;i++){
        int u,v;cin>>u>>v;
        graph[u].insert(v);
        graph[v].insert(u);
      }
      vt<pair<int,int>>s1;
      for(int i=1;i<=n;i++){
        s1.pb({graph[i].size(),i});
      }
      sort(s1.begin(),s1.end());
      int d1=s1[n-1].second;
      for(int i=1;i<=n;i++){
        if(graph[i].find(d1)!=graph[i].end())graph[i].erase(d1);
      }
      s1.clear();
      for(int i=1;i<=n;i++){
        if(i!=d1){
            s1.pb({graph[i].size(),i});
        }
      }
      sort(s1.begin(),s1.end());
      int d2=s1[n-2].second;
      for(int i=1;i<=n;i++){
        if(i==d1)continue;
        if(graph[i].find(d2)!=graph[i].end())graph[i].erase(d2);
      }
      vt<int>vis(n+1,0);
      int cnt=0;
      for(int i=1;i<=n;i++){
        if(i==d1||i==d2)continue;
        if(vis[i]==0){dfs(vis,i,graph);cnt++;}
      }
      cout<<cnt<<'\n';
      }
}
void _solve2(){
int a=134,b=132,c=137;
cout<<((a^b)+(b^c)+(c^a))<<'\n';
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    _solve1();
    // _solve2();
}

Submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define _mp make_pair
#define vt vector
#define MAXN 200002
using namespace std;
typedef long long LL;
ll mod=1e9+7;
//     f[0]=f[1]=inv[0]=inv[1]=1;
// for(int i=2;i<MAXN;i++) f[i]=(LL)f[i-1]*i%mod,inv[i]=(LL)(mod-mod/i)*inv[mod%i]%mod;
// for(int i=1;i<MAXN;i++) inv[i]=(LL)inv[i-1]*inv[i]%mod;
ll f[MAXN],inv[MAXN];

ll C(const int n,const int m){return n<m?0:(LL)f[n]*inv[m]%mod*inv[n-m]%mod;}

ll power(ll num, ll p)
{   if(p<0)return 0;
    if (p == 0)
        return 1;
    if (p == 1)
        return num;
    ll n = power(num, p / 2);
    if (p % 2 == 0)
        return (n * n)%(1000000007) ;
    return (num*((n*n)%(1000000007)))%(1000000007);
}
ll fact(ll val)
{
    if (val <= 1)
        return 1;
    return (val * fact(val - 1));
}
ll ncr(ll n, ll r)
{
    if (n < r)
        return 0;
    ll p = 1, k = 1;
    if ((n - r) < r)
        r = n - r;

    if (r != 0)
    {
        while (r)
        {
            p = (p * n)%mod;
            k = (k * r)%mod;
            ll m = __gcd(p, k);
            p /= m;
            k /= m;
            n--;
            r--;
        }
    }
    else
        p = 1;
    return p;
}
// bool cmp(pair<int,int> v1, pair<int,int> v2)
// {
//     if(v1.first==v2.first)return v1.second>v2.second;
//     return v1.first<v2.first;
// }
bool isFact(ll n)
{
  for (ll i = 1;; i++) {
    if (n % i == 0) {
      n /= i;
    }
    else {
      break;
    }
  }
 
  if (n == 1LL) {
    return true;
  }
  else {
    return false;
  }
}
int lcs(string s, string t, int m, int n)
{
    if (m==0||n==0)return 0;
    if (s[m-1]==t[n-1])return 1 + lcs(s, t, m - 1, n - 1);
    else return max(lcs(s, t, m, n - 1),lcs(s, t, m - 1, n));
}
pair<ll,ll> maxSubArraySum(vector<ll>&a, int size)
{
    ll max_so_far = 0, max_ending_here = 0;
     int num=0,mx_len=0;
    for (int i = 0; i < size; i++) {
        max_ending_here = (max_ending_here + a[i]);
        num++;
        if (max_so_far < max_ending_here)
            {max_so_far = max_ending_here;mx_len=num;}
        else{
            max_ending_here=0;num=0;
        }
    }
    if(mx_len==size)max_so_far=0;
    return {max_so_far,mx_len};
}
bool isPrime(ll N){
    int flag = 1;

    double sqroot = sqrt(N);

    // Iterate from 2 to sqrt(n)
    for (int i = 2; i <= sqroot; i++) {

        // If n is divisible by any number between 2 and
        // sqrt(n), it is not prime
        if (N % i == 0) {
            flag = 0;
            break;
        }
    }
    return flag;
}
void rev(int start,int ind,int end,vt<int>&vec){
int val=ind+1;
while(val>0){
    for(int i=start;i<end;i++)swap(vec[i],vec[i+1]);
    val--;
}
}
int gcdExtended(ll a, ll b, ll* x, ll* y)
{

    // Base Case
    if (a == 0) {
        *x = 0, *y = 1;
        return b;
    }

    // To store results of recursive call
    ll x1, y1;
    int gcd = gcdExtended(b % a, a, &x1, &y1);

    // Update x and y using results of recursive
    // call
    *x = y1 - (b / a) * x1;
    *y = x1;

    return gcd;
}
ll modInverse(ll A, ll M)
{
    ll x, y;
    ll g = gcdExtended(A, M, &x, &y);
    if (g != 1)
        return -1;
    else {

        // m is added to handle negative x
        ll res = (x % M + M) % M;
        return res;
    }
}



// bool fun(int a,int b,int X,int Y){
//     double dis1=
// }


int countAndMerge(vector<int>& arr, int l, int m, int r) {
  
    // Counts in two subarrays
    int n1 = m - l + 1, n2 = r - m;

    // Set up two vectors for left and right halves
    vector<int> left(n1), right(n2);
    for (int i = 0; i < n1; i++)
        left[i] = arr[i + l];
    for (int j = 0; j < n2; j++)
        right[j] = arr[m + 1 + j];

    // Initialize inversion count (or result) and merge two halves
    int res = 0;
    int i = 0, j = 0, k = l;
    while (i < n1 && j < n2) {

        // No increment in inversion count if left[] has a 
        // smaller or equal element
        if (left[i] <= right[j]) 
            arr[k++] = left[i++];
      
        // If right is smaller, then it is smaller than n1-i 
          // elements because left[] is sorted
        else {
            arr[k++] = right[j++];
            res += (n1 - i);
        }
    }

    // Merge remaining elements
    while (i < n1)
        arr[k++] = left[i++];
    while (j < n2)
        arr[k++] = right[j++];

    return res;
}

// Function to count inversions in the array
ll countInv(vector<int>& arr, int l, int r){
    ll res = 0;
    if (l < r) {
        int m = (r + l) / 2;

        // Recursively count inversions in the left and 
        // right halves
        res += countInv(arr, l, m);
        res += countInv(arr, m + 1, r);

        // Count inversions such that greater element is in 
          // the left half and smaller in the right half
        res += countAndMerge(arr, l, m, r);
    }
    return res;
}

ll inversionCount(vector<int>& arr) {
      int n = arr.size();
      return countInv(arr, 0, n-1);
}
ll p(ll x,ll y){
    if(y==0)return 1;
    if(y==1)return x;
    ll z=p(x,y/2);
    ll r=(z*z)%(998244353);
    if(y%2)r=(r*x)%(998244353);
    return r;
}

vt<int>graph[1000+1];
bool cmp(pair<int,int>a,pair<int,int>b){
    if(a.first==b.first){
       return a.second>b.second;
    }
    else return a.first<b.first;
}
ll check(ll cnt){
    ll n=pow(2,cnt)-1;
    return n*(n+1)/2;
}
int fun(int val,int op){
    if(op==0)return val;
    if(op==1)
    {
    int sum=0;
    while(val>0){
        sum+=val%10;
        val/=10;
    }
    return sum;
    }
    else{
        int sum=0;
        while(val>0){
            sum+=val%10;
            val/=10;
        }
        int sum2=0;
        while(sum>0){
            sum2+=(sum)%10;
            sum/=10;
        } 
        return sum2;
    }
}
void dfs(vt<int>&vis,int n,set<int>graph[]){
    vis[n]=1;
    for(auto it:graph[n]){
        if(vis[it]==0)dfs(vis,it,graph);
    }
}
void _solve1()
{
    int t;cin>>t;
    while(t--){
      int n;cin>>n;
      set<int>graph[n+1];
      for(int i=0;i<n-1;i++){
        int u,v;cin>>u>>v;
        graph[u].insert(v);
        graph[v].insert(u);
      }
      vt<pair<int,int>>s1;
      for(int i=1;i<=n;i++){
        s1.pb({graph[i].size(),i});
      }
      sort(s1.begin(),s1.end());
      int d1=s1[n-1].second;
      for(int i=1;i<=n;i++){
        if(graph[i].find(d1)!=graph[i].end())graph[i].erase(d1);
      }
      s1.clear();
      for(int i=1;i<=n;i++){
        if(i!=d1){
            s1.pb({graph[i].size(),i});
        }
      }
      sort(s1.begin(),s1.end());
      int d2=s1[n-2].second;
      for(int i=1;i<=n;i++){
        if(i==d1)continue;
        if(graph[i].find(d2)!=graph[i].end())graph[i].erase(d2);
      }
      vt<int>vis(n+1,0);
      int cnt=0;
      for(int i=1;i<=n;i++){
        if(i==d1||i==d2)continue;
        if(vis[i]==0){dfs(vis,i,graph);cnt++;}
      }
      cout<<cnt<<'\n';
      }
}
void _solve2(){
int a=134,b=132,c=137;
cout<<((a^b)+(b^c)+(c^a))<<'\n';
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    _solve1();
    // _solve2();
}