ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063C - Remove Exactly Two
Language: c++20 

Code 1:
#include <iostream>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define x first
#define y second
#define int long long

const int MAXN = 2e5 + 100;
vector<int> g[MAXN];

int power[MAXN];
int used[MAXN];

void solve() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        g[i].clear();
        power[i] = 0;
    }
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    set<pair<int, int>> s;
    for (int i = 0; i < n; i++) {
        power[i] = g[i].size();
        s.insert({power[i], i});
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int u : g[i]) {
            s.erase({-power[u], u});
            power[u]--;
            s.insert({-power[u], u});
        }
        s.erase({-power[i], i});
        ans = max(ans, power[i] + -(s.begin()->x) - 1);
        s.insert({power[i], i});
        for (int u : g[i]) {
            s.erase({-power[u], u});
            power[u]++;
            s.insert({-power[u], u});
        }
    }
    cout << ans << '\n';
}

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

Code 2:
#include <iostream>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define x first
#define y second
#define int long long

const int MAXN = 2e5 + 100;
vector<int> g[MAXN];

int power[MAXN];
int used[MAXN];

void solve() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        g[i].clear();
        power[i] = 0;
    }
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    set<pair<int, int>> s;
    for (int i = 0; i < n; i++) {
        power[i] = g[i].size();
        s.insert({power[i], i});
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int u : g[i]) {
            s.erase({-power[u], u});
            power[u]--;
            s.insert({-power[u], u});
        }
        s.erase({-power[i], i});
        ans = max(ans, power[i] + -(s.begin()->x) - 1);
        s.insert({-power[i], i});
        for (int u : g[i]) {
            s.erase({-power[u], u});
            power[u]++;
            s.insert({-power[u], u});
        }
    }
    cout << ans << '\n';
}

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

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

Code 1:
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
vector<set<int>> g;
vector<int> vis;
void dfs (int v) {
    vis[v] = 1;
    for (int u : g[v]) {
        if (!vis[u]) dfs(u);
    }
}
void solve() {
    int n; cin >> n;
    g = vector<set<int>> (n);
    for (int i = 1; i < n; i++) {
        int u, v; cin >> u >> v;
        g[--u].insert(--v), g[v].insert(u);
    }
    vector<pair<int,int>> cnt;
    for (int i = 0; i < n; i++) cnt.push_back({g[i].size(), i});
    sort(cnt.rbegin(), cnt.rend());
    int del = cnt.begin()->second;
    for (int x : g[del]) {
        g[x].erase(del);
    }
    g[del].clear();
    cnt.clear();
    for (int i = 0; i < n; i++) {
        if (i == del) continue;
        cnt.push_back({g[i].size(), i});
    }
    sort(cnt.rbegin(), cnt.rend());
    int del2 = cnt.begin()->second;
    for (int x : g[del2]) {
        g[x].erase(del2);
    }
    g[del2].clear();
    int ans = 0;
    vis = vector<int> (n);
    for (int i = 0; i < n; i++) {
        if (!vis[i] and i != del and i != del2) ans++, dfs(i);
    }
    cout << ans << '\n';
}
int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t = 1; cin >> t;
    while (t--) solve();

}




Code 2:
#include <iostream>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
void solve() {
    int n; cin >> n;
    vector<set<int>> g(n);
    for (int i = 1; i < n; i++) {
        int u, v; cin >> u >> v;
        g[--u].insert(--v), g[v].insert(u);
    }
    multiset<int, greater<int>> s;
    for (auto x : g) s.insert(x.size());
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j : g[i]) ans = max(ans, (int)g[i].size() + (int)g[j].size() - 2);
    }
    for (int i = 0; i < n; i++) {
        s.erase(s.find(g[i].size()));
        for (int j : g[i]) s.erase(s.find(g[j].size()));
        if (s.size()) {
            ans = max(ans, (int)g[i].size() + *s.begin() - 1);
        }
        for (int j : g[i]) s.insert(g[j].size());
        s.insert(g[i].size());
    }
    cout << ans << '\n';
}
int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t = 1; cin >> t;
    while (t--) solve();

}




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

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

