ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2061D - Kevin and Numbers
Language: c++17 

Solution 1 (runtime: 811 ms):
#include <bits/stdc++.h>
using namespace std;

//postmortem: break down: there's exactly one decomposition for each member of b. Put them into a stack and greedily break them down.
void solve(){
    int n, m;
    cin >> n >> m;

    int a;
    map<int, int> t;
    int nonzero_ct = 0;
    for(int i = 0; i < n; i++){
        cin >> a;
        if(t[a]++ == 0)nonzero_ct++;
    }
    stack<int> st;
    for(int i = 0; i < m; i++){
        cin >> a;
        st.push(a);
    }

    while(!st.empty()){
        int b = st.top();
        st.pop();
        if(t[b] >= 1){
            t[b]--;
            if(t[b] == 0)nonzero_ct--;
        }
        else if(b != 1){
            st.push(b/2);
            st.push((b+1)/2);
        }else{
            cout << "NO" << endl;
            return;
        }
    }

    cout << (nonzero_ct == 0 ? "YES" : "NO") << endl;
}

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

    while(t--){
        solve();
    }

    return 0;
}

Solution 2 (runtime: 203 ms):
/* بِسْمِ اللَّـهِ الرَّحْمَـٰنِ الرَّحِيم */
#include <bits/stdc++.h>
#include <cmath>

using namespace std;
#define int long long
#define ll long long
#define pp push_back
#define all(_x) _x.begin(), _x.end()
#define allr(_x) _x.rbegin(), _x.rend()
#define ULL unsigned ll
#define isOn(num, pos) ((num>>pos)&1LL)
#define gg std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
const int MAX = 1e6;

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
//char d[] = {'L', 'U', 'D', 'R'};




void solve() {
    int n, m; cin>>n>>m;
    priority_queue<int>a, b;
    for(int i=0;i<n;i++){
        int x; cin>>x;
        a.push(x);
    }
    for(int i=0;i<m;i++){
        int x; cin>>x;
        b.push(x);
    }

    while(!a.empty()){
        if(b.empty() || b.size() > a.size()){
            cout<<"NO\n";
            return ;
        }
        if(b.top() == a.top()){
            b.pop();
            a.pop();
        }
        else if(b.top() > a.top()){
            int x=b.top();
            b.pop();
            b.push(x/2);
            b.push((x+1)/2);
        }
        else {
            cout<<"NO\n";
            return ;
        }
    }

    if(b.size()){
        cout<<"NO\n";
        return ;
    }
    cout<<"YES\n";
}


signed main() {
    gg
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int t;
    t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 187 ms):

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
const int R=(1<<18);

bool po (pair<int,int> a , pair<int,int> b){
    if(a.first==b.first){
        return a.second>b.second;
    }
    return a.first<b.first;
}


int main(){
    
    int ilosc;
    cin>>ilosc;
    
    for(int i=1;i<=ilosc;i++){
        string a;
        cin>>a;
        for(int j=0;j<a.size();j++){
            vector<pair<int,int>>kolej;
            for(int k=0;j+k<a.size() and k<9;k++){
                kolej.push_back({a[j+k]-'0'-k,j+k});
            }
            sort(kolej.begin(),kolej.end(),po);
            pair<int,int>p=kolej.back();
            //cout<<p.first<<" --- "<<p.second<<endl;
            for(int k=p.second;k>j;k--){
                char h=a[k]-1;
                //cout<<h<<endl;
                a[k]=a[k-1];
                a[k-1]=h;
                //cout<<a<<endl;
            }
            
        }
        cout<<a<<endl;
    }
    
//    1
//    5891917899
    return 0;
}


Solution 2 (runtime: 77 ms):
#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;

