ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050C - Uninteresting Number
Language: C++20 (GCC 13-64)

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

using namespace std;

#define int long long

#define vi vector<int>

#define rep(i,x,y) for(int i=x; i<y; i++)

bool in_range(int x,int y,int r,int c){

    return (x>=0 && y>=0 && x<r && y<c);

}

int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL); 

    int t;

    cin>>t;

    while(t--){

        int n;

        cin>>n;

        int value = 0;

        int twos = 0, threes = 0;

        int temp = n;

        while(temp){

            int digit = temp % 10;

            value += digit;

            if(digit == 2) twos++;

            if(digit == 3) threes++;

            temp /= 10;

        }

        if(value % 9 == 0){

            cout << "YES" << endl;

            continue;

        }

        bool flag = false;

        rep(i, 0, min(10ll, twos + 1)){

            rep(j, 0, min(10ll, threes + 1)){

                if((value + 2*i + 6*j) % 9 == 0){

                    flag = true;

                    break;

                }

            }

            if(flag) break;

        }

        if(flag) cout << "YES" << endl;

        else cout << "NO" << endl;

    }

    return 0;

}



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

using namespace std;

#define int long long

#define vi vector<int>

#define rep(i,x,y) for(int i=x; i<y; i++)

bool in_range(int x,int y,int r,int c){

    return (x>=0 && y>=0 && x<r && y<c);

}

int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL); 

    int t;

    cin>>t;

    while(t--){

        string n;

        cin>>n;

        int value = 0;

        int twos = 0, threes = 0;

        for(char c : n){

            int digit = c - '0';

            value += digit;

            if(digit == 2) twos++;

            if(digit == 3) threes++;

        }

        if(value % 9 == 0){

            cout << "YES" << endl;

            continue;

        }

        bool flag = false;

        rep(i, 0, min(10ll, twos + 1)){

            rep(j, 0, min(10ll, threes + 1)){

                if((value + 2*i + 6*j) % 9 == 0){

                    flag = true;

                    break;

                }

            }

            if(flag) break;

        }

        if(flag) cout << "YES" << endl;

        else cout << "NO" << endl;

    }

    return 0;

}
Problem: 2050C - Uninteresting Number
Language: C++20 (GCC 13-64)


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

using namespace std;

#define int long long

#define vi vector<int>

#define rep(i,x,y) for(int i=x; i<y; i++)

bool in_range(int x,int y,int r,int c){

return (x>=0 && y>=0 && x<r && y<c);

}

int32_t main() {

ios_base::sync_with_stdio(false);

cin.tie(NULL); 

int t;

cin>>t;

while(t--){

    int n;

    cin>>n;

    int value=0;

    int temp=n;

    int twos=0;

    int threes=0;

    while(temp){

        value+=(temp%10);

        if((temp%10)== 2)twos++;

        if((temp%10)==3) threes++;

        temp/=10;

    }

    if((value%9)==0){cout<<"YES"<<endl;continue;}

    bool flag=0;

    // at max we can do upto 8 values of 2 and 3 ...more than that mod 9 will we same as 0

    rep(i,0,min(9ll,twos+1)){

        rep(j,0,min(9ll,threes+1)){

            if(((value+(2*i+6*j))%9) == 0){

                flag=1;

                break;

            }

        }

    }

    if(flag)cout<<"YES";

    else cout<<"NO";

    cout<<endl;



}

return 0;

}
Problem: 2050C - Uninteresting Number
Language: C++20 (GCC 13-64)

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

using namespace std;

#define int long long

#define vi vector<int>

#define rep(i,x,y) for(int i=x; i<y; i++)

bool in_range(int x,int y,int r,int c){

return (x>=0 && y>=0 && x<r && y<c);

}

int32_t main() {

ios_base::sync_with_stdio(false);

cin.tie(NULL); 

int t;

cin>>t;

while(t--){

    int n;

    cin>>n;

    int value=0;

    int temp=n;

    int twos=0;

    int threes=0;

    while(temp){

        value+=(temp%10);

        if((temp%10)== 2)twos++;

        if((temp%10)==3) threes++;

        temp/=10;

    }

    if((value%9)==0){cout<<"YES"<<endl;continue;}

    bool flag=0;

    // at max we can do upto 8 values of 2 and 3 ...more than that mod 9 will we same as 0

    rep(i,0,min(9ll,twos+1)){

        rep(j,0,min(9ll,threes+1)){

            if(((value+(2*i+6*j))%9) == 0){

                flag=1;

                break;

            }

        }

        if(flag)break;

    }

    if(flag)cout<<"YES";

    else cout<<"NO";

    cout<<endl;



}

return 0;

}

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

