ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)


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

#define all(v) v.begin(), v.end()
#define debug(x) cout << #x << '=' << x << endl;
#define pb push_back
#define int long long
const long long M = 998244353;

void solve(){
    int n; cin>>n;

    vector<vector<int>> vec(n+1);
    unordered_set<int> st;
    for(int i = 1; i <n; i++){
        int a, b; cin>>a>>b;
        vec[a].push_back(b);
        vec[b].push_back(a);
        st.insert((a*1e6+b));
        st.insert((b*1e6+a));
    }
    if(n<=4){
        cout<<n-2<<endl;
        return;
    }

    vector<pair<int, int>> sz;
    for(int i = 1; i <=n; i++){
        sz.push_back({vec[i].size(), i});
    }
    sort(sz.rbegin(), sz.rend());

    for(int i = 1; i <n; i++){
        if(sz[i].first < sz[1].first){
            break;
        }
        if(st.count((sz[0].second*1e6+ sz[i].second)) == 0){
            cout<<sz[0].first+ sz[i].first-1<<endl;
            return;
        }
    }

    cout<<sz[0].first+ sz[1].first-2<<endl;

}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    while (t--) solve();

    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;

#define all(v) v.begin(), v.end()
#define debug(x) cout << #x << '=' << x << endl;
#define pb push_back
#define int long long
const long long M = 998244353;

void solve(){
    int n; cin>>n;

    vector<vector<int>> vec(n+1);
    unordered_set<int> st;
    for(int i = 1; i <n; i++){
        int a, b; cin>>a>>b;
        vec[a].push_back(b);
        vec[b].push_back(a);
        st.insert((a*1e6+b));
        st.insert((b*1e6+a));
    }
    if(n<=4){
        cout<<n-2<<endl;
        return;
    }

    vector<pair<int, int>> sz;
    for(int i = 1; i <=n; i++){
        sz.push_back({vec[i].size(), i});
    }
    sort(sz.begin(), sz.end());

    for(int i = n-2; i >=0; i--){
        if(sz[i].first < sz[n-2].first){
            break;
        }
        if(st.count((sz[n-1].second*1e6 + sz[i].second)) == 0){
            cout<<sz[n-1].first+ sz[i].first-1<<endl;
            return;
        }
    }

    cout<<sz[n-1].first+ sz[n-2].first-2<<endl;

}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    while (t--) solve();

    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

#define all(v) v.begin(), v.end()
#define debug(x) cout << #x << '=' << x << endl;
#define pb push_back
#define int long long
const long long M = 998244353;

void solve(){
    int n; cin>>n;

    vector<vector<int>> vec(n+1);
    unordered_set<int> st;
    for(int i = 1; i <n; i++){
        int a, b; cin>>a>>b;
        vec[a].push_back(b);
        vec[b].push_back(a);
        st.insert((a*1e6+b));
        st.insert((b*1e6+a));
    }
    if(n<=4){
        cout<<n-2<<endl;
        return;
    }

    vector<pair<int, int>> sz;
    for(int i = 1; i <=n; i++){
        // cout<<vec[i].size()<<" ";
        sz.push_back({vec[i].size(), i});
    } //cout<<endl;
    sort(sz.begin(), sz.end());

    int ans = sz[n-1].first+ sz[n-2].first-2;

    if(st.count((sz[n-1].second*1e6+sz[n-2].second)) == 0) ans++;

    for(int i = n-3; i >=0; i--){
        if(st.count((sz[n-1].second*1e6+sz[i].second)) == 0) ans = max(ans, sz[n-1].first+sz[i].first-1);
        else ans = max(ans, sz[n-1].first+sz[i].first-2);

        if(st.count((sz[n-2].second*1e6+sz[i].second)) == 0) ans = max(ans, sz[n-2].first+sz[i].first-1);
        else ans = max(ans, sz[n-2].first+sz[i].first-2);
    }


    cout<<ans<<endl;

}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    while (t--) solve();

    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: PyPy 3-64

Previous submission (verdict: Wrong answer on test 2):
import sys
import math
import collections
import heapq
input=sys.stdin.readline
t=int(input())
for w in range(t):
    n=int(input())
    d={}
    deg=[0 for i in range(n)]
    for i in range(n):
        d[i]=[]
    for i in range(n-1):
        u,v=(int(i) for i in input().split())
        d[u-1].append(v-1)
        d[v-1].append(u-1)
        deg[u-1]+=1
        deg[v-1]+=1
    ans=1
    ind,m=0,0
    for i in range(n):
        if(deg[i]>m):
            ind=i
            m=deg[i]
    ans+=m-1
    for i in d:
        for j in range(len(d[i])):
            if(d[i][j]==ind):
                del d[i][j]
                deg[i]-=1
                break
    del d[ind]
    del deg[ind]
    ans+=max(deg)-1
    print(ans)