void solve(){

string s; cin >> s;

for (int i = 0; i < s.size(); i++) {

int best = s[i] - '0', pos = i;

for (int j = i; j < min(i + 10, (int) s.size()); j++) {

if (s[j] - '0' - (j - i) > best) {

best = s[j] - '0' - (j - i);

pos = j;

}

}

while (pos > i) {

swap(s[pos], s[pos - 1]);

pos--;

}

s[i] = char(best + '0');

}

cout << s<<endl;

}

int main(){

int t;cin>>t;

while(t--){

solve();

}

}
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 93 ms):
#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }
    sort(v.begin(), v.end());
    vector<int> cnt = {1};
    for (int i = 1; i < n; i++) {
        if (v[i] == v[i - 1]) {
            cnt.back()++;
        } else {
            cnt.push_back(1);
        }
    }
    sort(cnt.begin(), cnt.end());
    int m = cnt.size();
    for (int i = 0; i < m - 1; i++) {
        if (cnt[i] > k) {
            cout << m - i << "\n";
            return;
        }
        k -= cnt[i];
    }
    cout << 1 << "\n";
}

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

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


Solution 2 (runtime: 93 ms):
// https://codeforces.com/problemset/problem/2057/B

#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;

int solve(vector<int>& arr, int n, int k){
    sort(arr.begin(), arr.end());
    std::vector<int> cnt = {1};

    for (int i = 1; i < n; i++) {
        if (arr[i] == arr[i - 1]) {
            cnt.back()++;
        } else {
            cnt.emplace_back(1);
        }
    }
    sort(cnt.begin(), cnt.end());

    int m = cnt.size();
    for (int i = 0; i < m - 1; i++) {
        if (cnt[i] > k) {
            return m - i;
        }
        k -= cnt[i];
    }

    return 1;
}

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

    int t;
    cin >> t;

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

        vector<int> arr(n);
        for (int i=0; i<n; i++) cin >> arr[i];

        cout << solve(arr, n, k) << endl;
    }
}
Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 186 ms):
#include<bits/stdc++.h>
using namespace std;
int T,n,l,r;
int a[100005],b[100005];
int main()
{
	cin>>T;
	while(T--)
	{
		scanf("%d%d%d",&n,&l,&r);
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			b[i]=a[i];
	    }
		sort(b+1,b+1+r);
		sort(a+l,a+n+1);
		long long s1=0,s2=0;
		for(int i=1;i<=r-l+1;i++) s1+=b[i];
		for(int i=l;i<=r;i++) s2+=a[i];
		printf("%lld\n",min(s1,s2));
	}
	return 0;
}

Solution 2 (runtime: 187 ms):
#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int l,r;
        cin>>l>>r;
        vector<int> prev,inter,post,t;
        for(int i=0;i<l-1;i++){
            int a;
            cin>>a;
            prev.push_back(a);
        }
        for(int i=l-1;i<r;i++){
            int a;
            cin>>a;
            inter.push_back(a);
        }
        for(int i=r;i<n;i++){
            int a;
            cin>>a;
            post.push_back(a);
        }
        long long s=0;
        sort(prev.begin(),prev.end(),greater<int>());
        sort(inter.begin(),inter.end(),greater<int>());
        sort(post.begin(),post.end(),greater<int>());
        t=inter;
        for(int i=0;i<r-l+1;i++){
            if(prev.empty() || inter.back()<=prev.back()){
                s+=inter.back();
                inter.pop_back();
            }else{
                s+=prev.back();
                prev.pop_back();
            }
        }
        long long mi=s;
        s=0;
        inter=t;
        for(int i=0;i<r-l+1;i++){
            if(post.empty() || inter.back()<=post.back()){
                s+=inter.back();
                inter.pop_back();
            }else{
                s+=post.back();
                post.pop_back();
            }
        }
        mi=min(s,mi);
        cout<<mi<<endl;
    }
    return 0;
}
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 61 ms):
#include <bits/stdc++.h>
#define ll long long
using namespace std;

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

