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

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

using namespace std;

long long MOD = 998244353;

void solve(){
    int n,m;
    cin >> n >> m;
    vector<int> arr1(n), arr2(m);
    multiset<int,greater<>> ms1, ms2;
    for(int i=0; i<n; i++){
        cin >> arr1[i];
        ms1.insert(arr1[i]);
    }
    for(int i=0; i<m; i++){
        cin >> arr2[i];
        ms2.insert(arr2[i]);
    }
    while(ms2.size() && ms1.size() && ms2.size() <= n){
        if(*ms2.begin() == (*ms1.begin())){
            ms2.erase(ms2.begin());
            ms1.erase(ms1.begin());
        }else{
            int c = *ms2.begin(); ms2.erase(ms2.begin());
            ms2.insert(c/2);
            if(c%2 == 1) ms2.insert(c/2+1);
            else ms2.insert(c/2);
        }
    }

    if(ms2.size() == n){
        while(ms2.size() && ms1.size()){
            if(*ms2.begin() != *ms1.begin()){
                cout << "No\n"; return;
            }
            ms2.erase(ms2.begin());
            ms1.erase(ms1.begin());
        }
    }
    if(ms1.size()) cout << "No\n";
    else cout << "Yes\n";
    

    
}

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

Solution 2 (runtime: 640 ms):
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
using ll=long long;
void solve(){
    int n,m;
    cin>>n>>m;
    priority_queue<int>q1;
    priority_queue<int>q2;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        q1.push(x);
    }
    for(int i=1;i<=m;i++){
        int x;
        cin>>x;
        q2.push(x);
    }
    //分解b
    while(!q1.empty()&&!q2.empty()){
        while(!q1.empty()&&!q2.empty()&&q1.top()==q2.top()){
            q1.pop();
            q2.pop();
        }
        if(q1.empty()||q2.empty())break;
        if(q1.top()>q2.top())break;
        if(q2.size()>=q1.size())break;
        //分解
        int t=q2.top();
        int a=t/2,b=t-a;
        q2.pop();
        //入队
        q2.push(a);
        q2.push(b);
    }
    if(q1.empty()&&q2.empty())cout<<"Yes"<<'\n';
    else cout<<"No"<<'\n';
}
int main(){
    int _;
    cin>>_;
    while(_--)solve();
    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 608 ms):
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define forn(i, a, b) for(int i=a;i<(b);i++)
#define rforn(i, b, a) for(int i=b;i>=a;i--)
#define dbg(x) cout<<#x<<" = "<<x<<'\n'
#define dbgv(x) for(int i=0;i<x.size();i++)cout<<x[i]<<' ';cout<<'\n'
#define inf 1000000005
#define mod 1000000007
#define fir first
#define sec second

vector<vector<int>> adj;
vector<int> indeg;

void solve() {
    int n; cin >> n;
    adj.assign(n, vector<int>());
    indeg.assign(n, 0);

    forn(i, 0, n - 1) {
        int u, v; cin >> u >> v;
        u--; v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
        indeg[u]++; indeg[v]++;
    }

    int ans = 0;
    forn(cur, 0, n) {
        for (int ch: adj[cur]) {
            ans = max(ans, indeg[cur] + indeg[ch] - 2);
        }
    }

    multiset<int> se(indeg.begin(), indeg.end());
    forn(cur, 0, n) {
        se.erase(se.find(indeg[cur]));
        for (int ch: adj[cur]) se.erase(se.find(indeg[ch]));
        if (!se.empty()) ans = max(ans, indeg[cur] + *se.rbegin() - 1);
        for (int ch: adj[cur]) se.insert(indeg[ch]);
        se.insert(indeg[cur]);
    }
    cout << ans << '\n';
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    ll t;
    cin >> t;
    while (t--) {
        solve();
    }
}

