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


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>

#define ll long long

using namespace std;

void fast_in_out() {
    cout.precision(18);
    cin.tie(nullptr)->sync_with_stdio(false);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
ll sz[200010];
signed main() {
    fast_in_out();
    ll t;
    cin >> t;
    while(t--){
        ll n,i,x,y,m,a,b,j;
        vector<pair<ll,ll>> v;
        set<pair<ll,ll>> s;
        cin >> n;
        m=n-1;
        for(i=1;i<=n;i++)sz[i]=0;
        while(m--){
            cin >> x >> y;
            sz[x]++;
            sz[y]++;
            s.insert({x,y});
            s.insert({y,x});
        }
        ll ch=1;
        for(i=1;i<=n;i++)v.push_back({sz[i],i});
        sort(v.begin(),v.end(),greater());
        for(i=0;i<v.size() && v[i].first>=v[0].first && ch==1;i++){
            for(j=i+1;j<v.size() && v[j].first>=v[1].first && ch==1;j++){
                if(s.find({v[i].second,v[j].second})!=s.end())ch=0;
            }
        }
        cout << min(n-2,v[0].first+v[1].first-ch) << "\n";
    }
    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>

#define ll long long

using namespace std;

void fast_in_out() {
    cout.precision(18);
    cin.tie(nullptr)->sync_with_stdio(false);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
ll sz[200010];
signed main() {
    fast_in_out();
    ll t;
    cin >> t;
    while(t--){
        ll n,i,x,y,m,a,b,j;
        vector<pair<ll,ll>> v;
        set<pair<ll,ll>> s;
        cin >> n;
        m=n-1;
        for(i=1;i<=n;i++)sz[i]=0;
        while(m--){
            cin >> x >> y;
            sz[x]++;
            sz[y]++;
            s.insert({x,y});
            s.insert({y,x});
        }
        ll ch=1;
        for(i=1;i<=n;i++)v.push_back({sz[i],i});
        sort(v.begin(),v.end(),greater());
        for(i=0;i<v.size() && v[i].first>=v[0].first && ch==1;i++){
            for(j=i+1;j<v.size() && v[j].first>=v[1].first && ch==1;j++){
                if(s.find({v[i].second,v[j].second})!=s.end())ch=0;
            }
        }
        cout << min(n-2,v[0].first+v[1].first-ch) << "\n";
    }
    return 0;
}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

#define ll long long

using namespace std;

void fast_in_out() {
    cout.precision(18);
    cin.tie(nullptr)->sync_with_stdio(false);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
ll sz[200010];
signed main() {
    fast_in_out();
    ll t;
    cin >> t;
    while(t--){
        ll n,i,x,y,m,a,b,j;
        vector<pair<ll,ll>> v;
        set<pair<ll,ll>> s;
        cin >> n;
        m=n-1;
        for(i=1;i<=n;i++)sz[i]=0;
        while(m--){
            cin >> x >> y;
            sz[x]++;
            sz[y]++;
            s.insert({x,y});
            s.insert({y,x});
        }
        ll ch=1;
        for(i=1;i<=n;i++)v.push_back({sz[i],i});
        sort(v.begin(),v.end(),greater());
        for(i=0;i<v.size() && v[i].first>=v[0].first && ch==1;i++){
            for(j=i+1;j<v.size() && v[j].first>=v[1].first && ch==1;j++){
                if(s.find({v[i].second,v[j].second})!=s.end())ch=0;
            }
        }
        cout << min(n-2,v[0].first-1+v[1].first-1-ch+1) << "\n";
    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)

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

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        unordered_map<int, int> freq;

        // Read input and count occurrences
        for (int i = 0; i < n; i++) {
            int temp;
            cin >> temp;
            freq[temp]++;
        }

        // Min-heap to process smallest frequency first
        priority_queue<int, vector<int>, greater<int>> pq;
        for (auto &p : freq) {
            pq.push(p.second);
        }

        int ans = freq.size(); // Number of unique elements

        // Remove elements with the smallest frequency while k allows
        while (!pq.empty() && k >= pq.top()) {
            k -= pq.top();
            pq.pop();
            ans--;
        }

        // Ensure at least one element remains
        cout << max(1, ans) << "\n";
    }
    return 0;
}

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

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

    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        map<int, int> freq;

        // Count occurrences
        for (int i = 0; i < n; i++) {
            int temp;
            cin >> temp;
            freq[temp]++;
        }

        // Extract frequencies and sort them
        vector<int> counts;
        for (auto &[num, count] : freq) {
            counts.push_back(count);
        }
        sort(counts.begin(), counts.end());

        int ans = counts.size();
        int i = 0;

        // Greedily remove smallest frequency elements
        while (i < counts.size() && k >= counts[i]) {
            k -= counts[i];
            ans--;
            i++;
        }

        cout << max(1, ans) << "\n";
    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)


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

int main() {
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        int ans=0;
        unordered_map<int,int> m;
        for(int i=0;i<n;i++){
            int temp;
            cin>>temp;
            m[temp]+=1;
        }
        vector<int> v;
        for(auto i:m){
            v.push_back(i.second);
        }
        sort(v.begin(),v.end());
        ans=v.size();
        for(auto i:v){
            cout<<i<<",";
        }
        cout<<"\n";
        int h=0;
        while(k){
            if(v[h]<=k){
                k-=v[h];
                ans-=1;
            }
            else k=0;
            h+=1;
        }
        if(ans<=0) cout<<"1"<<"\n";
        else cout<<ans<<"\n";
    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)

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

int main() {
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        int ans=0;
        unordered_map<int,int> m;
        for(int i=0;i<n;i++){
            int temp;
            cin>>temp;
            m[temp]+=1;
        }
        vector<int> v;
        for(auto i:m){
            v.push_back(i.second);
        }
        sort(v.begin(),v.end());
        ans=v.size();
        int h=0;
        while(k){
            if(v[h]<=k){
                k-=v[h];
                ans-=1;
            }
            else k=0;
            h+=1;
        }
        if(ans<=0) cout<<"1"<<"\n";
        else cout<<ans<<"\n";
    }
    return 0;
}

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

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        unordered_map<int, int> freq;

        // Read input and count occurrences
        for (int i = 0; i < n; i++) {
            int temp;
            cin >> temp;
            freq[temp]++;
        }

        // Min-heap to process smallest frequency first
        priority_queue<int, vector<int>, greater<int>> pq;
        for (auto &p : freq) {
            pq.push(p.second);
        }

        int ans = freq.size(); // Number of unique elements

        // Remove elements with the smallest frequency while k allows
        while (!pq.empty() && k >= pq.top()) {
            k -= pq.top();
            pq.pop();
            ans--;
        }

        // Ensure at least one element remains
        cout << max(1, ans) << "\n";
    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)

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

