ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)

Previous submission (verdict: Compilation error):
#include <iostream>
#include <vector>
using namespace std;

void solve(int n, int k, vector<int> vec)
{
    vector<long long> frequencies;
    sort(vec.begin(), vec.end());

    long long current = vec[0];
    long long count = 0;

    for (int i = 0; i < n; i++)
    {
        if (vec[i] == current)
        {
            count++;
        }
        else 
        {
            frequencies.push_back(count);
            current = vec[i];
            count = 1;
        }
    }
    frequencies.push_back(count);
    int fa = 0;
    for (int i = 0; i < frequencies.size(); i++)
    {
        fa++;
    }

    sort(frequencies.begin(), frequencies.end(), [](const long long &a, const long long &b) -> bool{
            return a > b;
        });

    if (k >= n)
    {
        cout << "1\n";        
    }
        
    long long target = n - k;
    long long cumulative = 0;
    long long r =0;
    for(auto f: frequencies){
        cumulative += f;
        r++;
        if(cumulative >= target){
            break;
        }
    }
    r = max(r, (long long)1);
    cout << r << "\n";

}

int main()
{
    int cases;
    cin >> cases;

    while (cases--)
    {
        int n, k;
        cin >> n >> k;
        vector<int> vec(n);
        for (int i = 0; i < n; i++)
        {
            cin >> vec[i];
        }
        solve(n, k, vec);
    }
    return 0;
}

Submission (verdict: Wrong answer on test 1):
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void solve(int n, int k, vector<int> vec)
{
    vector<long long> frequencies;
    sort(vec.begin(), vec.end());

    long long current = vec[0];
    long long count = 0;

    for (int i = 0; i < n; i++)
    {
        if (vec[i] == current)
        {
            count++;
        }
        else 
        {
            frequencies.push_back(count);
            current = vec[i];
            count = 1;
        }
    }
    frequencies.push_back(count);
    int fa = 0;
    for (int i = 0; i < frequencies.size(); i++)
    {
        fa++;
    }

    sort(frequencies.begin(), frequencies.end(), [](const long long &a, const long long &b) -> bool{
            return a > b;
        });

    if (k >= n)
    {
        cout << "1\n";        
    }
        
    long long target = n - k;
    long long cumulative = 0;
    long long r =0;
    for(auto f: frequencies){
        cumulative += f;
        r++;
        if(cumulative >= target){
            break;
        }
    }
    r = max(r, (long long)1);
    cout << r << "\n";

}