using namespace std;

#define int long long

#define vi vector<int>

#define rep(i,x,y) for(int i=x; i<y; i++)

bool in_range(int x,int y,int r,int c){

return (x>=0 && y>=0 && x<r && y<c);

}

int32_t main() {

ios_base::sync_with_stdio(false);

cin.tie(NULL); 

int t;

cin>>t;

while(t--){

    int n;

    cin>>n;

    int value=0;

    int temp=n;

    int twos=0;

    int threes=0;

    while(temp){

        value+=(temp%10);

        if((temp%10)== 2)twos++;

        if((temp%10)==3) threes++;

        temp/=10;

    }

    if((value%9)==0){cout<<"YES"<<endl;continue;}

    bool flag=0;

    // at max we can do upto 8 values of 2 and 3 ...more than that mod 9 will we same as 0

    rep(i,0,min(10ll,twos+1)){

        rep(j,0,min(10ll,threes+1)){

            if(((value+(2*i+6*j))%9) == 0){

                flag=1;

                break;

            }

        }

        if(flag)break;

    }

    if(flag)cout<<"YES";

    else cout<<"NO";

    cout<<endl;



}

return 0;

}
Problem: 2050C - Uninteresting Number
Language: C++20 (GCC 13-64)

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

using namespace std;

#define int long long

#define vi vector<int>

#define rep(i,x,y) for(int i=x; i<y; i++)

bool in_range(int x,int y,int r,int c){

return (x>=0 && y>=0 && x<r && y<c);

}

int32_t main() {

ios_base::sync_with_stdio(false);

cin.tie(NULL); 

int t;

cin>>t;

while(t--){

    int n;

    cin>>n;

    int value=0;

    int temp=n;

    int twos=0;

    int threes=0;

    while(temp){

        value+=(temp%10);

        if((temp%10)== 2)twos++;

        if((temp%10)==3) threes++;

        temp/=10;

    }

    if((value%9)==0){cout<<"YES"<<endl;continue;}

    bool flag=0;

    // at max we can do upto 8 values of 2 and 3 ...more than that mod 9 will we same as 0

    rep(i,0,min(9ll,twos+1)){

        rep(j,0,min(9ll,threes+1)){

            if(((value+(2*i+6*j))%9) == 0){

                flag=1;

                break;

            }

        }

    }

    if(flag)cout<<"YES";

    else cout<<"NO";

    cout<<endl;



}

return 0;

}

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

using namespace std;

#define int long long

#define vi vector<int>

#define rep(i,x,y) for(int i=x; i<y; i++)

bool in_range(int x,int y,int r,int c){

return (x>=0 && y>=0 && x<r && y<c);

}

int32_t main() {

ios_base::sync_with_stdio(false);

cin.tie(NULL); 

int t;

cin>>t;

while(t--){

    int n;

    cin>>n;

    int value=0;

    int temp=n;

    int twos=0;

    int threes=0;

    while(temp){

        value+=(temp%10);

        if((temp%10)== 2)twos++;

        if((temp%10)==3) threes++;

        temp/=10;

    }

    if((value%9)==0){cout<<"YES"<<endl;continue;}

    bool flag=0;

    // at max we can do upto 8 values of 2 and 3 ...more than that mod 9 will we same as 0

    rep(i,0,min(9ll,twos+1)){

        rep(j,0,min(9ll,threes+1)){

            if(((value+(2*i+6*j))%9) == 0){

                flag=1;

                break;

            }

        }

        if(flag)break;

    }

    if(flag)cout<<"YES";

    else cout<<"NO";

    cout<<endl;



}

return 0;

}
Problem: 2062C - Cirno and Operations
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define rep(i,x,y) for(int i=x; i<y; i++)
bool in_range(int x,int y,int r,int c){
return (x>=0 && y>=0 && x<r && y<c);
}
bool compare(pair<vi,int>a,pair<vi,int>b){
    return (a.first[0]<b.first[0]);
}
int sum(const vi &a){
    int ret=0;
    rep(i,0,a.size())ret+=a[i];
    return ret;
}
vi diff(const vi &a){
    vi b;
    rep(i,0,a.size()-1){
        b.push_back(a[i+1]-a[i]);
    }
    return b;
}
void f(vi &a,int &ans,vi &prev){
    if(a.size()==1){
        ans=max(ans,a[0]);
       return ;
    }
    vi s=a;
    reverse(a.begin(),a.end());
    int rev_value=sum(a);
    int normal_diff_value=sum(diff(s));
    ans=max(rev_value,normal_diff_value);
    if(a!=prev)
    f(a,ans,prev);
    vi new_vector=diff(s);
    prev=new_vector;
    f(new_vector,ans,prev);  
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); 
int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    vi a(n);
    rep(i,0,n)cin>>a[i];
    int ans=INT_MIN;
    ans=max(ans,sum(a));
    vi prev=a;
    f(a,ans,prev);
    cout<<ans<<endl;
   
}
return 0;
}

