ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063B - Subsequence Update
Language: c++20 

Code 1:
#include"bits/stdc++.h"
using namespace std;
 
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define endl "\n"
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
 
typedef long long ll;
const int MOD = 1000000007;
const int Nmax = 2e5+5;

void akibify(){ 
ll n,l,r ;
cin>>n>>l>>r ;
vector<ll>v(n);
for(auto &a : v) cin>>a ;
sort(v.begin(),v.end());
ll ans = 0 ;
for(int i = 0 ; i < r-l+1 ; i++){
   ans += v[i];
}
cout<<ans<<endl;
}

int main(){
   fast_io;
    int t ;
    cin>>t ;
    while(t--){
    akibify();

        
    }
 
 
    return 0;
}

Code 2:
#include"bits/stdc++.h"
using namespace std;
 
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define endl "\n"
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
 
typedef long long ll;
const int MOD = 1000000007;
const int Nmax = 2e5+5;

void akibify(){ 
ll n,l,r ;
cin>>n>>l>>r ;
vector<ll>v(n);
vector<ll>lv,rv;
for(auto &a : v) cin>>a ;

for(int i = 0 ; i < r ; i++) lv.push_back(v[i]);
for(int i = l-1 ; i < n ; i++) rv.push_back(v[i]);
sort(lv.begin(),lv.end());
sort(rv.begin(),rv.end());
ll suml=0,sumr = 0;
for(int i = 0 ; i < r-l+1 ; i++) suml+=lv[i];
for(int i = 0 ; i < r-l+1 ; i++) sumr+=rv[i];
ll ans = min(suml,sumr) ;

cout<<ans<<endl;

}

int main(){
   fast_io;
    int t ;
    cin>>t ;
    while(t--){
    akibify();

        
    }
 
 
    return 0;
}

Answer: 2
Problem: 2063B - Subsequence Update
Language: c++17 

Code 1:
def solve():

    n,l,r=tuple(map(int,input().split()))

    a=list(map(int,input().split()))

    a.sort()

    res=0

    for i in range(r-l+1):

        res+=a[i]

    print(res)

        

T=int(input())

for _ in range(T):

    solve()

    



Code 2:
def solve():

    n,l,r=tuple(map(int,input().split()))

    a=list(map(int,input().split()))

    l-=1

    r-=1

    ll=a[:r+1]

    rr=a[l:]

    ll.sort()

    rr.sort()

    print(min(sum(ll[0:r-l+1]),sum(rr[0:r-l+1])))

T=int(input())

for _ in range(T):

    solve()

    



Answer: 2
Problem: 2063B - Subsequence Update
Language: c++20 

Code 1:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int test;
    cin>>test;
    while(test--)
    {
        int n,l,r;
        cin>>n>>l>>r;
        vector<int> vec(n);
        for(int i=0;i<n;i++)cin>>vec[i];
        ll sum1=0,sum2=0;
        vector<int> vec2(n);
        for(int i=0;i<n;i++)vec2[i]=vec[i];
        sort(vec.begin(),vec.begin()+r-1);
        sort(vec2.begin()+l-1,vec2.end());
        for(int i=0;i<r-l+1;i++)sum1+=vec[i];
        for(int i=l-1;i<r;i++)sum2+=vec2[i];
        cout<<min(sum1,sum2)<<endl;
    }
}

Code 2:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int test;
    cin>>test;
    while(test--)
    {
        int n,l,r;
        cin>>n>>l>>r;
        vector<int> vec(n);
        for(int i=0;i<n;i++)cin>>vec[i];
        ll sum1=0,sum2=0;
        vector<int> vec2 =vec;
        sort(vec.begin(),vec.begin()+r);
        sort(vec2.begin()+l-1,vec2.end());
        for(int i=0;i<r-l+1;i++)sum1+=vec[i];
        for(int i=l-1;i<r;i++)sum2+=vec2[i];
        cout<<min(sum1,sum2)<<endl;
    }
}

Answer: 2
Problem: 2063B - Subsequence Update
Language: c++17 

Code 1:
#include<iostream>

#include<cmath>

#include<cstring>

#include<algorithm>

using namespace std;

const int N=1e5+5;

int a[N];

typedef long long ll;

int main(){

	ios::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

	int  t;cin>>t;

	while(t--){

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

		for(int i=1;i<=n;i++)cin>>a[i];

		sort(a+1,a+1+n);

		ll sum=0;

		for(int i=1;i<=r-l+1;i++)sum+=a[i];

		cout<<sum<<"\n";

	}

}