int main()
{
    int cases;
    cin >> cases;

    while (cases--)
    {
        int n, k;
        cin >> n >> k;
        vector<int> vec(n);
        for (int i = 0; i < n; i++)
        {
            cin >> vec[i];
        }
        solve(n, k, vec);
    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)


Submission (verdict: Compilation error):
#include <iostream>
#include <vector>
using namespace std;

void solve(int n, int k, vector<int> vec)
{
    vector<long long> frequencies;
    sort(vec.begin(), vec.end());

    long long current = vec[0];
    long long count = 0;

    for (int i = 0; i < n; i++)
    {
        if (vec[i] == current)
        {
            count++;
        }
        else 
        {
            frequencies.push_back(count);
            current = vec[i];
            count = 1;
        }
    }
    frequencies.push_back(count);
    int fa = 0;
    for (int i = 0; i < frequencies.size(); i++)
    {
        fa++;
    }

    sort(frequencies.begin(), frequencies.end(), [](const long long &a, const long long &b) -> bool{
            return a > b;
        });

    if (k >= n)
    {
        cout << "1\n";        
    }
        
    long long target = n - k;
    long long cumulative = 0;
    long long r =0;
    for(auto f: frequencies){
        cumulative += f;
        r++;
        if(cumulative >= target){
            break;
        }
    }
    r = max(r, (long long)1);
    cout << r << "\n";

}

int main()
{
    int cases;
    cin >> cases;

    while (cases--)
    {
        int n, k;
        cin >> n >> k;
        vector<int> vec(n);
        for (int i = 0; i < n; i++)
        {
            cin >> vec[i];
        }
        solve(n, k, vec);
    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Runtime error on test 2):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll T;
    for (cin >> T; T--; ) {
        ll n, k; cin >> n >> k; unordered_map<ll,ll> m; vector<ll> a;
        for (ll i = 0, x; i < n; ++i) cin >> x, ++m[x];
        for (auto [x, y] : m) a.emplace_back(y);
        sort(a.begin(), a.end());
        for (ll i = 0; i < n or k; ++i) {
            ll val = min(k, a[i]);
            a[i] -= val;
            k -= val;
            a.back() += val;
        }
        ll ans = 0;
        for (auto x : a) ans += !!x;
        cout << ans << "\n";
    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Runtime error on test 2):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll T;
    for (cin >> T; T--; ) {
        ll n, k; cin >> n >> k; unordered_map<ll,ll> m; vector<ll> a;
        for (ll i = 0, x; i < n; ++i) cin >> x, ++m[x];
        for (auto [x, y] : m) a.emplace_back(y);
        sort(a.begin(), a.end());
        for (ll i = 0; i < n or k; ++i) {
            ll val = min(k, a[i]);
            a[i] -= val;
            k -= val;
            a.back() += val;
        }
        ll ans = 0;
        for (auto x : a) ans += !!x;
        cout << ans << "\n";
    }
    return 0;
}

Submission (verdict: Time limit exceeded on test 11):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll T;
    for (cin >> T; T--; ) {
        ll n, k; cin >> n >> k; unordered_map<ll,ll> m; vector<ll> a;
        for (ll i = 0, x; i < n; ++i) cin >> x, ++m[x];
        for (auto [x, y] : m) a.emplace_back(y);
        sort(a.begin(), a.end());
        for (ll i = 0; i < a.size() or k; ++i) {
            ll val = min(k, a[i]);
            a[i] -= val;
            k -= val;
            a.back() += val;
        }
        ll ans = 0;
        for (auto x : a) ans += !!x;
        cout << ans << "\n";
    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 11):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll T;
    for (cin >> T; T--; ) {
        ll n, k; cin >> n >> k; unordered_map<ll,ll> m; vector<ll> a;
        for (ll i = 0, x; i < n; ++i) cin >> x, ++m[x];
        for (auto [x, y] : m) a.emplace_back(y);
        sort(a.begin(), a.end());
        for (ll i = 0; i < a.size() or k; ++i) {
            ll val = min(k, a[i]);
            a[i] -= val;
            k -= val;
            a.back() += val;
        }
        ll ans = 0;
        for (auto x : a) ans += !!x;
        cout << ans << "\n";
    }
    return 0;
}

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

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll T;
    for (cin >> T; T--; ) {
        ll n, k; cin >> n >> k; vector<ll> a(n), b;
        for (auto& x : a) cin >> x; sort(a.begin(), a.end());
        ll cnt = 1;
        for (ll i = 1; i < n; ++i) {
            if (a[i] == a[i - 1]) ++cnt;
            else b.emplace_back(cnt), cnt = 1;
        }
        b.emplace_back(cnt);
        sort(b.begin(), b.end());
        ll ans = 0;
        for (ll i = 0; i < b.size(); ++i) {
            ll val = min(k, b[i]);
            b[i] -= val;
            k -= val;
            b.back() += val;
            ans += !!b[i];
        }
        cout << ans << "\n";
    }
    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)


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



void test() {
  int n; 
  cin >> n;
  vector<int> arr(n+1,0);
  vector<vector<int>> adj(n+1);
 
  for(int i =1; i<n; i++) {
    int u, v;
    cin >> u >> v;
    arr[u]++;
    arr[v]++;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  if(n == 2) {
    cout <<"0\n";
    return ;

  }
  

  int maxi = -1, node =-1;

  for(int i =0; i<n+1; i++) {
    if(arr[i] > maxi) {
      node =i;
      maxi = arr[i];
    }
  }
  arr[node] =0;
  int ans = maxi;
  for(auto i : adj[node] ) {
    arr[i]--;
  }
  adj[node] = {};
  maxi = node =-1;
  for(int i =0; i<n+1; i++) {
    if(arr[i] > maxi) {
      node =i;
      maxi = arr[i];
    }
  }
  if(maxi >0) {
    ans += maxi-1;
  } else {
    
    ans--;
  }
  cout << ans<<"\n";
  
  
}

int  main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long long t;
    cin >> t;
    while(t--) {
       test(); 
    }
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

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



void test() {
  int n; 
  cin >> n;
  vector<int> arr(n+1,0);
  vector<vector<int>> adj(n+1);
 
  for(int i =1; i<n; i++) {
    int u, v;
    cin >> u >> v;
    arr[u]++;
    arr[v]++;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  if(n == 2) {
    cout <<"0\n";
    return ;

  }
  

  int maxi = -1, node =-1;

  for(int i =0; i<n+1; i++) {
    if(arr[i] > maxi) {
      node =i;
      maxi = arr[i];
    }
  }
  arr[node] =0;
  int ans = maxi;
  for(auto i : adj[node] ) {
    arr[i]--;
  }
  adj[node] = {};
  maxi = node =-1;
  for(int i =0; i<n+1; i++) {
    if(arr[i] > maxi) {
      node =i;
      maxi = arr[i];
    }
  }
  if(maxi >0) {
    ans += max(1,maxi-1);
  } else {

    ans--;
  }
  cout << ans<<"\n";
  
  
}

int  main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long long t;
    cin >> t;
    while(t--) {
       test(); 
    }
}

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



void test() {
  ll n; 
  cin >> n;
  set<pair<ll,ll>> edges;
  vector<pair<ll,ll>> vt;
  vector<ll> deg(n,0);
  for(ll i=0; i<n-1; i++) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    edges.insert({u,v});
    edges.insert({v,u});
    deg[u]++;
    deg[v]++;
    
  }
  for(ll i=0; i<n; i++) {
    vt.push_back({deg[i],i});
  }
  ll ans =0;
  sort(vt.begin(), vt.end(), greater<pair<ll,ll>>());
  for(ll i =0; i<n; i++) {
    auto a = vt[i];
    for(ll j = i+1; j<n; j++) {
      auto b = vt[j];
      if(edges.find({a.second, b.second}) == edges.end()) {
        ans = max(ans, a.first+b.first -1);
        break;
      }
    }
  }

  for(auto i: edges) {
    ans = max(ans, deg[i.first] + deg[i.second] -2);
  }
  

  cout << ans<<"\n";
  
  
}