int main() {
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        int ans=0;
        unordered_map<int,int> m;
        for(int i=0;i<n;i++){
            int temp;
            cin>>temp;
            m[temp]+=1;
        }
        vector<int> v;
        for(auto i:m){
            v.push_back(i.second);
        }
        sort(v.begin(),v.end());
        ans=v.size();
        for(auto i:v){
            cout<<i<<",";
        }
        cout<<"\n";
        int h=0;
        while(k){
            if(v[h]<=k){
                k-=v[h];
                ans-=1;
            }
            else k=0;
            h+=1;
        }
        if(ans<=0) cout<<"1"<<"\n";
        else cout<<ans<<"\n";
    }
    return 0;
}

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

int main() {
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        int ans=0;
        unordered_map<int,int> m;
        for(int i=0;i<n;i++){
            int temp;
            cin>>temp;
            m[temp]+=1;
        }
        vector<int> v;
        for(auto i:m){
            v.push_back(i.second);
        }
        sort(v.begin(),v.end());
        ans=v.size();
        int h=0;
        while(k){
            if(v[h]<=k){
                k-=v[h];
                ans-=1;
            }
            else k=0;
            h+=1;
        }
        if(ans<=0) cout<<"1"<<"\n";
        else cout<<ans<<"\n";
    }
    return 0;
}
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)


Submission (verdict: Time limit exceeded on test 6):
#include<bits/stdc++.h>
using namespace std;
void solve();

int main()
{
    int t=1;
    cin>>t;
    while(t--)
    {
        solve();
        cout<<"\n";
    }
}

bool swapOrNot(int a, int b)
{
    int num1 = (b-1)*10+a;
    int num2 = (a*10)+b;
    return num1>num2;
}

void solve()
{
    string num;
    cin>>num;
    
    int n=num.length();

    if(n==1)
    {
        cout<<num;
        return;
    }

    string ans="";
    ans+=num[n-1];

    for(int i=n-2;i>=0;i--)
    { 
        ans=num[i]+ans;
        for(int j=0;j<ans.length()-1;j++)
        {
           if(swapOrNot(ans[j]-'0',ans[j+1]-'0'))
           {
              char modifiedChar = ans[j+1] - 1;
              swap(ans[j], modifiedChar);
              ans[j+1] = modifiedChar;
           }
        }
    }

    cout<<ans;
}
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 6):
#include<bits/stdc++.h>
using namespace std;
void solve();

