ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063B - Subsequence Update
Language: C++23 (GCC 14-64, msys2)

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

#define endl "\n"
#define Yahia cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0);
#define INF INT64_MAX
#define int long long int
const int sz = 1e5 + 5;
const int OO = 0x3f3f3f3f;

void execute_case(int tc) {
    int n, k;
    cin >> n >> k;
    vector<int> v(n + 1);
    for (int i = 1; i <= n; ++i) cin >> v[i];

    if (n == k) {
        int idx = 1;
        for (int i = 2; i <= n; i += 2) {
            if (v[i] != idx) return void(cout << idx);
            ++idx;
        }
        return void(cout << idx);
    }

    for (int i = 2; i <= n - k + 2; ++i) {
        if (v[i] != 1) return void(cout << 1);
    }

    int idx = 2;
    for (int i = n - k + 1; i <= n; i += 2) {
        if (v[i] != idx) return void(cout << idx);
        ++idx;
    }

    return void(cout << idx);
}

signed main() {
    Yahia
    int t = 1;
    cin >> t;
    for (int tc = 1; tc <= t; ++tc) {
        execute_case(tc);
        cout << endl;
    }
    return 0;
}


Submission (verdict: Accepted):
#include <iostream>
#include <bits/stdc++.h>
#include <map>
#include <cctype>
using namespace std;
#define endl "\n"
#define Yahia cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0);
#define INF INT64_MAX
#define int long long int
const int sz=1e5+5;
const int OO=0x3f3f3f3f;
#include <unordered_set>

int find_mex(const set<int>& arr) {
    unordered_set<int> present_elements;
    for (int num : arr) {
        present_elements.insert(num);
    }
    int mex = 0;
    while (present_elements.find(mex) != present_elements.end()) {
        mex++;
    }
    return mex;
}


void execute_case(int tc) {
   int n,l,r; cin>>n>>l>>r;
    vector<int>v(n),f,s;
    for(auto &i:v)cin>>i;
    for(int i=0;i<r;++i)f.push_back(v[i]);
    for(int i=l-1;i<n;++i)s.push_back(v[i]);
    sort(s.begin(),s.end());
    sort(f.begin(),f.end());
    int sf{},ss{};
    for(int i=0;i<r-l+1;++i)sf+=f[i];
    for(int i=0;i<r-l+1;++i)ss+=s[i];
    cout<<min(sf,ss);

}
signed main(){
    Yahia
    int t;
    cin>>t;
    for(int tc=1;tc<=t;++tc)
    {
        execute_case(tc);
        cout<<endl;
    }
    return 0;
}
Problem: 2063B - Subsequence Update
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include <iostream>

#include <bits/stdc++.h>

#include <map>

#include <cctype>

using namespace std;

#define endl "\n"

#define Yahia cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0);

#define INF INT64_MAX

#define int long long int

const int sz=1e5+5;

const int OO=0x3f3f3f3f;

#include <unordered_set>



int find_mex(const set<int>& arr) {

    unordered_set<int> present_elements;

    for (int num : arr) {

        present_elements.insert(num);

    }

    int mex = 0;

    while (present_elements.find(mex) != present_elements.end()) {

        mex++;

    }

    return mex;

}





void execute_case(int tc) {

   int n,l,r; cin>>n>>l>>r;

    vector<int>v(n);

    for(auto &i:v)cin>>i;

    int s=accumulate(v.begin(),v.end(),0);

    if(l==1 and r==n) {

        return void(cout<<s);

    }

    sort(v.begin(),v.end());

    int range=r-l+1;

    int ans{};

    for(int i=0;i<range;++i)ans+=v[i];

    cout<<ans;

}

signed main(){

    Yahia

    int t;

    cin>>t;

    for(int tc=1;tc<=t;++tc)

    {

        execute_case(tc);

        cout<<endl;

    }

    return 0;

}

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

#define endl "\n"
#define Yahia cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0);
#define INF INT64_MAX
#define int long long int
const int sz = 1e5 + 5;
const int OO = 0x3f3f3f3f;

void execute_case(int tc) {
    int n, k;
    cin >> n >> k;
    vector<int> v(n + 1);
    for (int i = 1; i <= n; ++i) cin >> v[i];

    if (n == k) {
        int idx = 1;
        for (int i = 2; i <= n; i += 2) {
            if (v[i] != idx) return void(cout << idx);
            ++idx;
        }
        return void(cout << idx);
    }

    for (int i = 2; i <= n - k + 2; ++i) {
        if (v[i] != 1) return void(cout << 1);
    }

    int idx = 2;
    for (int i = n - k + 1; i <= n; i += 2) {
        if (v[i] != idx) return void(cout << idx);
        ++idx;
    }

    return void(cout << idx);
}