int  main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long long t;
    cin >> t;
    while(t--) {
       test(); 
    }
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

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



void test() {
  int n; 
  cin >> n;
  vector<int> arr(n+1,0);
  vector<vector<int>> adj(n+1);
 
  for(int i =1; i<n; i++) {
    int u, v;
    cin >> u >> v;
    arr[u]++;
    arr[v]++;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  if(n == 2) {
    cout <<"0\n";
    return ;

  }
  

  int maxi = -1, node =-1;

  for(int i =0; i<n+1; i++) {
    if(arr[i] > maxi) {
      node =i;
      maxi = arr[i];
    }
  }
  arr[node] =0;
  int ans = maxi;
  for(auto i : adj[node] ) {
    arr[i]--;
  }
  adj[node] = {};
  maxi = node =-1;
  for(int i =0; i<n+1; i++) {
    if(arr[i] > maxi) {
      node =i;
      maxi = arr[i];
    }
  }
  if(maxi >0) {
    ans += maxi-1;
  } else {
    
    ans--;
  }
  cout << ans<<"\n";
  
  
}

int  main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long long t;
    cin >> t;
    while(t--) {
       test(); 
    }
}

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



void test() {
  int n; 
  cin >> n;
  vector<int> arr(n+1,0);
  vector<vector<int>> adj(n+1);
 
  for(int i =1; i<n; i++) {
    int u, v;
    cin >> u >> v;
    arr[u]++;
    arr[v]++;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  if(n == 2) {
    cout <<"0\n";
    return ;

  }
  

  int maxi = -1, node =-1;

  for(int i =0; i<n+1; i++) {
    if(arr[i] > maxi) {
      node =i;
      maxi = arr[i];
    }
  }
  arr[node] =0;
  int ans = maxi;
  for(auto i : adj[node] ) {
    arr[i]--;
  }
  adj[node] = {};
  maxi = node =-1;
  for(int i =0; i<n+1; i++) {
    if(arr[i] > maxi) {
      node =i;
      maxi = arr[i];
    }
  }
  if(maxi >0) {
    ans += max(1,maxi-1);
  } else {

    ans--;
  }
  cout << ans<<"\n";
  
  
}

int  main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long long t;
    cin >> t;
    while(t--) {
       test(); 
    }
}
Problem: 2062C - Cirno and Operations
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);




int main() {
    
    ll t; cin>>t;
    
    while(t--){
        ll n; cin>>n;
        
        vector<ll>v(n);
        for(ll i=0;i<n;i++) cin>>v[i];
        
        if(n==1) { cout<<v[0]<<endl; continue;}
        
        for(ll i=1;i<n-1;i++){
            for(ll j=0;j<=n-i;j++){
                v[j]=v[j+1]-v[j];
            }
        }
        
        cout<<max(abs(v[0]-v[1]), v[0]+v[1])<<endl;
        
        
    }
    
    
}
Problem: 2062C - Cirno and Operations
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);