Solution 2 (runtime: 531 ms):
#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int kkksc03=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while(isdigit(ch)){kkksc03=(kkksc03<<1)+(kkksc03<<3)+(ch^48);ch=getchar();}
	return kkksc03*f;
}
inline void write(int kkk){
	if(kkk<0) kkk=-kkk,putchar('-');
	int cnt=0;char cc[20];
	do{cc[++cnt]=48+kkk%10;kkk/=10;}while(kkk);
	while(cnt) putchar(cc[--cnt+1]);
	putchar('\n');
	return;
}
int t,n,ans=0,du[200010];
vector<int> v[200010],qwq;
map<pair<int,int>,bool> mp;
signed main(){
	t=read();
	while(t--){
		n=read();ans=0;
		for(int i=1,a,b;i<n;i++){
			a=read();b=read();
			mp[{a,b}]=1;mp[{b,a}]=1;
			++du[a];++du[b];
			v[a].push_back(b);
			v[b].push_back(a);
		}
		if(n==2){
			puts("0");
			qwq.clear();mp.clear();
			for(int i=1;i<=n;i++) du[i]=0,v[i].clear();
			continue;
		}
		int mx=0;
		for(int i=1;i<=n;i++) mx=max(mx,du[i]);
		for(int i=1;i<=n;i++) if(mx==du[i]) qwq.push_back(i);
		if(qwq.size()==1){
			mx=0;
			for(int i=1;i<=n;i++) if(i!=qwq.back()) mx=max(mx,du[i]);
			for(int i=1;i<=n;i++) if(mx==du[i]) ans=max(ans,du[qwq.back()]+du[i]-1-mp[{i,qwq.back()}]);
		}else{
			ans=mx*2-2;
			bool p=0;
			for(int i=0;i<qwq.size();i++){
				for(int j=i+1;j<qwq.size();j++){
					if(!mp[{qwq[i],qwq[j]}]){
						p=1;
						++ans;
						break;
					}
				}
				if(p) break;
			}
		}
		write(ans);
		qwq.clear();mp.clear();
		for(int i=1;i<=n;i++) du[i]=0,v[i].clear();
	}
	return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 139 ms):
#include<bits/stdc++.h>
using namespace std;
#define fr(ii,ss,tt) for(int ii=ss;ii<=tt;ii++)
#define rf(ii,ss,tt) for(int ii=ss;ii>=tt;ii--)
#define pii pair<int,int>
#define pt printf
#define x first
#define y second
#define all(a) begin(a),end(a)
#define pb push_back
#define eb emplace_back
#define siz(x) ((int)size(x))
#ifdef UFILE
#define file(f1,f2) (f1!=""&&freopen(f1,"r",stdin),f2!=""&&freopen(f2,"w",stdout))
#else
#define file(...) 0
#endif
#ifdef DEBUG
#include DEBUG
#else
#define assert(...) 0
#define Z(...) 0
#define ZZ(...) 0
#endif
typedef long long LL;

template<typename T>bool tomax(T&x,const T&y){if(x<y)return x=y,true;return false;}
template<typename T>bool tomin(T&x,const T&y){if(x>y)return x=y,true;return false;}
inline void rd(auto&x,auto&...y){char ch;bool f=0;while(!isdigit(ch=getchar()))if(ch=='-')f=1;x=0;do{x=(x<<1)+(x<<3)+(ch^48);}while(isdigit(ch=getchar()));if(f)x=-x;if constexpr(sizeof...(y))rd(y...);}
inline void wtl(auto&x,auto&...y){cout<<x;if constexpr(sizeof...(y))cout<<' ',wt(y...);else cout<<'\n';}

const int N=1e6+33;
int n,k,a[N];
signed main(){
	ios::sync_with_stdio(0);
	int T;
	rd(T);
	while(T--){
		rd(n,k);
		
		map<int,int>mp;
		fr(i,1,n){
			rd(a[i]);
			mp[a[i]]++;
		}if(k==n){
			pt("1\n");continue;
		}
		priority_queue<pii,vector<pii>,greater<pii>>q;
		for(auto[v,c]:mp){
			q.push({c,v});
		}
		while(1){
			auto[c,v]=q.top();
			if(c>k)break;
			q.pop();
			k-=c;
		}
		pt("%d\n",q.size());
	}
	return 0;
}

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

const int N = 1e5 + 10;
map<int, int> mp;
int cnt[N];