Solution 2 (runtime: 46 ms):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<ll> a;
        for (int i = 0; i < n; i++)
        {
            int x;
            cin >> x;
            a.push_back(x);
        }
        ll maxi=0;
        for(int i=0;i<n;i++)
        maxi+=a[i];
        vector<ll>v;
        while(n>1)
        {
            ll sum=0;
            for(int i=0;i<n-1;i++)
            {
                v.push_back(a[i+1]-a[i]);
            }
            for(int i=0;i<n-1;i++)
            sum+=v[i];
            maxi=max(abs(sum),maxi);
            for(int i=0;i<n-1;i++)
            a[i]=v[i];
            v.clear();
            n--;
        }
        cout<<maxi<<endl;

    }
    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: c++17 

Solution 1 (runtime: 327 ms):
#include <iostream>
#include <vector>
#include <numeric>
#include <unordered_set>
#include <map>

void printarr(std::vector<int> &vec) {
    for (int i : vec) {
        std::cout << i << ' ';
    }
    std::cout << '\n';
    return;
}

int main() {
    int n; std::cin >> n; std::vector<int> vec(n); std::vector<bool> passed(n); for (int i = 0; i < n; i++) {
        std::cin >> vec[i];
    }

    std::vector<int> ans;

    std::map<int, int> cur; // car to index
    for (int i = 1; i <= n; i++) {
        cur[i] = i-1;
    }
    std::vector<int> curr(n); // index to car
    std::iota(curr.begin(), curr.end(), 1);

    for (int i = 0; i < n; i++) {
        int c = vec[i];
        for (int j = cur[c]+1; j < n; j++) {
            ans.push_back(curr[j]*(n+1)+curr[j-1]);
            std::swap(curr[j], curr[j-1]);
            cur[curr[j]] = j;
            cur[curr[j-1]] = j-1;
            // printarr(curr);
        }
        for (int j = n-1; j > i; j--) {
            ans.push_back(curr[j]*(n+1)+curr[j-1]);
            std::swap(curr[j], curr[j-1]);
            cur[curr[j]] = j;
            cur[curr[j-1]] = j-1;
            // printarr(curr);
        }
    }

    std::cout << ans.size() << '\n';
    for (int p : ans) {
        std::cout << p/(n+1) << ' ' << p%(n+1) << '\n';
    }
}


Solution 2 (runtime: 296 ms):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll num[1010],pos[1010],jiaohuan[2010100][2],ans[1010],f[1010];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>num[i];
		//pos[num[i]]=i;//pos[i]就是数字i最后在第几个 
		ans[i]=i;//ans[i]就是数字i现在在第几个 
		f[i]=i;//代表现在的数列 
	}
	ll cnt=0;
	for(int i=n;i>=1;i--){
		ll mudi=pos[num[i]];
		ll xian=ans[num[i]];
		for(int j=xian;j>=2;j--){
			cnt++;
			jiaohuan[cnt][0]=f[j];
			jiaohuan[cnt][1]=f[j-1];
			swap(f[j],f[j-1]);
			ans[f[j]]=j;
			ans[f[j-1]]=j-1;
		}
		for(int j=1;j<=mudi-1;j++){
			cnt++;
			jiaohuan[cnt][0]=f[j+1];
			jiaohuan[cnt][1]=f[j];
			swap(f[j],f[j+1]);
			ans[f[j]]=j;
			ans[f[j+1]]=j+1;
		}
	}
	cout<<cnt<<'\n';
	for(int i=1;i<=cnt;i++){
		cout<<jiaohuan[i][0]<<" "<<jiaohuan[i][1]<<'\n';
	}
	return 0;
}
Problem: 2049C - MEX Cycle
Language: c++17 

Solution 1 (runtime: 62 ms):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fastio std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

#define endl '\n'

const ll mod = 998244353;

const int N = 2e5+10;

int a[N];