Code 2:
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;

const int N=1e5+5;
int a[N],b[N];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int  t;cin>>t;
	while(t--){
		int n,l,r;
		cin>>n>>l>>r;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			b[i]=a[i];
		}
		sort(a+1,a+1+r);
		sort(b+l,b+n+1);
		ll sum1=0,sum2=0;
		for(int i=1;i<=r-l+1;i++)sum1+=a[i];
		for(int i=l;i<=r;i++)sum2+=b[i];
		cout<<min(sum1,sum2)<<"\n";
	}
}

Answer: 2
Problem: 2063B - Subsequence Update
Language: python

Code 1:
for _ in range(int(input())):
    n,l,r=map(int,input().split())
    l-=1
    r-=1
    a=list(map(int,input().split()))
    if l==0 and r==n-1:
        print(sum(a[l:r+1]))
        continue
    if l==r:
        print(min(a))
        continue
    sub,subleft,subright,t,res=sorted(a[l:r+1],reverse=True),sorted(a[:l]),sorted(a[r+1:]),sum(a[l:r+1]),float('inf')
    if l>0:
        change=0
        for i in range(r-l+1):
            if i<len(subleft):
                if subleft[i]<sub[i]:
                    change+=subleft[i]-sub[i]
            else:
                break
        res=min(res,t+change)
    if r<n:
        change=0
        for i in range(r-l+1):
            if i<len(subright):
                if subright[i]<sub[i]:
                    change+=subright[i]-sub[i]
            else:
                break
        res=min(res,t+change)
    print(res)

Code 2:
for _ in range(int(input())):

    n,l,r=map(int,input().split())

    l-=1

    r-=1

    a=list(map(int,input().split()))

    if l==0 and r==n-1:

        print(sum(a[l:r+1]))

        continue

    if l==r:

        print(min(a))

        continue

    sub,subleft,subright,t,res=sorted(a[l:r+1],reverse=True),sorted(a[:l]),sorted(a[r+1:]),sum(a[l:r+1]),9999999

    combined=[]

    for i in subleft:

        combined.append(i)

    for i in subright:

        combined.append(i)

    combined.sort()

    change=0

    for i in range(r-l+1):

        if i<len(combined):

            if combined[i]<sub[i]:

                change+=combined[i]-sub[i]

        else:

            break

    res=min(res,t+change)

    print(res)

    

Answer: 1
Problem: 2063B - Subsequence Update
Language: c++17 

Code 1:
#include<bits/stdc++.h>



using namespace std;



#define int long long

#define PB push_back

typedef vector<int> vi;

typedef vector<string> vs;



const int mod = 1e9 + 7;

const int inf = INT_MAX;



template<typename t>

vector<t> I(int n) {

    vector<t> vector;

    for (int i = 0; i < n; i++) {

        t input;

        cin >> input;

        vector.push_back(input);

    }



    return vector;

}



template<typename t>

void D(vector<t> vector) {

    for (auto i: vector) {

        cout << i << " ";

    }



    cout << "\n";

}





void solve() {

    int n, l , r;

    cin >> n >> l >> r;

    vi x = I<int>(n);

    vi first(x.begin(), x.begin() + r);

    vi second(x.begin() + l - 1, x.end());

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

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

    long long sum1 = accumulate(first.begin(), first.begin() + (r - l + 1), 0LL);

    long long sum2 = accumulate(second.begin(), second.begin() + (r - l + 1), 0LL);

    cout << min(sum1, sum2) << "\n";

}





int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);



    int t;

    cin >> t;

    while (t--) {

        solve();

    }

}



Code 2:
#include<bits/stdc++.h>



using namespace std;



#define int long long

#define PB push_back

typedef vector<int> vi;

typedef vector<string> vs;



const int mod = 1e9 + 7;

const int inf = INT_MAX;



template<typename t>

vector<t> I(int n) {

    vector<t> vector;

    for (int i = 0; i < n; i++) {

        t input;

        cin >> input;

        vector.push_back(input);

    }



    return vector;

}



template<typename t>

void D(vector<t> vector) {

    for (auto i: vector) {

        cout << i << " ";

    }



    cout << "\n";

}