void solve() {
	mp.clear();
	
	int n, k;
	cin >> n >> k; 

	for (int i = 0; i < n; i++) {
		int t;
		cin >> t;
		mp[t]++;
	}
	
	int cnt_n = 0;
	for (pair<int, int> p : mp) {
		cnt[cnt_n++] = p.second;
	}
	
	sort(cnt, cnt + cnt_n);
	int ans = mp.size();
	for (int i = 0; i < n; i++) {
		k -= cnt[i];
		if (k >= 0) {
			ans--;
		} else {
			break;
		}
	}
	if (ans <= 0) {
		ans = 1;
	}
	cout << ans << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int tt;
	cin >> tt;
	while (tt--) {
		solve();
	}
	
	return 0;
}
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 796 ms):
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#define int long long
using namespace std;
int mod=998244353;
int INF=-1e10;
void dfs(int u,int p,auto &adj,auto &dp,auto &dgr){
    int m1=0;
    int m2=0;
    for(auto v:adj[u]){
        if(v==p) continue;
        dfs(v,u,adj,dp,dgr);
        if(m1<=max(dp[v][1][1],dp[v][1][0])){
            m2=m1;
            m1=max(dp[v][1][1],dp[v][1][0]);
        }
        else if(m2<max(dp[v][1][1],dp[v][1][0])){
            m2=max(dp[v][1][1],dp[v][1][0]);
        }
        dp[u][1][0]=max({dp[u][1][0],dp[v][1][1],dp[v][1][0]});
        dp[u][2][0]=max({dp[u][2][0],dp[v][2][0],dp[v][2][1]});
        dp[u][1][1]=dgr[u];
        dp[u][2][1]=max({dp[u][2][1],dp[v][1][1]+dgr[u]-2,dp[v][1][0]+dgr[u]-1});
    }
    dp[u][2][0]=max(dp[u][2][0],m1+m2-1);
    if(dgr[u]==1){
        dp[u][1][1] = 1;
    }
}
void solve(){
    int n;
    cin>>n;
    vector<vector<int>> adj(n);
    vector<vector<vector<int>>> dp(n,vector<vector<int>>(3,vector<int>(2,0)));
    vector<int> dgr(n,0);
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        u--;v--;
        dgr[u]++;
        dgr[v]++;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(0,-1,adj,dp,dgr);
    cout<<max(dp[0][2][0],dp[0][2][1])<<endl;
}
int32_t main(){
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
}


Solution 2 (runtime: 1250 ms):
#ifdef DEBUG
constexpr bool debug = true;
#else
constexpr bool debug = false;
#endif
#include <bits/stdc++.h>

using namespace std;

int main() {
  int tc;
  cin >> tc;
  vector<vector<int>> adj;
  // vector<int> deg;
  set<pair<int, int>, std::greater<pair<int, int>>> seq;
  while (tc--) {
    int n;
    cin >> n;
    adj.reserve(n);
    // deg.reserve(n);
    // deg.assign(n, 0);
    adj.assign(n, vector<int>{});
    for (int i = 0; i < n-1; i++) {
      int u, v;
      cin >> u >> v;
      u -= 1;
      v -= 1;
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    for (int i = 0; i < n; i++) {
      seq.insert({adj[i].size(), i});
    }
    int max_comps = 0;
    for (int i = 0; i < n; i++) {
      seq.erase(seq.find({adj[i].size(), i}));
      for (auto j : adj[i]) {
        if constexpr(debug) {
          printf("For %d: Deleting %d with deg %d\n", i, j, adj[j].size());
        }
        seq.erase(seq.find({adj[j].size(), j}));
        seq.insert({adj[j].size()-1, j});
      }
      max_comps = max(max_comps, int(adj[i].size() + seq.begin()->first - 1));
      for (auto j : adj[i]) {
        seq.erase(seq.find({adj[j].size()-1, j}));
        seq.insert({adj[j].size(), j});
      }
      seq.insert({adj[i].size(), i});
    }
    printf("%d\n", max_comps);
    adj.clear();
    seq.clear();
  }
}
Problem: 2050D - Digital string maximization
Language: c++17 

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

using namespace std;

void solve()
{

    string s;
    cin >> s;
    int nn = s.size();
    int n = s.size();

    for (int i = 0; i < s.size(); i++)
    {
        int maxi_ind = -1;
        int maxi = s[i] - '0';
        for (int j = i + 1; j < min(i + 9, n); j++)
        {
            if (s[j] - '0' - (j - i) > maxi)
            {
                maxi = s[j] - '0' - (j - i);
                maxi_ind = j;
            }
        }

        while (maxi_ind > i)
        {
            swap(s[maxi_ind], s[maxi_ind - 1]);
            maxi_ind--;
        }
        s[i] = maxi + '0';
    }

    cout << s << endl;
}

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

    return 0;
}

Solution 2 (runtime: 46 ms):
//trying :-}





//      (????????????
//       ~  ?? ~





#include <iostream>

#include <string>

#include <vector>

#include <set>

#include <unordered_set>

#include <algorithm>

#include <numeric>

#include <cmath>

#include <map>

using namespace std;

using ll = long long;



#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);

#define f(i, a, b) for (int i = a; i < b; ++i)