int main() {

	fastio;

	int T;

	cin>>T;

	while(T--) {

		int n,x,y;

		cin>>n>>x>>y;

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

			if(i&1) a[i]=0;

			else a[i]=1;

		}

		if(n&1) a[n]=2;

		if(a[x]==a[y]) {

			a[y]=2;

			if(n&1) {

				if(y==n-2) a[n-1]=0,a[n]=1;

				else if(y==n-1) a[n]=1;

			}

		}



		for(int i=1; i<=n; i++) cout<<a[i]<<" ";

		cout<<endl;



	}

	return 0;

}



Solution 2 (runtime: 62 ms):
#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

#include <cstdio>

#include <set>

#include <map>

#include <queue>

using namespace std;

//#define int long long

//#include<bits/stdc++.h>

#define inf 0x3f3f3f3f

void solve()

{

	int n, x, y;

	cin >> n >> x >> y;

	if (n % 2 == 1)

	{

		int flag = 0;

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

		{

			if (i == y)

			{

				cout << 2 << " ";

			}

            else

            {

                cout<<flag<<' ';

                flag^=1;

            }

		}

		cout << '\n';

	}

	else

	{

	  int flag = 0;

      int cnt=x%2==y%2;

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

	  {

        if(i==y&&cnt)

        {

            cout<<2<<" ";

        }

        else

        {

            cout<<flag<<' ';

        }

        flag^=1;

	  }

	  cout << '\n';

	}

}



signed main()

{

	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	int t;

	cin >> t;

	while (t--)

	{

		solve();

	}

	return 0;

}
Problem: 2050B - Transfusion
Language: c++17 

Solution 1 (runtime: 109 ms):
#include <bits/stdc++.h>

using namespace std;

#define all(v) v.begin(), v.end()
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define ll long long
#define ld long double
#define fr(i, n) for (int i = 0; i < n; i++)
#define vi vector<ll>
#define pii pair<int, int>
#define mii map<int, int>
#define pb push_back
#define int long long
#define endl "\n"

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vi a(n);
        fr(i, n) cin >> a[i];
        int x = 0;
        int b = 0;
        int y = 0;
        int z = 0;
        for (int i = 0; i < n; i++)
        {
            if (i % 2 == 0)
            {
                x += a[i];
                y++;
            }
            else
            {
                b += a[i];
                z++;
            }
        }
        if (x / y == b / z && x % y == 0 && b % z == 0)
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }
    return 0;
}

Solution 2 (runtime: 359 ms):
#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n; cin >> n;
    vector<int> a(n);
    for (int &x : a) cin >> x;
    
    long long ods = 0, evs = 0;
    for (int i = 0; i < n; i++) {
        if (i & 1) ods += a[i];
        else evs += a[i];
    }
    int odc = n / 2, evc = n / 2;
    if (n & 1) evc++;

    if (ods % odc != 0 || evs % evc != 0 || ods / odc != evs / evc) {
        cout << "NO";
        return;
    }
    cout << "YES";
}

int main() {
    int TESTS; cin >> TESTS;
    while (TESTS --> 0) {
        solve();
        cout << '\n';
    }
    return 0;
}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 327 ms):
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool ascending(vector<int> &v, int n)
{
    for (int i = 0; i < n-1; i++)
    {
        if (v[i] > v[i + 1])
            return false;
    }
    return true;
}

bool descending(vector<int> &v, int n)
{
    for (int i = 0; i < n-1; i++)
    {
        if (v[i] < v[i + 1])
            return false;
    }
    return true;
}
int main()
{
    int t;
    cin >> t;

    int n;
    while (t--)
    {
        cin >> n;

        vector<int> v(n);

        for (int i = 0; i < n; i++)
        {
            cin >> v[i];
        }

        if (ascending(v, n))
            cout << "YES" << endl;

      
        else
        {

            int a;
            for (int i = 0; i < n - 1; i++)
            {
                a = min(v[i], v[i + 1]);

                v[i] -= a;
                v[i + 1] -= a;
            }

            if (ascending(v, n))
                cout << "YES" << endl;

            else
                cout << "NO" << endl;
        }
    }

    return 0;
}