signed main() {
    Yahia
    int t = 1;
    cin >> t;
    for (int tc = 1; tc <= t; ++tc) {
        execute_case(tc);
        cout << endl;
    }
    return 0;
}
Problem: 2057C - Trip to the Olympiad
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 1):
//#pragma GCC target( "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#include <bits/stdc++.h>
using namespace std;
#define accepted ios_base::sync_with_stdio(0);cin.tie(0);
#define Daulbekov signed
#define Makan main
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const long long N=2e6+5;
const long long MOD=998244353;
const long long INF=2e18;
int binpow(int a,int b){
	a%=MOD;
	if(b==0)return 1;
	if(b%2)return binpow(a,b-1)*a%MOD;
	else{
		int res=binpow(a,b/2);
		return res*res%MOD;
	}
}
bool cmp(pair<int,int>a,pair<int,int>b){
	if(a.F/300==b.F/300)return a.S<b.S;
	return a.F<b.F;
}
Daulbekov Makan(){
//	freopen("tic-tac-toe.in","r",stdin);
//	freopen("tic-tac-toe.out","w",stdout);
	accepted
	int t;
	cin>>t;
	while(t--){
		int l,r;
		cin>>l>>r;
		int x=l,y=l+1,z=0;
		for(int cur=log2(r);cur>=0;cur--){
			if((x&(1<<cur))&(y&(1<<cur))){
				if(z+(1<<cur)-1<l)z+=(1<<cur);
			}
			else{
				if(z+(1<<cur)<=r)z+=(1<<cur);
			}
		}
		cout<<x<<" "<<y<<" "<<z<<"\n";
	}
}

Submission (verdict: Wrong answer on test 1):
//#pragma GCC target( "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#include <bits/stdc++.h>
using namespace std;
#define accepted ios_base::sync_with_stdio(0);cin.tie(0);
#define Daulbekov signed
#define Makan main
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const long long N=2e6+5;
const long long MOD=998244353;
const long long INF=2e18;
int binpow(int a,int b){
	a%=MOD;
	if(b==0)return 1;
	if(b%2)return binpow(a,b-1)*a%MOD;
	else{
		int res=binpow(a,b/2);
		return res*res%MOD;
	}
}
bool cmp(pair<int,int>a,pair<int,int>b){
	if(a.F/300==b.F/300)return a.S<b.S;
	return a.F<b.F;
}
Daulbekov Makan(){
//	freopen("tic-tac-toe.in","r",stdin);
//	freopen("tic-tac-toe.out","w",stdout);
	accepted
	int t;
	cin>>t;
	while(t--){
		int l,r;
		cin>>l>>r;
		int x=l,y=r,z=0;
		for(int cur=log2(r);cur>=0;cur--){
			if((x&(1<<cur))&(y&(1<<cur))){
				if(z+(1<<cur)-1<l)z+=(1<<cur);
			}
			else{
				if(z+(1<<cur)<=r)z+=(1<<cur);
			}
		}
		if(z==r)z--;
		else if(x==l)z++;
		cout<<x<<" "<<y<<" "<<z<<"\n";
	}
}
Problem: 2057C - Trip to the Olympiad
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 1):
//#pragma GCC target( "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#include <bits/stdc++.h>
using namespace std;
#define accepted ios_base::sync_with_stdio(0);cin.tie(0);
#define Daulbekov signed
#define Makan main
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const long long N=2e6+5;
const long long MOD=998244353;
const long long INF=2e18;
int binpow(int a,int b){
	a%=MOD;
	if(b==0)return 1;
	if(b%2)return binpow(a,b-1)*a%MOD;
	else{
		int res=binpow(a,b/2);
		return res*res%MOD;
	}
}
bool cmp(pair<int,int>a,pair<int,int>b){
	if(a.F/300==b.F/300)return a.S<b.S;
	return a.F<b.F;
}
Daulbekov Makan(){
//	freopen("tic-tac-toe.in","r",stdin);
//	freopen("tic-tac-toe.out","w",stdout);
	accepted
	int t;
	cin>>t;
	while(t--){
		int l,r;
		cin>>l>>r;
		int x=l,y=r,z=0;
		for(int cur=log2(r);cur>=0;cur--){
			if((x&(1<<cur))&(y&(1<<cur))){
				if(z+(1<<cur)-1<l)z+=(1<<cur);
			}
			else{
				if(z+(1<<cur)<=r)z+=(1<<cur);
			}
		}
		if(z==r)z--;
		else if(x==l)z++;
		cout<<x<<" "<<y<<" "<<z<<"\n";
	}
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native,avx2")
#define int long long
using namespace std;
main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int l, r;
        cin >> l >> r;
        int sum = 0;
        for (int j = 30; j >= 0; j--) {
            if (((1<<j) & l) == ((1<<j) & r)) {
                sum += ((1LL << j) & l);
            } else {
                int a = sum + (1LL << j);
                int b = a - 1;
                int x = l;
                while (x == a || x == b) {
                    x++;
                }
                cout << a << ' ' << b << ' ' << x << endl;
                break;
            }
        }
    }
}
Problem: 2057C - Trip to the Olympiad
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 1):
//#pragma GCC target( "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#include <bits/stdc++.h>
using namespace std;
#define accepted ios_base::sync_with_stdio(0);cin.tie(0);
#define Daulbekov signed
#define Makan main
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const long long N=2e6+5;
const long long MOD=998244353;
const long long INF=2e18;
int binpow(int a,int b){
	a%=MOD;
	if(b==0)return 1;
	if(b%2)return binpow(a,b-1)*a%MOD;
	else{
		int res=binpow(a,b/2);
		return res*res%MOD;
	}
}
bool cmp(pair<int,int>a,pair<int,int>b){
	if(a.F/300==b.F/300)return a.S<b.S;
	return a.F<b.F;
}
Daulbekov Makan(){
//	freopen("tic-tac-toe.in","r",stdin);
//	freopen("tic-tac-toe.out","w",stdout);
	accepted
	int t;
	cin>>t;
	while(t--){
		int l,r;
		cin>>l>>r;
		int x=l,y=l+1,z=0;
		for(int cur=log2(r);cur>=0;cur--){
			if((x&(1<<cur))&(y&(1<<cur))){
				if(z+(1<<cur)-1<l)z+=(1<<cur);
			}
			else{
				if(z+(1<<cur)<=r)z+=(1<<cur);
			}
		}
		cout<<x<<" "<<y<<" "<<z<<"\n";
	}
}
Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):