Submission (verdict: Time limit exceeded on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define rep(i,x,y) for(int i=x; i<y; i++)
bool in_range(int x,int y,int r,int c){
return (x>=0 && y>=0 && x<r && y<c);
}
bool compare(pair<vi,int>a,pair<vi,int>b){
    return (a.first[0]<b.first[0]);
}
unordered_map<string, int> dp;
int sum(const vi &a) {
    string key = to_string(a.size()) + "-";
    for (int num : a) key += to_string(num) + ",";
    
    if (dp.find(key) != dp.end()) return dp[key]; 

    int ret = accumulate(a.begin(), a.end(), 0LL);
    return dp[key] = ret; 
}
vi diff(const vi &a){
    vi b;
    rep(i,0,a.size()-1){
        b.push_back(a[i+1]-a[i]);
    }
    return b;
}
void f(vi &a,int &ans,vi &prev){
    if(a.size()==1){
        ans=max(ans,a[0]);
       return ;
    }
    vi s=a;
    reverse(a.begin(),a.end());
    int rev_value=sum(a);
    int normal_diff_value=sum(diff(s));
    ans=max(rev_value,normal_diff_value);
    if(a!=prev)
    f(a,ans,prev);
    vi new_vector=diff(s);
    prev=new_vector;
    f(new_vector,ans,prev);  
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); 
int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    vi a(n);
    rep(i,0,n)cin>>a[i];
    int ans=INT_MIN;
    ans=max(ans,sum(a));
    vi prev=a;
    f(a,ans,prev);
    cout<<ans<<endl;
   
}
return 0;
}
Problem: 2062C - Cirno and Operations
Language: C++20 (GCC 13-64)