Solution 2 (runtime: 327 ms):
#include<iostream>

#include<algorithm>

#include<queue>

#include<vector>

typedef long long ll;

using namespace std;

const int N = 2e5+10;



int t;

int n;

ll a[N];

int main() {

cin >> t;

while (t--) {

cin >> n;

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

cin >> a[i];

}

int f = 1;

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

if (a[i] <= a[i + 1]) {

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

a[i] = 0;

}

}

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

if (a[i] > a[i + 1]) {

f = 0;

break;

}

}

if (f)cout << "YES";

else cout << "NO";

cout << '\n';

}



return 0;

}
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 265 ms):
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,k,a[100005];
vector<int>v;
map<int,int>m;
signed main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>k;
		int ans=0;
		m.clear();
		v.clear();
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			if(!m[a[i]])
			{
				v.push_back(a[i]);
			}
			m[a[i]]++;
		}
		vector<int>v2;
		for(auto x:v)
		{
			v2.push_back(m[x]);
		}
		sort(v2.begin(),v2.end());
		for(auto x:v2)
		{
			if(k>=x)
			{
				k-=x;
			}
			else
			{
				ans++;
			}
		}
		cout<<max(1ll,ans)<<endl;
	}	
	return 0;
}


Solution 2 (runtime: 124 ms):
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n, k) for (int i = k; i < n + k; i++)
#define all(v) v.begin(), v.end()
#define space << " " 
#define int long long
#define M (998244353) // 1000000007
#define take(n) ll n; cin >> n;
#define takean(a, n, k) ll a[n + k] ={}; forn(i, n, k) cin >> a[i];
#define yes cout << "YES" << endl; return;
#define no cout << "NO" << endl; return;
#define ll long long
#define vi vector<long long int>
#define vvi vector<vector<long long int>>
bool isPrime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
void solve()
{
 take(n);
 take(k);
 vector<int>a(n);
 map<int,int>mpp;
 for(int i=0;i<n;++i){
    cin>>a[i];
    mpp[a[i]]++;
 }
 vector<int>v;
 for(auto i:mpp){
    v.push_back(i.second);
 }
 sort(v.begin(),v.end());
//  for(auto i:v){
//     cout<<i<<' ';
//  }
//  cout<<endl;
 int m=1;
 for(int i=0;i<v.size()-1;++i){
    if(k==0){
        m=v.size()-i;
        break;
    }
    if(k>=v[i]){
        k=k-v[i];
    }
    else{
        m=v.size()-i;
        break;
    }

 }
 cout<<m<<endl;


//  for(auto i:mpp){
//     cout<<i.first<<' '<<i.second<<endl;
//  }


}
signed main()
{
int tc = 1;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> tc;
while (tc-- > 0)
{
solve();
}
return 0;
}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 93 ms):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define nl "\n"

#ifndef ONLINE_JUDGE
#include "debug_template.h"
#else
#define debug(...)
#define debugArr(...)
#endif

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

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

    ll cnt= 0;
    for(int i=2; i<n; i++) {
      if(a[i-1]>=a[i-2] && a[i-1]-a[i-2]<=a[i]) {
        cnt++;
        a[i-1]-=a[i-2];
        a[i-2]=0;
      }
    }
    cout<<(is_sorted(a,a+n)? "YES\n":"No\n");

    debugArr(a,n);

  }
  return 0;
}

Solution 2 (runtime: 124 ms):
#include <bits/stdc++.h>
using namespace std;

#define fast_io()                     \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define int long long
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define endl '\n'

const int MOD = 1e9 + 7;
const int INF = LLONG_MAX;