#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define deb(...)
#define debArr(...)
#endif
#define ll long long
#define ld long double
#define ull unsigned long long
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define alln(x) (begin(x) + 1), (end(x))
#define allrn(x) (rbegin(x)), (rbegin(x) + (sz(x) - 1))
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define FORs(i, a, b) for (int i = a; i <= b; ++i)
#define fi(a, n) for (int i = a; i <= n; ++i)
#define fj(a, n) for (int j = a; j <= n; ++j)
#define fk(a, n) for (int k = a; k <= n; ++k)
#define fl(a, n) for (int l = a; l <= n; ++l)
#define fiz(a, n) for (int i = a; i < n; ++i)
#define trv(x, a) for (auto &x : a)
#define endl '\n'
#define vvi vector<vi>
#define mat(n, m) (n, vector<int>(m, 0))
#define mem(x) memset(x, 0, sizeof x)
#define int ll

bool pair_sort(const pair<ll, ll> &a, const pair<ll, ll> &bb)
{
    if (a.first == bb.first)
    {
        return a.second < bb.second;
    }
    return (a.first > bb.first);
}

int dx[] = {0, 1, -1, 0, 0};
int dy[] = {0, 0, 0, 1, -1};

const int N = 200020;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;

const int inf = 1e18;

void solve()
{
    int n, m;
    cin >> n >> m;
    vi a(n + 1), b(n + 1);
    multiset<int> sa, sb;
    fi(1, n)
    {
        cin >> a[i];
        sa.insert(a[i]);
    }
    fi(1, m)
    {
        cin >> b[i];
        sb.insert(b[i]);
    }

    fi(1, m)
    {
        if (sa.count(b[i]))
        {
            sa.erase(sa.find(b[i]));
            sb.erase(sb.find(b[i]));
        }
    }

    while (sz(sa))
    {
        int first = *sa.begin();
        sa.erase(sa.find(first));
        int second = *sa.begin();

        if (sb.count(first))
        {
            sb.erase(sb.find(first));
        }
        else if (sb.count(second))
        {
            sa.insert(first);
            sa.erase(sa.find(second));
        }
        else if (sz(sa) and abs(second - first) <= 1)
        {
            sa.erase(sa.find(second));
            sa.insert(first + second);
        }
        else
        {
            cout << "NO" << endl;
            return;
        }
    }
    // deb(sa, sb)

    cout << "YES" << endl;
}
bool tests = 1;
int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    if (tests)
        cin >> t;
    fi(1, 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;

#define int long long
//
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define vi  vector<int>
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define vvi(v,n,m,k) vector<vi>v(n,vi(m,k));
#define endl "\n"
#define MOD 998244353      
#define print(v) for(int i=0;i<v.size();i++){cout<<v[i]<<" ";} cout<<endl;
#define printl(v) for(auto it :v){cout<<it<<endl;}
#define printp(v) for(auto it : v){cout<<it.ff<<" "<<it.ss<<endl;}
#define printvv(v) {for(int i=0;i<v.size();i++){print(v[i]);}}
//
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)