Submission (verdict: Time limit exceeded on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define rep(i,x,y) for(int i=x; i<y; i++)
bool in_range(int x,int y,int r,int c){
return (x>=0 && y>=0 && x<r && y<c);
}
bool compare(pair<vi,int>a,pair<vi,int>b){
    return (a.first[0]<b.first[0]);
}
int sum(const vi &a){
    int ret=0;
    rep(i,0,a.size())ret+=a[i];
    return ret;
}
vi diff(const vi &a){
    vi b;
    rep(i,0,a.size()-1){
        b.push_back(a[i+1]-a[i]);
    }
    return b;
}
void f(vi &a,int &ans,vi &prev){
    if(a.size()==1){
        ans=max(ans,a[0]);
       return ;
    }
    vi s=a;
    reverse(a.begin(),a.end());
    int rev_value=sum(a);
    int normal_diff_value=sum(diff(s));
    ans=max(rev_value,normal_diff_value);
    if(a!=prev)
    f(a,ans,prev);
    vi new_vector=diff(s);
    prev=new_vector;
    f(new_vector,ans,prev);  
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); 
int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    vi a(n);
    rep(i,0,n)cin>>a[i];
    int ans=INT_MIN;
    ans=max(ans,sum(a));
    vi prev=a;
    f(a,ans,prev);
    cout<<ans<<endl;
   
}
return 0;
}
Problem: 2062C - Cirno and Operations
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define rep(i,x,y) for(int i=x; i<y; i++)
bool in_range(int x,int y,int r,int c){
return (x>=0 && y>=0 && x<r && y<c);
}
bool compare(pair<vi,int>a,pair<vi,int>b){
    return (a.first[0]<b.first[0]);
}
unordered_map<string, int> dp;
int sum(const vi &a) {
    string key = to_string(a.size()) + "-";
    for (int num : a) key += to_string(num) + ",";
    
    if (dp.find(key) != dp.end()) return dp[key]; 

    int ret = accumulate(a.begin(), a.end(), 0LL);
    return dp[key] = ret; 
}
vi diff(const vi &a){
    vi b;
    rep(i,0,a.size()-1){
        b.push_back(a[i+1]-a[i]);
    }
    return b;
}
void f(vi &a,int &ans,vi &prev){
    if(a.size()==1){
        ans=max(ans,a[0]);
       return ;
    }
    vi s=a;
    reverse(a.begin(),a.end());
    int rev_value=sum(a);
    int normal_diff_value=sum(diff(s));
    ans=max(rev_value,normal_diff_value);
    if(a!=prev)
    f(a,ans,prev);
    vi new_vector=diff(s);
    prev=new_vector;
    f(new_vector,ans,prev);  
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); 
int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    vi a(n);
    rep(i,0,n)cin>>a[i];
    int ans=INT_MIN;
    ans=max(ans,sum(a));
    vi prev=a;
    f(a,ans,prev);
    cout<<ans<<endl;
   
}
return 0;
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define rep(i,x,y) for(int i=x; i<y; i++)
bool in_range(int x,int y,int r,int c){
return (x>=0 && y>=0 && x<r && y<c);
}
bool compare(pair<vi,int>a,pair<vi,int>b){
    return (a.first[0]<b.first[0]);
}
unordered_map<string, int> dp;
int sum(const vi &a) {
    string key = to_string(a.size()) + "-";
    for (int num : a) key += to_string(num) + ",";
    
    if (dp.find(key) != dp.end()) return dp[key]; 

    int ret = accumulate(a.begin(), a.end(), 0LL);
    return dp[key] = ret; 
}
vi diff(const vi &a){
    vi b;
    rep(i,0,a.size()-1){
        b.push_back(a[i+1]-a[i]);
    }
    return b;
}
// void f(vi &a,int &ans,vi &prev){
//     if(a.size()==1){
//         ans=max(ans,a[0]);
//        return ;
//     }
//     vi s=a;
//     reverse(a.begin(),a.end());
//     int rev_value=sum(a);
//     int normal_diff_value=sum(diff(s));
//     ans=max(rev_value,normal_diff_value);
//     if(a!=prev)
//     f(a,ans,prev);
//     vi new_vector=diff(s);
//     prev=new_vector;
//     f(new_vector,ans,prev);  
// }
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); 
int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    vi a(n);
    rep(i,0,n)cin>>a[i];
    int ans=accumulate(a.begin(),a.end(),0LL);
    while(a.size()>1){
        vi b(a.size()-1);
        for(int i=1;i<a.size();i++){
            b[i-1]=a[i]-a[i-1];
        }
        // if(b.size()>1)
        ans=max(ans,abs(accumulate(b.begin(),b.end(),0LL)));
        a=b;
    }
    cout<<ans<<endl;
    
   
}
return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include<iostream>
#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
const int mod=998244353,N=200005;
vector<int>G[N];
int du[N],n;
int Fnd(){
	int it=0,i;
	for(i=1;i<=n;i++){
		if(du[i]>du[it])it=i;
	}
	int res=du[it];du[it]=-1;
//	printf("!%d %d\n",it,res);
	for(int u:G[it]){du[u]--;}
	return res-1;
}
signed main(){
	int i,T,x,y;
	scanf("%d",&T);du[0]=-1;
	while(T--){
		scanf("%d",&n);
		for(i=1;i<=n;i++)du[i]=0,G[i].clear();
		for(i=1;i<n;i++){
			scanf("%d%d",&x,&y);
			du[x]++;du[y]++;
			G[x].push_back(y),G[y].push_back(x);
		}
		int s1=Fnd(),s2=Fnd();
		printf("%d\n",1+s1+s2); 
	}
}

Submission (verdict: Wrong answer on test 2):
#include<iostream>
#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
const int mod=998244353,N=200005;
vector<int>G[N];
int du[N],n;
signed main(){
	int i,T,x,y;
	scanf("%d",&T);du[0]=-1;
	while(T--){
		scanf("%d",&n);
		for(i=1;i<=n;i++)du[i]=0,G[i].clear();
		for(i=1;i<n;i++){
			scanf("%d%d",&x,&y);
			du[x]++;du[y]++;
			G[x].push_back(y),G[y].push_back(x);
		}
		int ct=0,mx=0;
		for(i=1;i<=n;i++){
			if(mx==du[i])ct++;
			else if(mx<du[i])mx=du[i],ct=1;
		}
		if(ct>=3){
			printf("%d\n",1+mx-1+mx-1);
		}
		else if(ct==2)printf("%d\n",2*mx-2);
		else{
			int it;
			for(i=1;i<=n;i++){
				if(mx==du[i]){it=i;break;}
			}
			for(int u:G[it])du[u]--;
			int rs=0;
			for(i=1;i<=n;i++){
				if(i==it)continue;
				rs=max(rs,du[i]);
			}
			printf("%d\n",1+mx-1+rs-1);
		}
	}
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)