#define pb push_back





void solve() {

string a;

cin>>a;

int n=a.size();







f(i,0,n){

int k=i;

f(j,i,min(n,i+10)){

if(a[k]-k<a[j]-j){

k=j;

}

}

while(k>i){

a[k]-=1;

char t=a[k];

a[k]=a[k-1];

a[k-1]=t;

k--;

}

}

f(i,0,n) cout<<a[i];

cout<<"\n";





}



int main() {

fastio;

int t;

cin >> t;

while (t--) solve();



return 0;

}





//  :-
Problem: 2050E - Three Strings
Language: c++17 

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

template <typename T>
bool cmin(T &a, const T &b)
{
    return b < a ? a = b, 1 : 0;
}

template <typename T>
bool cmax(T &a, const T &b)
{
    return b > a ? a = b, 1 : 0;
}

void go()
{
    string a, b, c;
    cin >> a >> b >> c;
    int n = a.size();
    int m = b.size();
    vector<vector<int>> f(n + 1, vector<int>(m + 1, -1));
    f[0][0] = 0;
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= m; j++)
        {
            if (f[i][j] == -1)
            {
                continue;
            }
            if (i + 1 <= n)
            {
                cmax(f[i + 1][j], f[i][j] + (a[i] == c[i + j] ? 1 : 0));
            }
            if (j + 1 <= m)
            {
                cmax(f[i][j + 1], f[i][j] + (b[j] == c[i + j] ? 1 : 0));
            }
        }
    }
    cout << n + m - f[n][m] << endl;
}

int main()
{
    int caseCnt;
    cin >> caseCnt;
    for (int i = 1; i <= caseCnt; i++)
        go();
    return 0;
}

Solution 2 (runtime: 936 ms):
#include "bits/stdc++.h"
using namespace std;
#define int long long int

//~~~~~~~LAVISH SINGHAL~~~~~~~//

int dp[1005][1005];

int cost(string &a, string &b, string &c, int i, int j, int k)
{
    int ans = 1e10;
    if (i == a.size() && j == b.size())
    {
        return 0;
    }
    if (dp[i][j] != -1)
    {
        return dp[i][j];
    }
    if (i < a.size())
    {
        ans = min(ans, cost(a, b, c, i + 1, j, k + 1) + (a[i] != c[k]));
    }
    if (j < b.size())
    {
        ans = min(ans, cost(a, b, c, i, j + 1, k + 1) + (b[j] != c[k]));
    }
    return dp[i][j] = ans;
}

void solve()
{
    string a, b, c;
    cin >> a >> b >> c;
    for (int i = 0; i < 1005; i++)
    {
        for (int j = 0; j < 1005; j++)
        {
            dp[i][j] = -1;
        }
    }
    cout << cost(a, b, c, 0, 0, 0) << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    // #ifndef ONLINE_JUDGE
    // freopen("input1.txt", "r", stdin);
    // freopen("output1.txt", "w", stdout);
    // #endif
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
Problem: 2060D - Subtract Min Sort
Language: c++17 

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

#define endl "\n"
#define int long long


void solve() {
	
	int n;
	cin >> n;

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

	if(v[0] > v[1]){
		cout << "No" << endl;
		return;
	}

	int sub = v[1] - v[0];
	for(int i=2; i<n; i++){
		if(v[i] < sub){
			cout << "No" << endl;
			return;
		}

		sub = v[i] - sub;
	}

	cout << "Yes" << endl;
	return;
}


int32_t main() {

	ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);

	int t = 1;
	cin >> t;

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

	return 0;
}