int main() {
    
    ll t; cin>>t;
    
    while(t--){
        ll n; cin>>n;
        
        vector<ll>v(n);
        for(ll i=0;i<n;i++) cin>>v[i];
        
        vector<ll> rev_v = v;

        reverse(rev_v.begin(), rev_v.end());

        
        if(n==1) { cout<<v[0]<<endl; continue;}
        
        for(ll i=1;i<n-1;i++){
            for(ll j=0;j<=n-i;j++){
                rev_v[j]=rev_v[j+1]-rev_v[j];
            }
        }
        ll ans2=max(abs(rev_v[0]-rev_v[1]), rev_v[0]+rev_v[1]);
        
        for(ll i=1;i<n-1;i++){
            for(ll j=0;j<=n-i;j++){
                v[j]=v[j+1]-v[j];
            }
        }
        
        ll ans1=max(abs(v[0]-v[1]), v[0]+v[1]);
        
        cout<<max(ans1,ans2)<<endl;
    }
    
    
}
   


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);




int main() {
    
    int tc; cin >> tc;
       while (tc--) {
           ll n; cin >> n;
           vector<ll> v(n);
           
           for (ll i=0;i<n;i++) cin >> v[i];

           ll ans = accumulate(v.begin(), v.end(), 0LL);
           
           while (v.size() > 1) {
               vector<ll> temp;
               for (ll i = 1; i < v.size(); i++)
                   temp.push_back(v[i] - v[i-1]);

               ll tempSum = accumulate(temp.begin(), temp.end(), 0LL);
               ans = max(ans, abs(tempSum));
               
               v = move(temp);
           }

           cout << ans << endl;
       }
    
}
Problem: 2062C - Cirno and Operations
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);




int main() {
    
    ll t; cin>>t;
    
    while(t--){
        ll n; cin>>n;
        
        vector<ll>v(n);
        for(ll i=0;i<n;i++) cin>>v[i];
        
        if(n==1) { cout<<v[0]<<endl; continue;}
        
        for(ll i=1;i<n-1;i++){
            for(ll j=0;j<=n-i;j++){
                v[j]=v[j+1]-v[j];
            }
        }
        
        cout<<max(abs(v[0]-v[1]), v[0]+v[1])<<endl;
        
        
    }
    
    
}
   


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);




int main() {
    
    ll t; cin>>t;
    
    while(t--){
        ll n; cin>>n;
        
        vector<ll>v(n);
        for(ll i=0;i<n;i++) cin>>v[i];
        
        vector<ll> rev_v = v;

        reverse(rev_v.begin(), rev_v.end());

        
        if(n==1) { cout<<v[0]<<endl; continue;}
        
        for(ll i=1;i<n-1;i++){
            for(ll j=0;j<=n-i;j++){
                rev_v[j]=rev_v[j+1]-rev_v[j];
            }
        }
        ll ans2=max(abs(rev_v[0]-rev_v[1]), rev_v[0]+rev_v[1]);
        
        for(ll i=1;i<n-1;i++){
            for(ll j=0;j<=n-i;j++){
                v[j]=v[j+1]-v[j];
            }
        }
        
        ll ans1=max(abs(v[0]-v[1]), v[0]+v[1]);
        
        cout<<max(ans1,ans2)<<endl;
    }
    
    
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3,unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define f0r(i, n) for (int i = 0; i < (n); ++i)
#define fnr(i, n, k) for (int i = (n); i < (k); ++i)
#define ctn(x) cout << x << '\n'
#define ctl(lst)                          \
  for (auto &a : (lst)) cout << a << ' '; \
  cout << endl;
#define pb push_back
#define F first
#define S second
#define all(v) (v).begin(), (v).end()
#define ub(v, x) (upper_bound(all(v), x) - begin(v))
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define sz(v) (v).size()

using namespace std;
template <class T>
using V = vector<T>;
using ll = long long;
using vi = V<int>;
using pi = pair<int, int>;
using Adj = V<vi>;


signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

	int T;
	cin >> T;
	while (T--) {
		int N, u, v;
		cin >> N;
		vi deg(N);
		Adj adj(N);
		f0r(i, N-1) {
			cin >> u >> v;
			adj[--u].pb(--v);
			adj[v].pb(u);
			deg[u]++, deg[v]++;
		}
		int comps = 1;
		auto mx1 = max_element(all(deg));
		comps += *mx1 - 1;
		for (auto &c: adj[mx1-begin(deg)]) deg[c]--;
		*mx1 = 0;
		mx1 = max_element(all(deg));
		ctn((comps += *mx1 - 1));
	}
}