Submission (verdict: Wrong answer on test 2):
import sys
import math
import collections
import heapq
input=sys.stdin.readline
t=int(input())
for w in range(t):
    n=int(input())
    d={}
    deg=[0 for i in range(n)]
    for i in range(n):
        d[i]=[]
    for i in range(n-1):
        u,v=(int(i) for i in input().split())
        d[u-1].append(v-1)
        d[v-1].append(u-1)
        deg[u-1]+=1
        deg[v-1]+=1
    ans=1
    ind,m=0,0
    for i in range(n):
        if(deg[i]>m):
            ind=i
            m=deg[i]
    ans+=m-1
    for i in d:
        for j in range(len(d[i])):
            if(d[i][j]==ind):
                del d[i][j]
                deg[i]-=1
                break
    del d[ind]
    del deg[ind]
    ans+=max(deg)-1
    if(max(deg)==1):
        ans+=1
    print(ans)
Problem: 2063C - Remove Exactly Two
Language: PyPy 3-64

Previous submission (verdict: Wrong answer on test 2):
import sys
import math
import collections
import heapq
input=sys.stdin.readline
t=int(input())
for w in range(t):
    n=int(input())
    d={}
    deg=[0 for i in range(n)]
    for i in range(n):
        d[i]=[]
    for i in range(n-1):
        u,v=(int(i) for i in input().split())
        d[u-1].append(v-1)
        d[v-1].append(u-1)
        deg[u-1]+=1
        deg[v-1]+=1
    ans=1
    ind,m=0,0
    for i in range(n):
        if(deg[i]>m):
            ind=i
            m=deg[i]
    ans+=m-1
    for i in d:
        for j in range(len(d[i])):
            if(d[i][j]==ind):
                del d[i][j]
                deg[i]-=1
                break
    del d[ind]
    del deg[ind]
    ans+=max(deg)-1
    if(max(deg)==1):
        ans+=1
    print(ans)

Submission (verdict: Accepted):
import sys
import math
import collections
import heapq
input=sys.stdin.readline
t=int(input())
for w in range(t):
    n=int(input())
    d={}
    deg=[0 for i in range(n)]
    for i in range(n):
        d[i]=[]
    for i in range(n-1):
        u,v=(int(i) for i in input().split())
        d[u-1].append(v-1)
        d[v-1].append(u-1)
        deg[u-1]+=1
        deg[v-1]+=1
    deg1=[deg[i] for i in range(n)]
    d1={}
    for i in range(n):
        d1[i]=d[i]
    ans=1
    ind,m=-1,-1
    for i in range(n):
        if(deg[i]>m):
            ind=i
            m=deg[i]
    ans+=m-1
    for i in d:
        for j in range(len(d[i])):
            if(d[i][j]==ind):
                del d[i][j]
                deg[i]-=1
                break
    del d[ind]
    del deg[ind]
    ans+=max(deg)-1
    if(deg1.count(m)>1):
        ans1=1
        ind1,m1=-1,-1
        for i in range(n):
            if(deg1[i]>m1 and i!=ind):
                ind1=i
                m1=deg1[i]
        ans1+=m1-1
        for i in d1:
            for j in range(len(d1[i])):
                if(d1[i][j]==ind1):
                    del d1[i][j]
                    deg1[i]-=1
                    break
        del d1[ind1]
        del deg1[ind1]
        ans1+=max(deg1)-1
        ans=max(ans,ans1)
    print(ans)
Problem: 2063C - Remove Exactly Two
Language: PyPy 3-64


Submission (verdict: Wrong answer on test 2):
import sys
import math
import collections
import heapq
input=sys.stdin.readline
t=int(input())
for w in range(t):
    n=int(input())
    d={}
    deg=[0 for i in range(n)]
    for i in range(n):
        d[i]=[]
    for i in range(n-1):
        u,v=(int(i) for i in input().split())
        d[u-1].append(v-1)
        d[v-1].append(u-1)
        deg[u-1]+=1
        deg[v-1]+=1
    ans=1
    ind,m=0,0
    for i in range(n):
        if(deg[i]>m):
            ind=i
            m=deg[i]
    ans+=m-1
    for i in d:
        for j in range(len(d[i])):
            if(d[i][j]==ind):
                del d[i][j]
                deg[i]-=1
                break
    del d[ind]
    del deg[ind]
    ans+=max(deg)-1
    print(ans)
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 16):
#include <bits/stdc++.h>
#define endl '\n'
#define yes cout << "YES" << '\n'
#define no cout << "NO" << '\n'
#define ll long long int
using namespace std;