void solve() {

    int n, l , r;

    cin >> n >> l >> r;

    vi x = I<int>(n);

    vi first(x.begin(), x.begin() + r);

    vi second(x.begin() + l - 1, x.end());

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

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

    int sum1 = accumulate(first.begin(), first.begin() + r - l + 1, 0);

    int sum2 = accumulate(second.begin(), second.begin() + r - l + 1, 0);

    cout << min(sum1, sum2) << "\n";

}





int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);



    int t;

    cin >> t;

    while (t--) {

        solve();

    }

}



Answer: 1
Problem: 2063B - Subsequence Update
Language: c++23 

Code 1:
#include<bits/stdc++.h>



using namespace std;



const int N=100010;



typedef long long ll;



int a[N],b[N];

ll sum1[N],sum2[N];



int main()

{

    int T;

    cin>>T;

    while(T--)

    {

        memset(a,0,sizeof a);

        memset(b,0,sizeof b);

        memset(sum1,0,sizeof sum1);

        memset(sum2,0,sizeof sum2);

        int n,l,r;

        cin>>n>>l>>r;

        

        for(int i=1;i<=n;i++)

        {

            scanf("%d",&a[i]);

        }

        

        for(int i=l;i<=n;i++)

        {

            b[i-l+1]=a[i];

        }

        

        sort(a+1,a+r+1);

        sort(b+1,b+n-l+2);

        

        for(int i=1;i<=r;i++)

        {

            sum1[i]=sum1[i-1]+a[i];

            sum2[i]=sum2[i-1]+b[i];

        }

        

        printf("%lld\n",min(sum1[r-l+1],sum2[r-l+1]));

    }

    

    return 0;

}



Code 2:
#include<bits/stdc++.h>



using namespace std;



const int N=100010;



typedef long long ll;



int a[N];

ll sum[N];



int main()

{

    int T;

    cin>>T;

    while(T--)

    {

        memset(a,0,sizeof a);

        memset(sum,0,sizeof sum);

        int n,l,r;

        cin>>n>>l>>r;

        

        for(int i=1;i<=n;i++)

        {

            scanf("%d",&a[i]);

        }

        

        sort(a+1,a+n+1);

        

        for(int i=1;i<=n;i++)

        {

            sum[i]=sum[i-1]+a[i];

        }

        

        printf("%lld\n",sum[r-l+1]-sum[0]);

    }

    

    return 0;

}



Answer: 1
Problem: 2063B - Subsequence Update
Language: c++23 

Code 1:
#include<iostream>

#include<algorithm>

#include<cstring>

#include<cmath>

#include<vector>

#include<queue>

using namespace std;

const int N=200010;

long long a[N],b[N],c[N];

void solve(){

	int n,l,r;

	cin>>n>>l>>r;

	for(int i=0;i<n;i++){

		cin>>a[i];

	}

	for(int i=0;i<r;i++) b[i]=a[i];

	int k=0;

	for(int i=l-1;i<n;i++) c[k++]=a[i];

	sort(b,b+r);

	sort(c,c+k);

	int t=r-l+1;

	long long minv=0;

	long long cnt=1e17;

	for(int i=0;i<t;i++) minv+=b[i];

	cnt=min(cnt,minv);

	minv=0;

	for(int i=0;i<t;i++) minv+=c[i];

	cnt=min(cnt,minv);

	cout << cnt << endl;

}

int main(){

	int t;

	cin>>t;

	while(t--){

		solve();

	}

	return 0;

} 

Code 2:
#include<iostream>

#include<algorithm>

#include<cstring>

#include<cmath>

#include<vector>

#include<queue>

using namespace std;

const int N=200010;

long long a[N],b[N],c[N];

void solve(){

	int n,l,r;

	cin>>n>>l>>r;

	for(int i=0;i<n;i++){

		cin>>a[i];

	}

	for(int i=0;i<r;i++) b[i]=a[i];

	int k=0;

	for(int i=l-1;i<n;i++) c[k++]=a[i];

	sort(b,b+r);

	sort(c,c+k);

	int t=r-l+1;

	long long minv=0;

	long long cnt=1e13;

	for(int i=0;i<t;i++) minv+=b[i];

	cnt=min(cnt,minv);

	minv=0;

	for(int i=0;i<t;i++) minv+=c[i];

	cnt=min(cnt,minv);

	cout << cnt << endl;

}

int main(){

	int t;

	cin>>t;

	while(t--){

		solve();

	}

	return 0;

} 