Solution 2 (runtime: 327 ms):
#include<bits/stdc++.h>
using namespace std;
int main() {
    int tt;
    cin >> tt;
    while(tt--) {
        int n;
        cin >> n;
        vector<int> arr(n);
        for(auto &i: arr) cin >> i;
        if(n == 1) {
            cout << "YES" << endl;
            continue;
        }
        if(arr[0] > arr[1]) {
            cout << "NO" << endl;
            continue;
        }
        int f = 1;
        for(int i = 1; i < n; i++) {
            if(arr[i] < arr[i - 1]) {
                if(arr[i - 1] - arr[i - 2] > arr[i]) {
                    f = 0;
                    break;
                } else {
                    arr[i - 1] -= arr[i - 2];
                    arr[i - 2] = 0;
                }
            } else {
                arr[i] -= arr[i - 1];
                arr[i - 1] = 0;
            }
        }
        if(f) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
}
Problem: 2052A - Adrenaline Rush
Language: c++17 

Solution 1 (runtime: 1218 ms):
//RADHE RADHE

#include <bits/stdc++.h>

using namespace std;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long

int32_t main() {
    IOS
    int n;
    cin>>n;

    vector<int>v1(n);
    for(int i=0;i<n;i++)cin>>v1[i];
    vector<array<int,2>>v;
    for(int i=n-1;i>=0;i--){
        vector<int>v2;
        for(int j=i-1;j>=0;j--){
            if(v1[j]<v1[i])v2.push_back(v1[j]);
        }
        sort(v2.begin(),v2.end());
        for(int j=(int)v2.size()-1;j>=0;j--){
            v.push_back({v1[i],v2[j]});
        }
        for(int j=0;j<v2.size();j++){
            v.push_back({v2[j],v1[i]});
        }
        v2.clear();
        for(int j=0;j<i;j++){
            if(v1[j]>v1[i])v2.push_back(v1[j]);
        }
        sort(v2.begin(),v2.end());

        for(int j=0;j<v2.size();j++){
            v.push_back({v2[j],v1[i]});
        }

    }
    cout<<v.size()<<endl;
    for(auto x:v)cout<<x[0]<<" "<<x[1]<<endl;
}



Solution 2 (runtime: 874 ms):
#include <bits/stdc++.h>
#define int long long

// macros
#define For(x) for (int i = 0; i < x; i++)
#define SORT(a) sort(a.begin(), a.end())
#define FOR(a, b, c) for(int i = a; i < b; i += c)
#define SUM(a) accumulate(a.begin(), a.end(), 0LL)
#define MAX(a) *max_element(a.begin(), a.end())
#define all(x) (x.begin(), x.end())

// syntax
#define mp make_pair

// other macros
#define MOD (int)1e8

// speed
#pragma GCC optimize("O3")

using namespace std;

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int count;
    cin >> count;
    vector<int> arr(count);
    vector<int> temp(count);
    For (count){
        cin >> arr[i];
    }
    int counter = 0;
    for (int i = 0; i < count; i++){
        for (int j = i + 1; j < count; j++){
            if (arr[i] > arr[j]){
                counter++;
            } else {
                counter += 2;
            }
        }
    }
    cout << counter << "\n";
    for (int i = 0; i < count; i++){
        for (int j = i + 1; j < count; j++){
            cout << j + 1 << " " << i + 1 << "\n";
        }
        temp[i] = count - i;
    }
    while (temp != arr){
        for (int i = 0; i < count - 1; i++){
            int index1 = find(arr.begin(), arr.end(), temp[i]) - arr.begin();
            int index2 = find(arr.begin(), arr.end(), temp[i+1]) - arr.begin();
            if (index1 > index2){
                cout << temp[i + 1] << ' ' << temp[i] << "\n";
                auto v = temp[i];
                temp[i] = temp[i + 1];
                temp[i + 1] = v;
            }
        }
    }
    // 4 3 2 1
    // 3 4 1 2
    // 3 4 1 2
    // 3 1 4 2
}
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 249 ms):
#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cassert>
#include <cstring>
#define int long long
using namespace std;

const int maxn = 3e5 + 1, mod = 998244353;
vector<int> nodes[maxn];
int fa[maxn];

struct Deg
{
	int deg;
	int id;
} deg[maxn];

void find_fa(int now, int pre)
{
	fa[now] = pre;
	for (auto to : nodes[now]) {
		if (to == pre) continue;
		find_fa(to, now);
	}
}

int is_adjacent(int u, int v)
{
	return (fa[u] == v) || (fa[v] == u);
}

int cmp(Deg a, Deg b)
{
	return a.deg > b.deg;
}

void solve()
{
	int n;
	cin >> n;
	for (int i = 0; i <= n; i++) nodes[i].clear();
	for (int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v;
		nodes[u].push_back(v);
		nodes[v].push_back(u);
	}
	if (n == 2) {
		cout << 0 << endl;
		return ;
	} else if (n == 3) {
		cout << 1 << endl;
		return ;
	}
	find_fa(1, -1);
	for (int i = 1; i <= n; i++) {
		deg[i - 1].id = i;
		deg[i - 1].deg = nodes[i].size();
	}
	sort(deg, deg + n, cmp);
	int ans = deg[0].deg + deg[1].deg - is_adjacent(deg[0].id, deg[1].id);
	for (int i = 2; i < n; i++)
		ans = max(ans, max(deg[0].deg + deg[i].deg - is_adjacent(deg[0].id, deg[i].id), deg[1].deg + deg[i].deg - is_adjacent(deg[1].id, deg[i].id)));
	cout << ans - 1 << endl;
}