void solve()
{
    int n; cin>>n;
    vector<int>a(n);
    for(auto &i: a)
    cin>>i;

    int mx = *max_element(a.begin(),a.end());

    if(mx == a[0]){
        no;
        return;
    }
    if(a[0] > a[1]){
        no;
        return;
    }
    int dif = abs(a[0]-a[1]);
    for(int i=1; i<n-1; i++){
        if(dif > a[i+1]){
            no;
            return;
        }
        dif = abs(dif - a[i+1]);
    }
    yes;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
#define endl '\n'
#define yes cout << "YES" << '\n'
#define no cout << "NO" << '\n'
#define ll long long int
using namespace std;

void solve()
{
    int n; cin>>n;
    vector<int>a(n);
    for(auto &i: a)
    cin>>i;

    int mx = *max_element(a.begin(),a.end());

    // if(mx == a[0]){
    //     no;
    //     return;
    // }
    if(a[0] > a[1]){
        no;
        return;
    }
    int dif = abs(a[0]-a[1]);
    for(int i=1; i<n-1; i++){
        if(dif > a[i+1]){
            no;
            return;
        }
        dif = abs(dif - a[i+1]);
    }
    yes;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define endl '\n'
#define yes cout << "YES" << '\n'
#define no cout << "NO" << '\n'
#define ll long long int
using namespace std;

void solve()
{
    int n; cin>>n;
    vector<int>a(n);
    for(auto &i: a)
    cin>>i;

    int mx = *max_element(a.begin(),a.end());

    if(mx == a[0]){
        no;
        return;
    }
    int dif = abs(a[0]-a[1]);
    for(int i=1; i<n-1; i++){
        if(dif > a[i+1]){
            no;
            return;
        }
        dif = abs(dif - a[i+1]);
    }
    yes;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}


Submission (verdict: Wrong answer on test 16):
#include <bits/stdc++.h>
#define endl '\n'
#define yes cout << "YES" << '\n'
#define no cout << "NO" << '\n'
#define ll long long int
using namespace std;

void solve()
{
    int n; cin>>n;
    vector<int>a(n);
    for(auto &i: a)
    cin>>i;

    int mx = *max_element(a.begin(),a.end());

    if(mx == a[0]){
        no;
        return;
    }
    if(a[0] > a[1]){
        no;
        return;
    }
    int dif = abs(a[0]-a[1]);
    for(int i=1; i<n-1; i++){
        if(dif > a[i+1]){
            no;
            return;
        }
        dif = abs(dif - a[i+1]);
    }
    yes;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define endl '\n'
#define yes cout << "YES" << '\n'
#define no cout << "NO" << '\n'
#define ll long long int
using namespace std;

void solve()
{
    int n; cin>>n;
    vector<int>a(n);
    for(auto &i: a)
    cin>>i;

    int mx = *max_element(a.begin(),a.end());

    if(mx == a[0]){
        no;
        return;
    }
    int dif = abs(a[0]-a[1]);
    for(int i=1; i<n-1; i++){
        if(dif > a[i+1]){
            no;
            return;
        }
        dif = abs(dif - a[i+1]);
    }
    yes;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}
Problem: 2049B - pspspsps
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 1):
/*****************************
File: B_pspspsps.cpp
Time & Date:
17:25 (GMT+06:00)
Wednesday, 01-Jan-2025 

Author:
SAYFULLAH MONSUR SUJAN SARKAR
3rd year B.Sc. Engineering Student
Department of Information and Communication Engineering
University of Rajshahi, Bangladesh
******************************/

#include <bits/stdc++.h>
using namespace std;

/************ Selfmade Debugger ************/
#ifdef SAIFULLAH
    #include <ICE/debugger>
#else
    #define debug(...)   991
    #define line()     992
    #define cendl()    993
    #define cerr       if(false)cerr
#endif

time_t ms() {
    return chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count();
}

/******* Policy Based Data Structure *******/
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

/***************** Typedefs ****************/
typedef  long long                   ll;
typedef  pair<int, int>              pint;
typedef  pair<long long, long long>  pll;
typedef  vector<int>                 vi;
typedef  vector<long long>           vll;

/******* Defines for long statements *******/
#define  pb        push_back
#define  ff        first
#define  ss        second
#define  yes       cout << "YES\n"
#define  no        cout << "NO\n"
#define  nai       cout << "-1\n"
#define  nl        "\n"
#define  all(x)    x.begin(), x.end()
#define  allr(x)   x.rbegin(), x.rend()

/**** Disable Synchronization between C++ and C Standard Streams ****/
#define  fastIO    cout << "FastIO\n"; ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

/************* Constant Values *************/
const int MOD = 1e9+7;
const ll INF = 1e18+10;

void solve(int tt) {
    cerr << "Testcase: " << tt << "\n";
    int n;
    cin >> n;
    string str;
    cin >> str;
    
    bool p_paisi = false;
    for(int i = 0; i < n; i++) {
        if(str[i] == 'p') {
            p_paisi = true;
        } else if(str[i] == 's' && p_paisi) {
            cerr << "1\n";
            no;
            return;
        }
    }

    for(int i = 1; i < n; i++) {
        if(str[i] == 's') {
            for(int j = i+1; j < n-1; j++) {
                if(str[j] == 'p') {
                    cerr << "2\n";
                    no;
                    return;
                }
            }
        }
    }
    yes;
    return;
}

int32_t main(int32_t argc, char* argv[]) {
    #ifdef SAIFULLAH
        time_t start = ms();
    #else
        fastIO;
    #endif

    int t = 1;
    cin >> t;
    for( int i = 1 ; i <= t ; i++ )
        solve(i);

    #ifdef SAIFULLAH
        time_t end = ms();
        cerr << "\nTime: " << end - start << "ms";
    #endif

    return 0;
}
Problem: 2060D - Subtract Min Sort
Language: GNU C11

Previous submission (verdict: Wrong answer on test 2):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  int t, n, i;
  scanf("%d", &t); 
  while(t--) {
    char b = 1;
    scanf("%d", &n);
    int a[n];
    for (i = 0 ; i < n ; i++) {
      scanf("%d", &a[i]);
    }
    if (a[0] > a[1]) {
      b = 0;
    } else {
      for (i = 2 ; i < n ; i++) {
        if (a[i] < a[i - 1]) {
          if ((a[i - 1] - a[i - 2]) > a[i]) {
            b = 0;
            break;
          } else {
            a[i - 1] -= a[i - 2];
            a[i - 2] = 0;
          }
          }
        }
      }
    if (b == 1)
      printf("YES\n");
    else
      printf("NO\n");
  }
  return 0;
}

Submission (verdict: Wrong answer on test 2):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  int t, n, i;
  scanf("%d", &t); 
  while(t--) {
    char b = 1;
    scanf("%d", &n);
    int a[n];
    for (i = 0 ; i < n ; i++) {
      scanf("%d", &a[i]);
    }
    if (a[0] > a[1]) {
      b = 0;
    } else {
      for (i = 2 ; i < n ; i++) {
        if (a[i] < a[i - 1]) {
          a[i - 1] -= a[i - 2];
          a[i - 2] = 0;
          if (a[i - 1] > a[i]) {
            b = 0;
            break;
          } 
        }
      }
    }
    if (b == 1)
      printf("YES\n");
    else
      printf("NO\n");
  }
  return 0;
}
Problem: 2060D - Subtract Min Sort
Language: GNU C11