Answer: 1
Problem: 2063B - Subsequence Update
Language: c++20 

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

#define int long long

#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

void fastio(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

const int maxN=2e3;
const int INF=1e9;
const int MOD=1e9+7;

int t, n, l, r, ans1, ans2;
vi arrM, arr;

bool invComp(int a, int b){
    return a>=b;
}

signed main(){
    fastio();
    cin >> t;
    while(t--){
        cin >> n >> l >> r;
        arr.resize(n+50, 0);
        for(int i=0; i<n; i++){
            cin >> arr[i];
        }
        l--; r--;
        ans1=ans2=0;
        arrM=arr;
        sort(arrM.begin(), arrM.begin()+r+1, invComp);
        for(int i=l; i<=r; i++){
            ans1+=arrM[i];
        }
        arrM=arr;
        sort(arrM.begin()+l, arrM.begin()+n);
        for(int i=l; i<=r; i++){
            ans2+=arrM[i];
        }
        cout << min(ans1, ans2) << endl;
    }
    return 0;
}


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

#define int long long

#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

void fastio(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

const int maxN=2e3;
const int INF=1e9;
const int MOD=1e9+7;

int t, n, l, r, ans1, ans2, a;
vi arr1, arr2;

signed main(){
    fastio();
    cin >> t;
    while(t--){
        cin >> n >> l >> r;
        arr1.clear();
        arr2.clear();
        l--; r--;
        for(int i=0; i<n; i++){
            cin >> a;
            if(i<=r) arr1.pb(a);
            if(i>=l) arr2.pb(a);
        }
        ans1=ans2=0;
        sort(all(arr1));
        sort(all(arr2));
        for(int i=0; i<=r-l; i++){
            ans1+=arr1[i];
            ans2+=arr2[i];
        }
        cout << min(ans1, ans2) << endl;
    }
    return 0;
}


Answer: 2
Problem: 2063B - Subsequence Update
Language: c++20 

Code 1:
#include <bits/stdc++.h>

using namespace std;

typedef long long i64;

typedef __int128_t i128;

const int M = 1e9 + 7;

#define N 200005

void solve()

{

    int n, l, r;

    cin >> n >> l >> r;

    l--;

    vector<int> L, R;

    for (int i = 0; i < n; i++)

    {

        int x;

        cin >> x;

        if (i < r)

        {

            L.push_back(x);

        }

        if (i >= l)

        {

            R.push_back(x);

        }

    }

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

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

    i64 ans1 = accumulate(L.begin(), L.begin() + r - l, 0LL);

    i64 ans2 = accumulate(R.begin(), R.begin() + r - l, 0LL);

    cout << min(ans1, ans2) << endl;

}



int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

    int t;

    cin >> t;

    while (t--)

    {

        solve();

    }



    return 0;

}



Code 2:
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
const int M = 1e9 + 7;
#define N 200005

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    if (n == 1)
    {
        cout << 1 << " " << 1 << endl;
        return;
    }

    map<int, int> cnt;
    for (int i = 1; i <= n; i++)
    {
        cnt[a[i]]++;
    }

    int cntmx = 0;
    int k = -1;
    for (const auto &[i, cnt_] : cnt)
    {
        if (cnt_ > cntmx)
        {
            cntmx = cnt_;
            k = i;
        }
    }

    vector<int> idxs;
    for (int i = 1; i <= n; i++)
    {
        if (a[i] == k)
        {
            idxs.push_back(i);
        }
    }

    int dis = 0;
    int l = -1;
    int r = -1;
    for (int i = 1; i < idxs.size(); i++)
    {
    int temp = idxs[i] - idxs[i - 1] - 1;
        if (temp > dis)
        {
            dis = temp;
            l = idxs[i - 1];
            r = idxs[i];
        }
    }

    // 输出结果
    if (l != -1 && r != -1)
    {
        cout << l + 1 << " " << r - 1 << endl;
    }
    else
    {
        cout << 0 << endl;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Answer: 1
Problem: 2063B - Subsequence Update
Language: c++17 

Code 1:
#include<bits/stdc++.h>

using namespace std;



typedef long long ll;



void solve() {



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

    vector<int>v(n+1),ans1,ans2;

    for (int i = 1 ; i <= n ; ++i)

        cin>>v[i];

        int siz = r-l+1 , ans = 0;

        for (int i = l ; i <= r ; ++i)

            ans1.push_back(v[i]),ans2.push_back(v[i]);

        for (int i = 1 ; i < l ; ++i)

            ans1.push_back(v[i]);

        for (int i = r+1 ; i <= n ; ++i)

            ans2.push_back(v[i]);

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

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

        ll sum1 = 0 , sum2 = 0;

        for (int i = 0 ; i <siz ; ++i)

            sum1 += ans1[i];

        for (int i = 0 ; i < siz ; ++i)

            sum2 += ans2[i];

        cout<<min(sum1,sum2)<<endl;

}

int main() {



    int t ; cin>>t;

    while(t--)

        solve();

    return 0;

}





















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

typedef long long ll;

void solve() {

    int n,l,r ; cin>>n>>l>>r;
    vector<int>v(n+1);
    for (int i = 1 ; i <= n ; ++i)
        cin>>v[i];
        sort(v.begin(),v.end());
        int siz = r-l+1 , ans = 0;
        for (int i = 1 ; i <= siz ; ++i)
            ans += v[i];
        cout<<ans<<endl;
}
int main() {

    int t ; cin>>t;
    while(t--)
        solve();
    return 0;
}




Answer: 1
Problem: 2063C - Remove Exactly Two
Language: c++17 

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

void solve(){
	int n;cin>>n;
	vector<vector<int>> adj(n+1);
	vector<int> edges(n+1);
	for(int i=0; i<n-1; i++){
		int a,b;cin>>a>>b;
		adj[a].push_back(b);
		adj[b].push_back(a);
		edges[a]++;
		edges[b]++;
	}
	if(n==2){
		cout<<0<<endl;
		return ;
	}
	long long max = -1,node=-1;;
	for(int i=1; i<=n; i++){
		if(edges[i] > max){
			node = i;
			max = edges[i];
		}
	}
	bool flag = false;
	for(auto it: adj[node]){
		if(edges[it] == max){
			
			if(flag){
			cout<<max*2-1<<endl;
			return ;}flag = true;
		}
		edges[it]--;
	}
	long long max2=-1,node2=-1;
	for(int i=1; i<=n; i++){
		if(edges[i]>max2 && i != node){
			node2=i;
			max2=edges[i];
		} 
	}
	cout<<max+max2-1<<endl;
}

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

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

void solve(){
	int n;cin>>n;
	vector<vector<int>> adj(n+1);
	vector<int> edges(n+1);
	for(int i=0; i<n-1; i++){
		int a,b;cin>>a>>b;
		adj[a].push_back(b);
		adj[b].push_back(a);
		edges[a]++;
		edges[b]++;
	}
	if(n==2){
		cout<<0<<endl;
		return ;
	}
	long long max = -1,node=-1;;
	for(int i=1; i<=n; i++){
		if(edges[i] > max){
			node = i;
			max = edges[i];
		}
	}
	for(auto it: adj[node]){
		edges[it]--;
	}
	
	long long max2=-1,node2=-1;
	for(int i=1; i<=n; i++){
		if(edges[i]>max2 && i != node){
			node2=i;
			max2=edges[i];
		} 
	}
	cout<<max+max2-1<<endl;
}

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

Answer: 1
Problem: 2063C - Remove Exactly Two
Language: c++17 

Code 1:
#include <bits/stdc++.h>

using namespace std;

bool isInSet(pair<int,int> pr, set<pair<int,int>> &s) {
    return s.find(pr) != s.end();
}

void solve(){
    int n;
    cin>>n;
    set<pair<int, int>> edge_set;
    vector<pair<int,int>> deg(n);
    for(int i=0;i<n;i++) {
        deg[i].second = i;
        deg[i].first = 0;
    }
    for (int i=1;i<n;i++) {
        int u,v;
        cin>>u>>v;
        --u,--v;
        edge_set.insert(make_pair(u,v));
        edge_set.insert(make_pair(v,u));
        deg[u].first++;
        deg[v].first++;
    }
    if (n==2) {
        cout<<"0\n";
        return;
    }
    sort(deg.begin(), deg.end());
    int ans = 0;
    for(int i=n-1;i>=n-3;i--) {
        for(int j=i-1;j>=n-3;j--) {
            ans = max(ans, deg[i].first + deg[j].first - (isInSet(make_pair(deg[i].second, deg[j].second), edge_set) ? 1: 0));
        }
    }
    cout<<(ans-1)<<'\n';
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Code 2:
#include <bits/stdc++.h>

using namespace std;

bool isInSet(pair<int,int> pr, set<pair<int,int>> &s) {
    return s.find(pr) != s.end();
}

void solve(){
    int n;
    cin>>n;
    set<pair<int, int>> edge_set;
    vector<pair<int,int>> deg(n);
    for(int i=0;i<n;i++) {
        deg[i].second = i;
        deg[i].first = 0;
    }
    for (int i=1;i<n;i++) {
        int u,v;
        cin>>u>>v;
        --u,--v;
        edge_set.insert(make_pair(u,v));
        edge_set.insert(make_pair(v,u));
        deg[u].first++;
        deg[v].first++;
    }
    if (n==2) {
        cout<<"0\n";
        return;
    }
    sort(deg.begin(), deg.end());
    int ans = 0;
    for(int i=n-2;i>=0;i--) {
        int j=n-1;
        ans = max(ans, deg[i].first + deg[j].first - (isInSet(make_pair(deg[i].second, deg[j].second), edge_set) ? 1: 0));
    }
    ans = max(ans, deg[n-2].first + deg[n-3].first - (isInSet(make_pair(deg[n-2].second, deg[n-3].second), edge_set) ? 1: 0));
    cout<<(ans-1)<<'\n';
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Answer: 2
Problem: 2063C - Remove Exactly Two
Language: c++20 

Code 1:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main() {
    ll t;
    cin >> t;
    
    while (t--) {
        ll z;
        cin >> z;
        vector<set<ll>> tree(z + 1);
        
        for (ll i = 0; i < z - 1; i++) {
            ll x, y;
            cin >> x >> y;
            tree[x].insert(y);
            tree[y].insert(x);
        }

        ll connected = 0, idx = 1;
        
        // Finding the most connected node
        for (ll i = 1; i <= z; i++) {
            if (connected < (ll)tree[i].size()) {
                connected = tree[i].size();
                idx = i;
            }
        }

        ll ans = 0;
        ans += tree[idx].size();
        
        // Remove first selected node's edges
        for (auto x : tree[idx]) {
            tree[x].erase(idx);
        }
        tree[idx].clear();

        // Find the next most connected node
        connected = 0, idx = 1;
        for (ll i = 1; i <= z; i++) {
            if (connected < (ll)tree[i].size()) {
                connected = tree[i].size();
                idx = i;
            }
        }

        ans += tree[idx].size();
        cout << ans-1 << endl;
    }
    
    return 0;
}


Code 2:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main() {
	ll t;
	cin >> t;
	
	while (t--) {
		ll z;
		cin >> z;
		vector<set<ll>> tree(z + 1);
		
		for (ll i = 0; i < z - 1; i++) {
			ll x, y;
			cin >> x >> y;
			tree[x].insert(y);
			tree[y].insert(x);
		}
		vector<set<ll>> tree1=tree;
		ll connected = 0, idx = 1;
		
		// Finding the most connected node
		for (ll i = 1; i <= z; i++) {
			if (connected < (ll)tree[i].size()) {
				connected = tree[i].size();
				idx = i;
			}
		}

		ll ans = 1;
		ans += tree[idx].size()-1;
		// Remove first selected node's edges
		for (auto x : tree[idx]) {
			tree[x].erase(idx);
		}
		tree[idx].clear();

		// Find the next most connected node
		connected = 0, idx = 1;
		for (ll i = 1; i <= z; i++) {
			if (connected < (ll)tree[i].size()) {
				connected = tree[i].size();
				idx = i;
			}
		}

		ans += tree[idx].size()-1;
		
		
		 connected = 0;
		  idx = 1;
		
		// Finding the most connected node
		for (ll i = z; i >=1; i--) {
			if (connected < (ll)tree1[i].size()) {
				connected = tree1[i].size();
				idx = i;
			}
		}

		ll ans1 = 1;
		ans1 += tree1[idx].size()-1;
		// Remove first selected node's edges
		for (auto x : tree1[idx]) {
			tree1[x].erase(idx);
		}
		tree1[idx].clear();

		// Find the next most connected node
		connected = 0, idx = 1;
		for (ll i = 1; i <= z; i++) {
			if (connected < (ll)tree1[i].size()) {
				connected = tree1[i].size();
				idx = i;
			}
		}

		ans1 += tree1[idx].size()-1;
		cout <<max(ans,ans1)<< endl;
	}
	
	return 0;
}



Answer: 2