Submission (verdict: Compilation error):
#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int mod=998244353,N=200005;
vector<int>G[N];
int du[N],n;
int Fnd(){
	int it=0,i;
	for(i=1;i<=n;i++){
		if(du[i]>du[it])it=i;
	}
	int res=du[it];du[it]=-1;
//	printf("!%d %d\n",it,res);
	for(int u:G[it]){du[u]--;}
	return res-1;
}
signed main(){
	int i,T,x,y;
	scanf("%d",&T);du[0]=-1;
	while(T--){
		scanf("%d",&n);
		for(i=1;i<=n;i++)du[i]=0,G[i].clear();
		for(i=1;i<n;i++){
			scanf("%d%d",&x,&y);
			du[x]++;du[y]++;
			G[x].push_back(y),G[y].push_back(x);
		}
		int s1=Fnd(),s2=Fnd();
		printf("%d\n",1+s1+s2); 
	}
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Compilation error):
#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int mod=998244353,N=200005;
vector<int>G[N];
int du[N],n;
int Fnd(){
	int it=0,i;
	for(i=1;i<=n;i++){
		if(du[i]>du[it])it=i;
	}
	int res=du[it];du[it]=-1;
//	printf("!%d %d\n",it,res);
	for(int u:G[it]){du[u]--;}
	return res-1;
}
signed main(){
	int i,T,x,y;
	scanf("%d",&T);du[0]=-1;
	while(T--){
		scanf("%d",&n);
		for(i=1;i<=n;i++)du[i]=0,G[i].clear();
		for(i=1;i<n;i++){
			scanf("%d%d",&x,&y);
			du[x]++;du[y]++;
			G[x].push_back(y),G[y].push_back(x);
		}
		int s1=Fnd(),s2=Fnd();
		printf("%d\n",1+s1+s2); 
	}
}