Submission (verdict: Wrong answer on test 2):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  int t, n, i;
  scanf("%d", &t); 
  while(t--) {
    char b = 1;
    scanf("%d", &n);
    int a[n];
    for (i = 0 ; i < n ; i++) {
      scanf("%d", &a[i]);
    }
    if (a[0] > a[1]) {
      b = 0;
    } else {
      for (i = 2 ; i < n ; i++) {
        if (a[i] < a[i - 1]) {
          if ((a[i - 1] - a[i - 2]) > a[i]) {
            b = 0;
            break;
          } else {
            a[i - 1] -= a[i - 2];
            a[i - 2] = 0;
          }
          }
        }
      }
    if (b == 1)
      printf("YES\n");
    else
      printf("NO\n");
  }
  return 0;
}
Problem: 2060D - Subtract Min Sort
Language: GNU C11

Previous submission (verdict: Wrong answer on test 2):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  int t, n, i;
  scanf("%d", &t); 
  while(t--) {
    char b = 1;
    scanf("%d", &n);
    int a[n];
    for (i = 0 ; i < n ; i++) {
      scanf("%d", &a[i]);
    }
    if (a[0] > a[1]) {
      b = 0;
    } else {
      for (i = 2 ; i < n ; i++) {
        if (a[i] < a[i - 1]) {
          a[i - 1] -= a[i - 2];
          a[i - 2] = 0;
          if (a[i - 1] > a[i]) {
            b = 0;
            break;
          } 
        }
      }
    }
    if (b == 1)
      printf("YES\n");
    else
      printf("NO\n");
  }
  return 0;
}