void solve(){
    int n;
    cin>>n;
    vector<vi>adj(n);
    for(int i=0;i<n-1;i++){
        int x,y;
        cin>>x>>y;
        x--;
        y--;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    vector<pii>v;
    vi incoming(n,0);
    for(int i=0;i<n;i++){
        incoming[i]=adj[i].size();
        v.pb({adj[i].size(),i});
    }
    sort(all(v));
    int cnt=v[n-1].ff;
    int node=v[n-1].ss;
    incoming[node]=0;
    for(int i=0;i<adj[node].size();i++){
        int ver=adj[node][i];
        incoming[ver]=adj[ver].size()-1;
    }
    sort(all(incoming));
    cnt+=incoming[n-1]-1;
    cout<<min(cnt,n-2)<<endl;
}


signed main(){
    fastio();
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}

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

#define int long long
//
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define vi  vector<int>
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define vvi(v,n,m,k) vector<vi>v(n,vi(m,k));
#define endl "\n"
#define MOD 998244353      
#define print(v) for(int i=0;i<v.size();i++){cout<<v[i]<<" ";} cout<<endl;
#define printl(v) for(auto it :v){cout<<it<<endl;}
#define printp(v) for(auto it : v){cout<<it.ff<<" "<<it.ss<<endl;}
#define printvv(v) {for(int i=0;i<v.size();i++){print(v[i]);}}
//
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)

void solve(){
    int n;
    cin>>n;
    vector<vi>adj(n);
    for(int i=0;i<n-1;i++){
        int x,y;
        cin>>x>>y;
        x--;
        y--;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    vector<pii>v;
    vi incoming(n,0);
    for(int i=0;i<n;i++){
        incoming[i]=adj[i].size();
        v.pb({adj[i].size(),i});
    }
    sort(all(v));
    int cnt=v[n-1].ff;
    int node=v[n-1].ss;
    incoming[node]=0;
    for(int i=0;i<adj[node].size();i++){
        int ver=adj[node][i];
        incoming[ver]--;
    }
    sort(all(incoming));
    cnt+=incoming[n-1]-1;
    cout<<cnt<<endl;
}


signed main(){
    fastio();
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)


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

#define int long long
//
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define vi  vector<int>
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define vvi(v,n,m,k) vector<vi>v(n,vi(m,k));
#define endl "\n"
#define MOD 998244353      
#define print(v) for(int i=0;i<v.size();i++){cout<<v[i]<<" ";} cout<<endl;
#define printl(v) for(auto it :v){cout<<it<<endl;}
#define printp(v) for(auto it : v){cout<<it.ff<<" "<<it.ss<<endl;}
#define printvv(v) {for(int i=0;i<v.size();i++){print(v[i]);}}
//
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)

void solve(){
    int n;
    cin>>n;
    vector<vi>adj(n);
    for(int i=0;i<n-1;i++){
        int x,y;
        cin>>x>>y;
        x--;
        y--;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    vector<pii>v;
    for(int i=0;i<n;i++){
        v.pb({adj[i].size(),i});
    }
    sort(all(v));
    // printp(v);
    int cnt=v[n-1].ff;
    cnt+=v[n-2].ff-1;
    cout<<min(cnt,n-2)<<endl;
}


signed main(){
    fastio();
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}
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;

#define int long long
//
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define vi  vector<int>
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define vvi(v,n,m,k) vector<vi>v(n,vi(m,k));
#define endl "\n"
#define MOD 998244353      
#define print(v) for(int i=0;i<v.size();i++){cout<<v[i]<<" ";} cout<<endl;
#define printl(v) for(auto it :v){cout<<it<<endl;}
#define printp(v) for(auto it : v){cout<<it.ff<<" "<<it.ss<<endl;}
#define printvv(v) {for(int i=0;i<v.size();i++){print(v[i]);}}
//
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)