void solve()
{
    int n;
    cin >> n;
    vector<int> v;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        v.pb(x);
    }
    for (int i = 0; i < n - 1; i++)
    {
        if (v[i] > v[i + 1])
        {
            cout << "NO" << endl;
            return;
        }
        else
        {
            int mini = min(v[i], v[i + 1]);
            v[i] -= mini;
            v[i + 1] -= mini;
        }
    }
    is_sorted(v.begin(), v.end()) ? cout << "YES" << endl : cout << "NO" << endl;
}

int32_t main()
{
    fast_io();
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 1577 ms):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<long long> vll;
#define reg(i,a,b) for(ll i=a;i<b;i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

ll s(set<pair<ll,ll>> &cont, ll node, map<ll, ll> &f, vector<vi> &adjL){
    ll ans = 0;
    ll fre = f[node];
    ans = fre;
    cont.erase({fre, node});
    for(auto nxt: adjL[node]){
        cont.erase({f[nxt], nxt});
        f[nxt]--;
        cont.insert({f[nxt], nxt});
    }

    ans += (*cont.rbegin()).first;
    ans--;
    for(auto nxt: adjL[node]){
        cont.erase({f[nxt], nxt});
        f[nxt]++;
        cont.insert({f[nxt], nxt});
    }
    return ans;
}

void solve() {
    ll n;
    cin>>n;
    vector<vi> adjL(n);
    map<ll, ll> f;

    reg(i,0,n - 1){
        ll u, v;
        cin>>u>>v;
        u--;
        v--;
        adjL[u].push_back(v);
        adjL[v].push_back(u);
        f[u]++;
        f[v]++;
    }
    set<pair<ll,ll>> cont;
    for(auto it : f) cont.insert({it.second, it.first});
    //ll node = (*cont.rbegin()).second;
    ll fre = (*cont.rbegin()).first;
    // cout<<fre<<endl;
    ll ans = -1;
    vector<pair<ll, ll>> temp(cont.begin(), cont.end());
    for(ll i = temp.size() - 1; i>=0; i--){
        if(temp[i].first < fre) break;
        // cout<<"chala"<<endl;
        // cout<<s(cont, temp[i].second, f, adjL)<<endl;
        ans = max(ans,s(cont, temp[i].second, f, adjL));
    }

    cout<<ans<<endl;
    return;
}

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


Solution 2 (runtime: 796 ms):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
/*
删除度数(图的度)最大的点，然后再删剩下的最大的
相同度数的删除顺序可能对答案有影响

情况1：最大次大 不相邻 d[mx1]+d[mx2]-1
情况2：最大次大 只能相邻 d[mx1]+d[mx2]-2;
情况2的形成条件： 
	1.最大、次大相同，且只有两个相同且相邻
	2.最大、次大不同，且次大都与最大相邻

*/
int n;
map<pii,bool> mp;
struct Node{
	int id,d;
	bool operator<(const Node&_) const{
		return d > _.d;
	}
}node[200005];
int u,v;
void solve(){
	cin>>n;
	for(int i = 1;i <= n;i++){
		node[i].d = 0;
		node[i].id = i;
	}
	mp.clear();
	for(int i = 1;i <= n-1;i++){
		cin>>u>>v;
		node[u].d++;
		node[v].d++;
		mp[pii(u,v)] = 1;
		mp[pii(v,u)] = 1;
	}
	sort(node+1,node+n+1);
	int ans = node[1].d+node[2].d-1;
	if(node[1].d == node[2].d){
		if(n==2 || node[3].d != node[2].d && mp[pii(node[1].id,node[2].id)] ) ans--;
	}
	else if(node[1].d != node[2].d){
		bool flag = 0;
		for(int i = 2;i <= n;i++){
			if(node[i].d != node[2].d) break;
			if(!mp.count(pii(node[1].id,node[i].id))) flag = 1;
		}
		if(!flag) ans--;
	}
	cout<<ans<<"\n";
}
int main(){
	int t;
	cin>>t;
	while(t--){
		solve();
	}
	
	return 0;
}
/*
1
5
1 2
1 3
2 4
3 5


*/