Submission (verdict: Accepted):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  int t, n, i;
  scanf("%d", &t); 
  while(t--) {
    char b = 1;
    scanf("%d", &n);
    int a[n];
    for (i = 0 ; i < n ; i++) {
      scanf("%d", &a[i]);
    }
    if (a[0] > a[1]) {
      b = 0;
    } else {
      for (i = 2 ; i < n ; i++) {  
        a[i - 1] -= a[i - 2];
        a[i - 2] = 0;
        if (a[i] < a[i - 1]) {
          b = 0;
          break;
        }
      }
    }
    if (b == 1)
      printf("YES\n");
    else
      printf("NO\n");
  }
  return 0;
}
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;

int maximumComponents() {
  int n; cin >> n;
  vector<vector<int>> adjList(n + 1, vector<int>());
  vector<int> inDegree(n + 1, 0);
  for (int i = 1; i < n; ++i) {
    int u, v; cin >> u >> v;
    adjList[u].push_back(v);
    adjList[v].push_back(u);
    inDegree[u]++; inDegree[v]++;
  }

  vector<int> visited(n + 1, 0);

  function<void(int)> componentExplore = [&](int node) -> void {
    visited[node] = 1;

    for (int neighbor : adjList[node]) {
      if (!visited[neighbor]) {
        componentExplore(neighbor);
      }
    }
  };

  function<int()> getMaxIndegreeNode = [&]() -> int {
    int maxIndegreeNode = 1;
    for (int i = 1; i <= n; ++i) {
      if (inDegree[i] >= inDegree[maxIndegreeNode]) {
        maxIndegreeNode = i;
      }
    }

    return maxIndegreeNode;
  };

  for (int i = 1; i < 3; ++i) {
    int maxIndegreeNode = getMaxIndegreeNode();
    visited[maxIndegreeNode] = 1;
    inDegree[maxIndegreeNode] = -1;
    for (int neighbor : adjList[maxIndegreeNode]) {
      inDegree[neighbor]--;
    }
  }

  int components = 0;
  for (int i = 1; i <= n; ++i) {
    if (!visited[i]) {
      componentExplore(i);
      components++;
    }
  }

  return components;
}

int32_t main() {
  ios_base::sync_with_stdio(false); cin.tie(0);

  int test; cin >> test;
  while (test--) {
    cout << maximumComponents() << '\n';
  }
    
  return 0;
}

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

int maximumComponents() {
  int n; cin >> n;
  vector<vector<int>> adjList(n + 1, vector<int>());
  vector<int> inDegree(n + 1, 0);
  for (int i = 1; i < n; ++i) {
    int u, v; cin >> u >> v;
    adjList[u].push_back(v);
    adjList[v].push_back(u);
    inDegree[u]++; inDegree[v]++;
  }

  vector<int> visited(n + 1, 0);

  function<void(int)> componentExplore = [&](int node) -> void {
    visited[node] = 1;

    for (int neighbor : adjList[node]) {
      if (!visited[neighbor]) {
        componentExplore(neighbor);
      }
    }
  };

  function<int()> getMaxIndegreeNode = [&]() -> int {
    int maxIndegreeNode = 0;
    for (int i = 1; i <= n; ++i) {
      if (inDegree[i] >= inDegree[maxIndegreeNode]) {
        maxIndegreeNode = i;
      }
    }

    return maxIndegreeNode;
  };

  for (int i = 1; i < 3; ++i) {
    int maxIndegreeNode = getMaxIndegreeNode();
    visited[maxIndegreeNode] = 1;
    inDegree[maxIndegreeNode] = -1;
    for (int neighbor : adjList[maxIndegreeNode]) {
      inDegree[neighbor]--;
    }
  }

  int components = 0;
  for (int i = 1; i <= n; ++i) {
    if (!visited[i]) {
      componentExplore(i);
      components++;
    }
  }

  return components;
}

int32_t main() {
  ios_base::sync_with_stdio(false); cin.tie(0);

  int test; cin >> test;
  while (test--) {
    cout << maximumComponents() << '\n';
  }
    
  return 0;
}