int main()
{
    int t=1;
    cin>>t;
    while(t--)
    {
        solve();
        cout<<"\n";
    }
}

bool swapOrNot(int a, int b)
{
    int num1 = (b-1)*10+a;
    int num2 = (a*10)+b;
    return num1>num2;
}

void solve()
{
    string num;
    cin>>num;
    
    int n=num.length();

    if(n==1)
    {
        cout<<num;
        return;
    }

    string ans="";
    ans+=num[n-1];

    for(int i=n-2;i>=0;i--)
    { 
        ans=num[i]+ans;
        for(int j=0;j<ans.length()-1;j++)
        {
           if(swapOrNot(ans[j]-'0',ans[j+1]-'0'))
           {
              char modifiedChar = ans[j+1] - 1;
              swap(ans[j], modifiedChar);
              ans[j+1] = modifiedChar;
           }
        }
    }

    cout<<ans;
}

Submission (verdict: Time limit exceeded on test 8):
#include <bits/stdc++.h>
using namespace std;
void solve();

int main()
{
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout << "\n";
    }
}

bool swapOrNot(int a, int b)
{
    int num1 = (b - 1) * 10 + a;
    int num2 = (a * 10) + b;
    return num1 > num2;
}

void solve()
{
    string num;
    cin >> num;

    int n = num.length();

    if (n == 1)
    {
        cout << num;
        return;
    }

    string ans = "";
    ans += num[n - 1];

    for (int i = n - 2; i >= 0; i--)
    {
        ans = num[i] + ans;
        if (ans[0] <= ans[1])
        {
            for (int j = 0; j < ans.length() - 1; j++)
            {
                if (ans[j] <= ans[j + 1])
                {
                    if (swapOrNot(ans[j] - '0', ans[j + 1] - '0'))
                    {
                        char modifiedChar = ans[j + 1] - 1;
                        swap(ans[j], modifiedChar);
                        ans[j + 1] = modifiedChar;
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }

    cout << ans;
}
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 8):
#include <bits/stdc++.h>
using namespace std;
void solve();

int main()
{
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout << "\n";
    }
}

bool swapOrNot(int a, int b)
{
    int num1 = (b - 1) * 10 + a;
    int num2 = (a * 10) + b;
    return num1 > num2;
}

void solve()
{
    string num;
    cin >> num;

    int n = num.length();

    if (n == 1)
    {
        cout << num;
        return;
    }

    string ans = "";
    ans += num[n - 1];

    for (int i = n - 2; i >= 0; i--)
    {
        ans = num[i] + ans;
        if (ans[0] <= ans[1])
        {
            for (int j = 0; j < ans.length() - 1; j++)
            {
                if (ans[j] <= ans[j + 1])
                {
                    if (swapOrNot(ans[j] - '0', ans[j + 1] - '0'))
                    {
                        char modifiedChar = ans[j + 1] - 1;
                        swap(ans[j], modifiedChar);
                        ans[j + 1] = modifiedChar;
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }

    cout << ans;
}

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

int main()
{
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout << "\n";
    }
}

bool swapOrNot(int a, int b)
{
    int num1 = (b - 1) * 10 + a;
    int num2 = (a * 10) + b;
    return num1 > num2;
}

void solve()
{
    string num;
    cin >> num;

    int n = num.length();

    if (n == 1)
    {
        cout << num;
        return;
    }

    string ans = "";
    ans += num[n - 1];

    for (int i = n - 2; i >= 0; i--)
    {
        ans = num[i] + ans;
        if (ans[0] < ans[1])
        {
            for (int j = 0; j < ans.length() - 1; j++)
            {
                if (ans[j] < ans[j + 1])
                {
                    if (swapOrNot(ans[j] - '0', ans[j + 1] - '0'))
                    {
                        char modifiedChar = ans[j + 1] - 1;
                        swap(ans[j], modifiedChar);
                        ans[j + 1] = modifiedChar;
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }

    cout << ans;
}
Problem: 2063B - Subsequence Update
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
namespace MySpace{
    #define max(...) MyDearmax(__VA_ARGS__)
    template <typename T>
    T MyDearmax(T a, T b) {return a > b ? a : b;}
    template <typename F, typename... R>
    F MyDearmax(F f,R... r){return MyDearmax(f,MyDearmax(r...));}
    #define min(...) MyDearmin(__VA_ARGS__)
    template <typename T>
    T MyDearmin(T a, T b) {return a < b ? a : b;}
    template <typename F, typename... R>
    F MyDearmin(F f,R... r){return MyDearmin(f,MyDearmin(r...));}
    #define lowbit(x) (x&(-x))
//    #define sqrt __builtin_sqrtf //浣跨敤纭欢鍔犻€熺殑寮€鏂?
//    #ifdef ONLINE_JUDGE
//    #define getchar getchar_unlocked
//    #endif
    inline void read() {}
    template <typename T, typename... R>
    inline void read(T &x,R &... oth){
        x=0;T f=1;
        char c=getchar();
        while(c<'0' || c>'9') {
            if(c=='-') f=-1;
            c=getchar();
        }
        while(c>='0' && c<='9') x=(x<<1)+(x<<3)+(c&15),c=getchar();
        x*=f;
        read(oth...);
        return;
    }
    template<typename T>
    T qpow(T a,T n,T p){
        T res=1;
        while (n){
            if (n&1) res=1ll*res*a%p;
            a=1ll*a*a%p;
            n>>=1;
        }
        return res;
    }
    template<typename T>
    T gcd(T a,T b){return (b>0?gcd(b,a%b):a);}
    template<typename T>
    T exgcd(T a,T b,T& x,T& y){
    	if (b<=0ll){
    		x=1ll,y=0ll;
    		return a;
		}
		T t=x;
		T d=exgcd(b,a%b,y,x);
		y-=(a/b)*x;
		return d;
	}
	template<typename T>
	T inv(T a,T p){
		if (a==1) return 1ll;
		T x,y;
		if (exgcd(a,p,x,y)!=1ll) return -1ll;
		else return (x+p)%p;
	}
}
using namespace MySpace;
//const int INF=0x66CCFF66;
#define int long long
const int maxn=1e5+5;
int n,l,r;
int T;
int a[maxn];
signed main(){
	read(T);
	while (T--){
		read(n,l,r);
		for (int i=1;i<=n;i++) read(a[i]);
		sort(a+1,a+1+n);
		int ans=0;
		for (int i=l;i<=r;i++) ans+=a[i-l+1];
		printf("%lld\n",ans);
	}

	return 0;
}
Problem: 2061D - Kevin and Numbers
Language: C++17 (GCC 7-32)


Submission (verdict: Time limit exceeded on test 3):
#pragma GCC optimize("Ofast")

#pragma GCC optimize("unroll-loops")



#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <sstream>

#include <queue>

#include <deque>

#include <bitset>

#include <iterator>

#include <list>

#include <stack>

#include <utility>

#include <map>

#include <set>

#include <numeric>

#include <cmath>

#include <cstring>



using namespace std;



typedef long long ll;

typedef vector<int> vi;

typedef vector<ll> vll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef multiset<int> msi;



#define loop(i, l, n) for (decltype(n) i = l; i < n; i++)

#define rloop(i, l, n) for (decltype(n) i = l; i >= n; i--)

#define pb push_back

#define YES cout << "YES\n"

#define NO cout << "NO\n"

#define all(v) v.begin(), v.end()



int f(int x, multiset<int>& ms, int maxi) {

    if (maxi < 0) return -1;

    if(ms.find(x) != ms.end())

    {

        return 1;

    }

    int a, b;

    if (x % 2 == 0) {

        a = b = x / 2;

    } else {

        a = x / 2;

        b = x / 2 + 1;

    }



    int flag = 0;



    // Try to use a

    if (ms.find(a) != ms.end()) {

        ms.erase(ms.find(a));

        flag++;

    } else {

        if (f(a, ms, maxi - 1) != -1) flag++;

    }



    // Try to use b

    if (ms.find(b) != ms.end()) {

        ms.erase(ms.find(b));

        flag++;

    } else {

        if (f(b, ms, maxi - 1) != -1) flag++;

    }



    return (flag == 2) ? 1 : -1;

}



void solve() {

    int n, m;

    cin >> n >> m;



    ll sum1 = 0;

    ll sum2= 0;

    multiset<int> ms;

    loop(i, 0, n) {

        int x;

        cin >> x;

        sum1+= x;

        ms.insert(x);

    }



    vi v2(m);

    loop(i, 0, m) 

    {cin >> v2[i];

     sum2+= v2[i];

    }

    if(sum1 != sum2)

    {

        NO;

        return;

    }



    // sort(all(v2));

    int maxi = n-m;;



    for (int i = m - 1; i >= 0; i--) {

        if (f(v2[i], ms, maxi) == -1) {

            NO;

            return;

        }

    }



    YES;

}



int main() {

    ios::sync_with_stdio(false);

    cin.tie(NULL);



    int t;

    cin >> t;

    while (t--) {

        solve();

    }



    return 0;

}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

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

int getMax(vector<int> &edgeCount, vector<vector<int>> &adj) {
    int mx = 0;
    int ind;
    for(int i = 1; i <= N; i++) {
        if(edgeCount[i] >= mx) {
            mx = edgeCount[i];
            ind = i;
        }
    }
    edgeCount[ind] = -1e9;

    for(auto &u : adj[ind]) {
        edgeCount[u]--;
    }

    return mx;
}

void solve() {
    cin >> N;

    vector<vector<int>> adj(N+1);
    vector<int> edgeCount(N+1);
    for(int i = 0; i < N-1; i++) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v); adj[v].push_back(u);
    }
    for(int i = 1; i<=N; i++) {
        edgeCount[i] = adj[i].size();
    }

    int ccCount = getMax(edgeCount, adj);
    ccCount += getMax(edgeCount, adj) - 1;

    cout << ccCount << endl;
}

int main() {
    int T; 
    cin >> T;
    while(T--) {
        solve();
    }
    return 0;
}

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

int getMax(vector<int> &edgeCount, vector<vector<int>> &adj) {
    int mx = 0;
    int ind;
    for(int i = 1; i <= N; i++) {
        if(edgeCount[i] >= mx) {
            mx = edgeCount[i];
            ind = i;
        }
    }
    edgeCount[ind] = -1e9;

    for(auto &u : adj[ind]) {
        if(edgeCount[u] == -1e9)
            mx++;
        else 
            edgeCount[u]--;
    }

    return mx;
}

void solve() {
    cin >> N;

    vector<vector<int>> adj(N+1);
    vector<int> edgeCount(N+1);
    for(int i = 0; i < N-1; i++) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v); adj[v].push_back(u);
    }
    for(int i = 1; i<=N; i++) {
        edgeCount[i] = adj[i].size();
    }

    int ccCount = getMax(edgeCount, adj);
    ccCount += getMax(edgeCount, adj) - 1;

    cout << ccCount << endl;
}

int main() {
    int T; 
    cin >> T;
    while(T--) {
        solve();
    }
    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

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

int getMax(vector<int> &edgeCount, vector<vector<int>> &adj) {
    int mx = 0;
    int ind;
    for(int i = 1; i <= N; i++) {
        if(edgeCount[i] >= mx) {
            mx = edgeCount[i];
            ind = i;
        }
    }
    edgeCount[ind] = -1e9;

    for(auto &u : adj[ind]) {
        if(edgeCount[u] == -1e9)
            mx++;
        else 
            edgeCount[u]--;
    }

    return mx;
}

void solve() {
    cin >> N;

    vector<vector<int>> adj(N+1);
    vector<int> edgeCount(N+1);
    for(int i = 0; i < N-1; i++) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v); adj[v].push_back(u);
    }
    for(int i = 1; i<=N; i++) {
        edgeCount[i] = adj[i].size();
    }

    int ccCount = getMax(edgeCount, adj);
    ccCount += getMax(edgeCount, adj) - 1;

    cout << ccCount << endl;
}

int main() {
    int T; 
    cin >> T;
    while(T--) {
        solve();
    }
    return 0;
}

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

int getMax(vector<int> &edgeCount, vector<vector<int>> &adj) {
    int mx = 0;
    int ind;
    for(int i = 1; i <= N; i++) {
        if(edgeCount[i] >= mx) {
            mx = edgeCount[i];
            ind = i;
        }
    }
    edgeCount[ind] = -1e9;

    if(mx != 0) {
        for(auto &u : adj[ind]) {
            if(edgeCount[u] == -1e9)
                mx++;
            else 
                edgeCount[u]--;
        }
    }

    return mx;
}

void solve() {
    cin >> N;

    vector<vector<int>> adj(N+1);
    vector<int> edgeCount(N+1);
    for(int i = 0; i < N-1; i++) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v); adj[v].push_back(u);
    }
    for(int i = 1; i<=N; i++) {
        edgeCount[i] = adj[i].size();
    }

    int ccCount = getMax(edgeCount, adj);
    ccCount += getMax(edgeCount, adj) - 1;

    cout << ccCount << endl;
}

int main() {
    int T; 
    cin >> T;
    while(T--) {
        solve();
    }
    return 0;
}