void solve() {
  int n;
  cin >> n;
  vector<vector<int>> graph(n + 1);
  vector<int> degree(n + 1, 0);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    graph[u].push_back(v);
    graph[v].push_back(u);
    degree[u]++;
    degree[v]++;
  }
  for (int u = 1; u <= n; u++)
    sort(graph[u].begin(), graph[u].end());
  vector<pair<int, int>> L;
  L.reserve(n);
  for (int u = 1; u <= n; u++)
    L.push_back({degree[u], u});
  sort(L.rbegin(), L.rend());
  ll best_nonadj = -1;
  for (int u = 1; u <= n; u++) {
    ll candidate = -1;
    for (auto &p : L) {
      int v = p.second;
      if (v == u) continue;
      if (binary_search(graph[u].begin(), graph[u].end(), v)) continue;
      candidate = (ll)degree[u] + degree[v] - 1;
      break;
    }
    best_nonadj = max(best_nonadj, candidate);
  }
  ll best_adj = -1;
  for (int u = 1; u <= n; u++) {
    for (int v : graph[u])
      if (v > u)
        best_adj = max(best_adj, (ll)degree[u] + degree[v] - 2);
  }
  ll ans = max(best_nonadj, best_adj);
  cout << ans << '\n';
}

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


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

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

  vector<vector<int>> graph(n + 1);
  vector<int> degree(n + 1, 0);

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    graph[u].push_back(v);
    graph[v].push_back(u);

    degree[u]++;
    degree[v]++;
  }

  vector<pair<int, int>> candidates;
  for (int i = 1; i <= n; i++)
    candidates.push_back({degree[i], i});

  sort(candidates.rbegin(), candidates.rend());

  int k = min(n, 200);
  vector<pair<int, int>> topcans(candidates.begin(), candidates.begin() + k);

  vector<unordered_set<int>> neighbors(k);
  for (int i = 0; i < k; i++) {
    int v = topcans[i].second;
    for (int neighbor : graph[v])
      neighbors[i].insert(neighbor);
  }

  ll best = 0;
  for (int i = 0; i < k; i++) {
    for (int j = i + 1; j < k; j++) {
      int u = topcans[i].second;
      int v = topcans[j].second;

      bool adj = (neighbors[i].find(v) != neighbors[i].end());
      ll current = degree[u] + degree[v] - 1 - (adj ? 1 : 0);

      best = max(best, current);
    }
  }

  cout << best << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  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>
#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){
    for(auto v:adj[u]){
        if(v==p) continue;
        dfs(v,u,adj,dp,dgr);
        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});
    }
    if(dgr[u]==1 && u!=0){
        dp[u][1][0] = 0;
        dp[u][2][0] = 0;
        dp[u][1][1] = 1;
        dp[u][2][1] = 0;
    }
}
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<<dp[4][1][1]<<endl;
    cout<<max(dp[0][2][0],dp[0][2][1])<<endl;
}
int32_t main(){
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
}


Code 2:
#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();
    }
}


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

Code 1:
#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<set>
#include<string.h>
#include<cstdio>
#include<map>
#include<math.h>
#include<algorithm>
#include<queue>
#include<utility>
#include<unordered_map>
#include<bitset>
#include<iomanip>
using namespace std;
#define IOS ios::sync_with_stdio;cin.tie(0);
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define ll long long
#define lf double
#define MAX_NUM 10000
void solve(){
	int n;
	cin>>n;
	map<int,int>du;
//	map<int,int>reserve_du;
	map<int,set<int>>child;
	for(int i=1;i<=n-1;i++){
		int a,b;
		cin>>a>>b;
		du[a]+=1;
		du[b]+=1;
		child[a].insert(b);
		child[b].insert(a);
	}
	
	vector<int>ans_arr;
	int max_du=-1;
	int cnt=5;
	for(auto x:du)max_du=max(max_du,x.second);
	for(auto x:du){
		if(x.second!=max_du)continue;
		int ord=x.first;
		for(int i=1;i<=n;i++){
			if(i==ord)continue;
			if(child[ord].find(i)!=child[ord].end()){
				ans_arr.push_back(du[ord]+du[i]-2);
			}else {
				ans_arr.push_back(du[ord]+du[i]-1);
			}
		}
		if(cnt--==0)break;
	}
	int ans=-1;
	for(auto x:ans_arr){
		ans=max(ans,x);
	}
	cout<<ans<<endl;
}
int main(){
	IOS;
	int T;
	cin>>T;
//	T=1;
	while(T--){
		solve();
	}
	return 0;
} 