Submission (verdict: Accepted):
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3,unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define f0r(i, n) for (int i = 0; i < (n); ++i)
#define fnr(i, n, k) for (int i = (n); i < (k); ++i)
#define forl(a, l) for (auto a : l)
#define ctn(x) cout << x << '\n'
#define ctl(lst)                          \
  for (auto &a : (lst)) cout << a << ' '; \
  cout << endl;
#define pb push_back
#define F first
#define S second
#define all(v) (v).begin(), (v).end()
#define ub(v, x) (upper_bound(all(v), x) - begin(v))
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define sz(v) (v).size()

using namespace std;
template <class T>
using V = vector<T>;
using ll = long long;
using vi = V<int>;
using pi = pair<int, int>;
using Adj = V<vi>;

void dfs(Adj &adj, int v, vi &par) {
	forl (c, adj[v]) {
		if (c == par[v]) continue;
		par[c] = v;
		dfs(adj, c, par);
	}
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

	int T;
	cin >> T;
	while (T--) {
		int N, u, v;
		cin >> N;
		vi deg(N);
		Adj adj(N);
		f0r(i, N-1) {
			cin >> u >> v;
			adj[--u].pb(--v);
			adj[v].pb(u);
			deg[u]++, deg[v]++;
		}
		vi maxes, par(N);
		int mx = 0;
		f0r(i, N) {
			if (deg[i] > mx) {
				mx = deg[i];
				maxes = {i};
			} else if (deg[i] == mx) maxes.pb(i);
		}
		dfs(adj, 0, par);
		int mx1 = 0, mx2 = 0;
		int v1 = maxes[0], v2 = maxes.size() == 1 ? maxes[0] : maxes[1];
		f0r(i, N) {
			if (i != v1) mx1 = max(mx1, deg[v1] + deg[i] - 1 - (par[v1] == i || par[i] == v1));
			if (i != v2) mx2 = max(mx2, deg[v2] + deg[i] - 1 - (par[v2] == i || par[i] == v2));
		}
		ctn(max(mx1, mx2));
	}
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3,unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define f0r(i, n) for (int i = 0; i < (n); ++i)
#define fnr(i, n, k) for (int i = (n); i < (k); ++i)
#define ctn(x) cout << x << '\n'
#define ctl(lst)                          \
  for (auto &a : (lst)) cout << a << ' '; \
  cout << endl;
#define pb push_back
#define F first
#define S second
#define all(v) (v).begin(), (v).end()
#define ub(v, x) (upper_bound(all(v), x) - begin(v))
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define sz(v) (v).size()

using namespace std;
template <class T>
using V = vector<T>;
using ll = long long;
using vi = V<int>;
using pi = pair<int, int>;
using Adj = V<vi>;


signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

	int T;
	cin >> T;
	while (T--) {
		int N, u, v;
		cin >> N;
		vi deg(N);
		Adj adj(N);
		f0r(i, N-1) {
			cin >> u >> v;
			adj[--u].pb(--v);
			adj[v].pb(u);
			deg[u]++, deg[v]++;
		}
		int comps = 1;
		auto mx1 = max_element(all(deg));
		comps += *mx1 - 1;
		for (auto &c: adj[mx1-begin(deg)]) deg[c]--;
		*mx1 = 0;
		mx1 = max_element(all(deg));
		comps += *mx1 - 1;
		ctn(comps);
	}
}


Submission (verdict: Compilation error):
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3,unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define f0r(i, n) for (int i = 0; i < (n); ++i)
#define fnr(i, n, k) for (int i = (n); i < (k); ++i)
#define ctn(x) cout << x << '\n'
#define ctl(lst)                          \
  for (auto &a : (lst)) cout << a << ' '; \
  cout << endl;
#define pb push_back
#define F first
#define S second
#define all(v) (v).begin(), (v).end()
#define ub(v, x) (upper_bound(all(v), x) - begin(v))
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define sz(v) (v).size()

using namespace std;
template <class T>
using V = vector<T>;
using ll = long long;
using vi = V<int>;
using pi = pair<int, int>;
using Adj = V<vi>;


signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

	int T;
	cin >> T;
	while (T--) {
		int N, u, v;
		cin >> N;
		vi deg(N);
		Adj adj(N);
		f0r(i, N-1) {
			cin >> u >> v;
			adj[--u].pb(--v);
			adj[v].pb(u);
			deg[u]++, deg[v]++;
		}
		int comps = 1;
		auto mx1 = max_element(all(deg));
		comps += *mx1 - 1;
		for (auto &c: adj[mx1-begin(deg)]) deg[c]--;
		*mx1 = 0;
		mx1 = max_element(all(deg));
		ctn(comps += *mx1 - 1);
	}
}