void solve(){
    int n;
    cin>>n;
    vector<vi>adj(n);
    for(int i=0;i<n-1;i++){
        int x,y;
        cin>>x>>y;
        x--;
        y--;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    vector<pii>v;
    vi incoming(n,0);
    for(int i=0;i<n;i++){
        incoming[i]=adj[i].size();
        v.pb({adj[i].size(),i});
    }
    sort(all(v));
    int cnt=v[n-1].ff;
    int node=v[n-1].ss;
    incoming[node]=0;
    for(int i=0;i<adj[node].size();i++){
        int ver=adj[node][i];
        incoming[ver]--;
    }
    sort(all(incoming));
    cnt+=incoming[n-1]-1;
    cout<<cnt<<endl;
}


signed main(){
    fastio();
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}

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

#define int long long
//
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define vi  vector<int>
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define vvi(v,n,m,k) vector<vi>v(n,vi(m,k));
#define endl "\n"
#define MOD 998244353      
#define print(v) for(int i=0;i<v.size();i++){cout<<v[i]<<" ";} cout<<endl;
#define printl(v) for(auto it :v){cout<<it<<endl;}
#define printp(v) for(auto it : v){cout<<it.ff<<" "<<it.ss<<endl;}
#define printvv(v) {for(int i=0;i<v.size();i++){print(v[i]);}}
//
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)

void solve(){
    int n;
    cin>>n;
    vector<vi>adj(n);
    for(int i=0;i<n-1;i++){
        int x,y;
        cin>>x>>y;
        x--;
        y--;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    vector<pii>v;
    vi incoming(n,0);
    for(int i=0;i<n;i++){
        incoming[i]=adj[i].size();
        v.pb({incoming[i],i});
    }
    sort(all(v));
    int cnt=v[n-1].ff;
    int node=v[n-1].ss;
    incoming[node]=-1;
    for(int i=0;i<adj[node].size();i++){
        int ver=adj[node][i];
        incoming[ver]--;
    }
    sort(all(incoming));
    cnt+=incoming[n-1]-1;
    int maxi=cnt;
    for(int i=0;i<n;i++){
        incoming[i]=adj[i].size();
    }
    cnt=v[n-2].ff;
    node=v[n-2].ss;
    incoming[node]=-1;
    for(int i=0;i<adj[node].size();i++){
        int ver=adj[node][i];
        incoming[ver]--;
    }
    sort(all(incoming));
    cnt+=incoming[n-1]-1;
    cout<<max(cnt,maxi)<<endl;
}


signed main(){
    fastio();
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

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

#define int long long
//
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define vi  vector<int>
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define vvi(v,n,m,k) vector<vi>v(n,vi(m,k));
#define endl "\n"
#define MOD 998244353      
#define print(v) for(int i=0;i<v.size();i++){cout<<v[i]<<" ";} cout<<endl;
#define printl(v) for(auto it :v){cout<<it<<endl;}
#define printp(v) for(auto it : v){cout<<it.ff<<" "<<it.ss<<endl;}
#define printvv(v) {for(int i=0;i<v.size();i++){print(v[i]);}}
//
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)

void solve(){
    int n;
    cin>>n;
    vector<vi>adj(n);
    for(int i=0;i<n-1;i++){
        int x,y;
        cin>>x>>y;
        x--;
        y--;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    vector<pii>v;
    for(int i=0;i<n;i++){
        v.pb({adj[i].size(),i});
    }
    sort(all(v));
    // printp(v);
    int cnt=v[n-1].ff;
    cnt+=v[n-2].ff-1;
    cout<<min(cnt,n-2)<<endl;
}


signed main(){
    fastio();
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}

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

#define int long long
//
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define vi  vector<int>
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define vvi(v,n,m,k) vector<vi>v(n,vi(m,k));
#define endl "\n"
#define MOD 998244353      
#define print(v) for(int i=0;i<v.size();i++){cout<<v[i]<<" ";} cout<<endl;
#define printl(v) for(auto it :v){cout<<it<<endl;}
#define printp(v) for(auto it : v){cout<<it.ff<<" "<<it.ss<<endl;}
#define printvv(v) {for(int i=0;i<v.size();i++){print(v[i]);}}
//
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)

void solve(){
    int n;
    cin>>n;
    vector<vi>adj(n);
    for(int i=0;i<n-1;i++){
        int x,y;
        cin>>x>>y;
        x--;
        y--;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    vector<pii>v;
    vi incoming(n,0);
    for(int i=0;i<n;i++){
        incoming[i]=adj[i].size();
        v.pb({adj[i].size(),i});
    }
    sort(all(v));
    int cnt=v[n-1].ff;
    int node=v[n-1].ss;
    incoming[node]=0;
    for(int i=0;i<adj[node].size();i++){
        int ver=adj[node][i];
        incoming[ver]=adj[ver].size()-1;
    }
    sort(all(incoming));
    cnt+=incoming[n-1]-1;
    cout<<min(cnt,n-2)<<endl;
}


signed main(){
    fastio();
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}