Code 2:
#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<set>
#include<string.h>
#include<cstdio>
#include<map>
#include<math.h>
#include<algorithm>
#include<queue>
#include<utility>
#include<unordered_map>
#include<bitset>
#include<iomanip>
using namespace std;
#define IOS ios::sync_with_stdio;cin.tie(0);
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define ll long long
#define lf double
#define MAX_NUM 10000
void solve(){
	int n;
	cin>>n;
	map<int,int>du;
//	map<int,int>reserve_du;
	map<int,set<int>>child;
	for(int i=1;i<=n-1;i++){
		int a,b;
		cin>>a>>b;
		du[a]+=1;
		du[b]+=1;
		child[a].insert(b);
		child[b].insert(a);
	}
	
	vector<int>ans_arr;
	int max_du=-1;
	for(auto x:du)max_du=max(max_du,x.second);
	for(auto x:du){
		if(x.second!=max_du)continue;
		int ord=x.first;
		for(int i=1;i<=n;i++){
			if(i==ord)continue;
			if(child[ord].find(i)!=child[ord].end()){
				ans_arr.push_back(du[ord]+du[i]-2);
			}else {
				ans_arr.push_back(du[ord]+du[i]-1);
			}
		}
	}
	int ans=-1;
	for(auto x:ans_arr){
		ans=max(ans,x);
	}
	cout<<ans<<endl;
}
int main(){
	IOS;
	int T;
	cin>>T;
//	T=1;
	while(T--){
		solve();
	}
	return 0;
} 


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

Code 1:
#include <stdio.h>
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+200;
set<int>e[N];
vector<pair<int,int>>v;
int a[N];
bool cmp(pair<int,int>x,pair<int,int>y){
	return x.second>y.second;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		ll n;
		cin>>n;
		v.clear();
		for(int i=1;i<=n;++i){
			a[i]=0;
			e[i].clear();
		}
		int mx=0;
		for(int i=1;i<n;++i){
			int x,y;
			cin>>x>>y;
			e[x].insert(y);
			e[y].insert(x);
			a[x]++;
			a[y]++;
		}
		for(int i=1;i<=n;++i) v.push_back({i,a[i]});
		sort(v.begin(),v.end(),cmp);
		int ans=1;
		int cnt=0;
		while(cnt<n-1&&v[cnt].second==v[cnt+1].second) cnt++;
		if(cnt==0){
			ans+=v[0].second-1;
			ans+=v[1].second-1;
			int f=1;
			cnt=1;
			while(cnt<n&&v[cnt].second==v[cnt+1].second) cnt++;
			for(int i=1;i<=cnt;++i){
				if(e[v[i].first].find(v[0].first)==e[v[i].first].end()){
					f=0;
					break;
				}
			}
			if(f) ans--;
		}else if(cnt==1){
			ans+=v[0].second-1;
			ans+=v[1].second-1;
			if(e[v[0].first].find(v[1].first)!=e[v[0].first].end())ans--;
		}else{
			ans+=v[0].second-1;
			ans+=v[1].second-1;
		}
		cout<<max(ans,0)<<'\n';
	} 
	return 0;
}

Code 2:
#include <stdio.h>
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=2e5+200;
set<int>e[N];
vector<pair<int,int>>v;
int a[N];
bool cmp(pair<int,int>x,pair<int,int>y){
	return x.second>y.second;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		ll n;
		cin>>n;
		v.clear();
		for(int i=1;i<=n;++i){
			a[i]=0;
			e[i].clear();
		}
		int mx=0;
		for(int i=1;i<n;++i){
			int x,y;
			cin>>x>>y;
			e[x].insert(y);
			e[y].insert(x);
			a[x]++;
			a[y]++;
		}
		for(int i=1;i<=n;++i) v.push_back({i,a[i]});
		sort(v.begin(),v.end(),cmp);
		int ans=1;
		int cnt=0;
		while(cnt<n-1&&v[cnt].second==v[cnt+1].second) cnt++;
		if(cnt==0){
			ans+=v[0].second-1;
			ans+=v[1].second-1;
			int f=1;
			cnt=1;
			while(cnt<n-1&&v[cnt].second==v[cnt+1].second) cnt++;
			for(int i=1;i<=cnt;++i){
				if(e[v[i].first].find(v[0].first)==e[v[i].first].end()){
					f=0;
					break;
				}
			}
			if(f) ans--;
		}else if(cnt==1){
			ans+=v[0].second-1;
			ans+=v[1].second-1;
			if(e[v[0].first].find(v[1].first)!=e[v[0].first].end())ans--;
		}else{
			ans+=v[0].second-1;
			ans+=v[1].second-1;
		}
		cout<<max(ans,0)<<'\n';
	} 
	return 0;
}

Answer: 2