Submission (verdict: Wrong answer on test 2):
#include<iostream>
#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
const int mod=998244353,N=200005;
vector<int>G[N];
int du[N],n;
int Fnd(){
	int it=0,i;
	for(i=1;i<=n;i++){
		if(du[i]>du[it])it=i;
	}
	int res=du[it];du[it]=-1;
//	printf("!%d %d\n",it,res);
	for(int u:G[it]){du[u]--;}
	return res-1;
}
signed main(){
	int i,T,x,y;
	scanf("%d",&T);du[0]=-1;
	while(T--){
		scanf("%d",&n);
		for(i=1;i<=n;i++)du[i]=0,G[i].clear();
		for(i=1;i<n;i++){
			scanf("%d%d",&x,&y);
			du[x]++;du[y]++;
			G[x].push_back(y),G[y].push_back(x);
		}
		int s1=Fnd(),s2=Fnd();
		printf("%d\n",1+s1+s2); 
	}
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include<iostream>
#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
const int mod=998244353,N=200005;
vector<int>G[N];
int du[N],n;
signed main(){
	int i,T,x,y;
	scanf("%d",&T);du[0]=-1;
	while(T--){
		scanf("%d",&n);
		for(i=1;i<=n;i++)du[i]=0,G[i].clear();
		for(i=1;i<n;i++){
			scanf("%d%d",&x,&y);
			du[x]++;du[y]++;
			G[x].push_back(y),G[y].push_back(x);
		}
		int ct=0,mx=0;
		for(i=1;i<=n;i++){
			if(mx==du[i])ct++;
			else if(mx<du[i])mx=du[i],ct=1;
		}
		if(ct>=3){
			printf("%d\n",1+mx-1+mx-1);
		}
		else if(ct==2)printf("%d\n",2*mx-2);
		else{
			int it;
			for(i=1;i<=n;i++){
				if(mx==du[i]){it=i;break;}
			}
			for(int u:G[it])du[u]--;
			int rs=0;
			for(i=1;i<=n;i++){
				if(i==it)continue;
				rs=max(rs,du[i]);
			}
			printf("%d\n",1+mx-1+rs-1);
		}
	}
}

Submission (verdict: Accepted):
#include<iostream>
#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
const int mod=998244353,N=200005;
vector<int>G[N];
int du[N],n;
signed main(){
	int i,T,x,y;
	scanf("%d",&T);du[0]=-1;
	while(T--){
		scanf("%d",&n);
		for(i=1;i<=n;i++)du[i]=0,G[i].clear();
		for(i=1;i<n;i++){
			scanf("%d%d",&x,&y);
			du[x]++;du[y]++;
			G[x].push_back(y),G[y].push_back(x);
		}
		int ct=0,mx=0;
		for(i=1;i<=n;i++){
			if(mx==du[i])ct++;
			else if(mx<du[i])mx=du[i],ct=1;
		}
		if(ct>=3){
			printf("%d\n",2*mx-1);
		}
		else if(ct==2){
			int p=0,q=0;
			for(i=1;i<=n;i++){
				if(du[i]==mx){
					if(!p)p=i;
					else q=i;
				}
			}
			int flg=0;
			for(int u:G[p]){
				if(u==q)flg=1;
			}
			printf("%d\n",2*mx-1-flg);
		}
		else{
			int it;
			for(i=1;i<=n;i++){
				if(mx==du[i]){it=i;break;}
			}
			for(int u:G[it])du[u]--;
			int rs=0;
			for(i=1;i<=n;i++){
				if(i==it)continue;
				rs=max(rs,du[i]);
			}
			printf("%d\n",1+mx-1+rs-1);
		}
	}
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)


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

#define pb push_back
#define sz(v) ((int)(v).size())

using namespace std;

class Task {
public:
	bool oneMore;
	int vip_node;
	void run1(int node, int from, int now_depth, int first_gp_member_depth, map<int, vector<int>>& conn, set<int> gp) {
		if (oneMore) {
			return;
		}
		bool is_gp_member = gp.find(node) != gp.end();
		if (is_gp_member) {
			if (first_gp_member_depth == -1) {
				first_gp_member_depth = now_depth;
			}
			if (first_gp_member_depth + 1 < now_depth) {
				oneMore = true;
				return;
			}
		}
		for (int next_node : conn[node]) {
			if (next_node != from) {
				run1(next_node, node, now_depth + 1, first_gp_member_depth, conn, gp);
			}
		}
	}
	void run2(int node, int from, int now_depth, int vip_depth, int first_gp_member_depth, map<int, vector<int>>& conn, set<int> gp) {
		if (oneMore) {
			return;
		}
		if (node == vip_node) {
			vip_depth = now_depth;
			if (first_gp_member_depth != -1 && first_gp_member_depth + 1 < now_depth) {
				oneMore = true;
				return;
			}
		}
		bool is_gp_member = gp.find(node) != gp.end();
		if (is_gp_member) {
			if (first_gp_member_depth == -1) {
				first_gp_member_depth = now_depth;
			}
			if (vip_depth != -1 && vip_depth + 1 < now_depth) {
				oneMore = true;
				return;
			}
		}
		for (int next_node : conn[node]) {
			if (next_node != from) {
				run2(next_node, node, now_depth + 1, vip_depth, first_gp_member_depth, conn, gp);
			}
		}
	}
	void solveOne() {
		int l, x, y, result;
		cin >> l;
		map<int, vector<int>> conn;
		for (int i = 0; i < l-1; i++) {
			cin >> x >> y;
			conn[x].pb(y);
			conn[y].pb(x);
		}
		if (l <= 4) {
			cout << l-2 << "\n";
			return;
		}
		map<int, vector<int>> deg;
		for (int i = 1; i <= l; i++) {
			x = sz(conn[i]);
			deg[x-1].pb(i);
		}
		auto ptr = --deg.end();
		oneMore = false;
		if (sz((*ptr).second) >= 2) {
			result = (*ptr).first * 2;
			vector<int> gp_v = (*ptr).second;
			set<int> gp;
			for (int i : gp_v) {
				gp.insert(i);
			}
			run1(1, 0, 0, -1, conn, gp);
		}
		else {
			auto ptr2 = ptr;
			--ptr2;
			result = (*ptr).first + (*ptr2).first;
			vip_node = (*ptr).second[0];
			vector<int> gp_v = (*ptr2).second;
			set<int> gp;
			for (int i : gp_v) {
				gp.insert(i);
			}
			run2(1, 0, 0, -1, -1, conn, gp);
		}
		cout << result + oneMore << "\n";
	}
	void solve() {
		int t;
		cin >> t;
		for (int i = 1; i <= t; i++) {
			solveOne();
		}
	}
};
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	Task solver;
	solver.solve();
	return 0;
}