signed main()
{
//	freopen("code.in", "r", stdin);
	ios_base::sync_with_stdio(false);

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

	return 0;
}


Solution 2 (runtime: 546 ms):
#include "bits/stdc++.h"
#define int long long
#define uint unsigned long long
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define pi pair<int, int>
#define vpi vector<pi>
#define vvpi vector<vpi>
#define loop(i, a, n) for(int i=(a); i<(n); i++)
#define all(v) v.begin(),v.end()
#define sortN(v) sort(v.begin(),v.end())
#define sortR(v) sort(v.rbegin(),v.rend())
#define maxE(v) *max_element(v.begin(),v.end())
#define minE(v) *min_element(v.begin(),v.end())
#define INF (int)(1e9+7)

using namespace std;

template<typename T>
void solve() {
    int n;
    cin>>n;
    vi deg(n+1, 0);
    set<pi> edge;
    loop(i, 1, n){
        int u, v;
        cin>>u>>v;
        deg[u]++;
        deg[v]++;
        edge.insert({v, u});
        edge.insert({u, v});
    }
    vpi pre;
    loop(i, 1, n+1){
        pre.push_back({deg[i], i});
    }
    sortR(pre);
    int ans = 0;
    loop(i, 0, pre.size()){
        loop(j, i+1, pre.size()){
            if(edge.find({pre[i].second, pre[j].second})==edge.end()){
                ans = max(ans, pre[i].first+pre[j].first-1);
                break;
            }
            else{
                ans = max(ans, pre[i].first+pre[j].first-2);
            }
        }
    }
    cout<<ans<<endl;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int T;
    cin >> T;
    while (T--) {
        solve<int>();   // Specify the type (default to 'int')
    }
    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: c++17 

Solution 1 (runtime: 2046 ms):
#include <iostream>
#include<vector>
#include<string>
#include<map>
#include<unordered_map>
#include<algorithm>
#include<cmath>
#include<numeric>
#include<set>
using namespace std;

void solve(){
    int n;
    cin>>n;
    int c[n];
    for(int i=0;i<n;i++){
        cin>>c[i];
    }
    int ans = 0;
    set< pair<int,int> > overtakes;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(c[i]<c[j]){ans = ans + 2;overtakes.insert(make_pair(c[i],c[j]));overtakes.insert(make_pair(c[j],c[i]));}
            else{ans++;overtakes.insert(make_pair(c[i],c[j]));}
        }
    }
    cout<<ans<<endl;
    int a[n];
    for(int i=0;i<n;i++){
        a[i]=i+1;
    }
    while(!overtakes.empty()){
        for(int i=0;i<n-1;i++){
            if(overtakes.find(make_pair(a[i+1],a[i])) != overtakes.end()){
                int temp = a[i];
                a[i]=a[i+1];a[i+1]=temp;overtakes.erase(make_pair(a[i],a[i+1]));
                cout<<a[i]<<" "<<a[i+1]<<endl;
            }
        }
    }

    return;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    solve();
    return 0;
}



Solution 2 (runtime: 1265 ms):
#include <stdio.h>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
	long long n, i, j;

	cin >> n;

	long long* c = new long long[n];

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

	long long num_inverse = 0;

	for (i = 0; i < n; i++)
	{
		for (j = i; j < n; j++)
		{
			if (c[i] > c[j])
			{
				num_inverse++;
			}
		}
	}
	
	long long num_pass = n * (n - 1) - num_inverse;

	vector<long long> arr;

	long long* pos_arr = new long long[n + 1];

	for (i = 0; i < n; i++)
	{
		arr.push_back(i + 1);

		pos_arr[i + 1] = i;
	}

	cout << num_pass << endl;

	for (i = 1; i < n + 1; i++)
	{
		long long pos = pos_arr[c[i - 1]];

		for (j = pos + 1; j < n + 1 - i; j++)
		{
			cout << arr[j] << " " << c[i - 1] << endl;
		}

		for (j = n - i; j >= 0; j--)
		{
			if (j != pos)
			{
				cout << c[i - 1] << " " << arr[j] << endl;
			}
			
			if (j > pos)
			{
				pos_arr[arr[j]]--;
			}
		}

		arr.erase(arr.begin() + pos);
	}

	return 0;
}