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

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

#define ll long long 
#define ar array

const int mod = 1e9+7;

void solve(){
    int n;cin>>n;
    vector<int> adj[n];
    vector<int> degree(n,0);
    map<pair<int,int>,int> m;

    for(int i=0;i<n-1;i++){
        int x,y;cin>>x>>y;
        x--,y--;
        m[{x,y}]++;
        degree[x]++,degree[y]++;
    }
    map<pair<int,int>,int> m2;
    for(auto i: m){
       int x = degree[i.first.first], y = degree[i.first.second];
       if(x>y)swap(x,y);
       m2[{x,y}]++;
    }
    sort(degree.begin(),degree.end());
    int x = degree[n-1], y = degree[n-2];
    if(x == 1)cout<<"0\n";
    else if(x == y){
        ll freq = count(degree.begin(),degree.end(),x);
        ll num = m2[{y,x}];
        int ans = 2*x - 1;
        ll choice = (1LL*(freq)*(freq-1))/2;
        if(num>=choice) {
            ans--;
        }
        cout<<ans<<"\n";
    }
    else {
        int freq = count(degree.begin(),degree.end(),y);
        int num = m2[{y,x}];
        int ans = y + x -1;
        int choice = freq;
        if(num>=choice) ans--;
        cout<<ans<<"\n";
    }
}

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

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

#define ll long long 
#define ar array

const int mod = 1e9+7;

void solve(){
    int n;cin>>n;
    vector<int> adj[n];
    vector<int> degree(n,0);
    map<pair<int,int>,int> m;

    for(int i=0;i<n-1;i++){
        int x,y;cin>>x>>y;
        x--,y--;
        m[{x,y}]++;
        degree[x]++,degree[y]++;
    }
    map<pair<int,int>,int> m2;
    for(auto i: m){
       int x = degree[i.first.first], y = degree[i.first.second];
       if(x>y)swap(x,y);
       m2[{x,y}]++;
    }
    int idx = distance(degree.begin(),max_element(degree.begin(), degree.end()));
    vector<int> degree2 = degree;
    sort(degree.begin(),degree.end());
    int x = degree[n-1], y = degree[n-2];
    if(x == 1)cout<<"0\n";
    else if(x == y){
        int freq = count(degree.begin(),degree.end(),x);
        int num = m2[{y,x}];
        int ans = 2*x - 1;
        int choice = (1LL*(freq)*(freq-1))/2;
        if(num>=choice) {
            ans--;
        }
        cout<<ans<<"\n";
    }
    else {
        int ans = x + y -2;

        for(int i=0;i<n;i++){
            if(i==idx)continue;
            int res = degree2[i] + x -1 ;
            // cout<<i<<" "<<idx<<" "<<res<<"\n";
            if(m.find({min(i,idx),max(i,idx)})!=m.end()){
                res--;
            } 
            ans=max(ans,res);
        }
        cout<<ans<<"\n";
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int t;cin>>t;
    while(t--)
    solve();
        return 0;
}


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

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



signed main(){
    int t;
    cin>>t;
    
    while(t--){
        
       int n ; cin>>n ;
       vector<int>indegree(n+1,0);
       vector<pair<int,int>>edges;
       map<int,set<int>>mp;
       for(int i =1  ; i<= (n-1) ; i++){
           int u,v ;
           cin>>u>>v;
           edges.push_back({u,v});
           indegree[u]++;
           indegree[v]++;
           mp[u].insert(v);
           mp[v].insert(u);
       }
       
       int ans = 0 ;
       for(int i = 0 ;  i<edges.size() ; i++){
           ans = max(ans,indegree[edges[i].first] + indegree[edges[i].second] - 2) ;
       }
       
       vector<pair<int,int>>vec;
       for(int i = 1 ; i<=n ; i++){
           vec.push_back({indegree[i],i});
       }
       
       sort(vec.rbegin(),vec.rend());
       
       for(int i = 0 ; i<vec.size(); i++){
           for(int j = i+1 ; j< vec.size() ; j++){
               int u = vec[i].second ;
               int v = vec[j].second ;
               if(mp[u].find(v) == mp[u].end()){
                   ans = max(ans,indegree[v]+indegree[u]-1);
                   break;
               }
               
           }
       }
       
       cout<<ans<<endl;
      
       
    }
}


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

void dfs(int node, int pp , vector<int>&vis, vector<int>graph[]){
    
    vis[node] = 1;
    for(auto ch : graph[node]){
        if(ch == pp)continue ;
        if(!vis[ch]){
            dfs(ch,node,vis,graph);
        }
    }
}

signed main(){
    int t;
    cin>>t;
    
    while(t--){
        
       int n ; cin>>n ;
       
     
       vector<int>indegree(n+1,0);
       
       priority_queue<int>pq;
       for(int i = 1 ; i <= (n-1); i++){
           int u,v ;
           cin>>u>>v;
           indegree[u]++;
           indegree[v]++;
       }
       
       for(int i =1 ; i<=n ; i++){
           pq.push(indegree[i]);
       }
       
       if(n == 2){
           cout<<0<<endl;
       }
       else{
           int a = pq.top();pq.pop();
           int b = pq.top();pq.pop();
        //   cout<<a<<" "<<b<<endl;
           int ans = a+b-1 ;
           if(b == 1){
               ans--;
           }
           cout<<ans<<endl;
       }
       
       
    }
}


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

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

void solve(int n) {
    vector<vector<int>> adj(n+1);
    vector<int> deg(n+1, 0);
    for (int i = 0; i < n-1; i++) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
        deg[a]++; deg[b]++;
    }
    int mx1 = 0;
    for (int i = 1; i <= n; i++) if (deg[i] > deg[mx1]) mx1 = i;
    int ans = deg[mx1];
    deg[mx1] = 0;
    for (int u : adj[mx1]) deg[u]--;
    int mx2 = 0;
    for (int i = 1; i <= n; i++) if (deg[i] > deg[mx2]) mx2 = i;
    ans += deg[mx2] - 1;
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        cin >> n;
        solve(n);
    }

    return 0;
}

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

void solve(int n) {
    vector<vector<int>> adj(n+1);
    vector<int> deg(n+1, 0);
    for (int i = 0; i < n-1; i++) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
        deg[a]++; deg[b]++;
    }

    int c1 = 0, c2 = 0;
    for (int i = 1; i <= n; i++) {
        if (deg[i] >= deg[c2]) {
            c2 = i;
            if (deg[c2] >= deg[c1]) swap(c1, c2);
        }
    }

    int ans1 = 0, ans2 = 0;
    for (int u : adj[c1]) deg[u]--;
    for (int i = 1; i <= n; i++) if (i != c1) ans1 = max(ans1, deg[i]);
    for (int u : adj[c1]) deg[u]++;
    ans1 += (deg[c1]-1);
    for (int u : adj[c2]) deg[u]--;
    for (int i = 1; i <= n; i++) if (i != c2) ans2 = max(ans2, deg[i]);
    ans2 += (deg[c2]-1);
    cout << max(ans1, ans2) << '\n';
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        cin >> n;
        solve(n);
    }

    return 0;
}

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

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

void solve() {
  int n; cin >> n;
  vector<pair<int,int>> b;
  set<pair<int,int>> edges;
  vector<int> degree(n,0);
  
  for (int i = 0; i < n-1; i++) {
    int u,v;
    cin >> u >> v;
    u--; v--;
    
    degree[u]++;
    degree[v]++;
    edges.insert(make_pair(u,v));
    edges.insert(make_pair(v,u));
  }
  
  for (int i = 0; i <= n-1; i++) {
    b.push_back(make_pair(degree[i],i));
  }
  sort(b.begin(), b.end(), greater<pair<int,int>>());
  
  if (n == 2) {cout << 0 << endl; return;}
  
  int M = b[0].first, N = b[1].first, K = b[2].first;
  
  if (M == N && N == K) {
    cout << 2*M - 1 << endl;
    return;
  } else if (M == N && N > K) {
    if (edges.find(make_pair(b[0].second, b[1].second)) != edges.end()) {
      cout << M+N-1 << endl;
    } else {
      cout << 2*M - 1 << endl;
    }
    return;
  } else if (M > N && N == K) {
    int flag = 0;
    for (int i = 1; i < n; i++) {
      if (b[i].first != b[1].first) break; 
      if (edges.find(make_pair(b[0].second, b[i].second)) == edges.end()) {
        flag = 1;
        break;
      }
    }
    
    if (flag) {
      cout << M+N-1 << endl;
    } else {
      cout << M+N-2 << endl;
    }
    return;
  } else {
    cout << M+N-1 << endl;
    return;
  }
  
  return;
}

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<pair<int,int>> b;
  set<pair<int,int>> edges;
  vector<int> degree(n,0);
  
  for (int i = 0; i < n-1; i++) {
    int u,v;
    cin >> u >> v;
    u--; v--;
    
    degree[u]++;
    degree[v]++;
    edges.insert(make_pair(u,v));
    edges.insert(make_pair(v,u));
  }
  
  for (int i = 0; i <= n-1; i++) {
    b.push_back(make_pair(degree[i],i));
  }
  sort(b.begin(), b.end(), greater<pair<int,int>>());
  
  if (n == 2) {cout << 0 << endl; return;}
  
  int M = b[0].first, N = b[1].first, K = b[2].first;
  
  if (M == N && N == K) {
    cout << 2*M - 1 << endl;
    return;
  } else if (M == N && N > K) {
    if (edges.find(make_pair(b[0].second, b[1].second)) != edges.end()) {
      cout << M+N-2 << endl;
    } else {
      cout << 2*M-1 << endl;
    }
    return;
  } else if (M > N) {
    int flag = 0;
    for (int i = 1; i < n; i++) {
      if (b[i].first != b[1].first) break; 
      if (edges.find(make_pair(b[0].second, b[i].second)) == edges.end()) {
        flag = 1;
        break;
      }
    }
    
    if (flag) {
      cout << M+N-1 << endl;
    } else {
      cout << M+N-2 << endl;
    }
    return;
  }
  
  return;
}

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

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

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

vector<vector<int>> adj; 
vector<int> degree;

// void dfs(int node, int par) {
//     for (auto v : adj[node]) {
//         if (v != par) {
//             degree[node]++;
//             dfs(v,node);
//         }
//     }
// }


signed main() {
    int t;
    cin>>t;
    while (t--)
    {
        
        int n;
        cin>>n;
        // adj.clear();
        // adj.resize(n + 1);
        degree.assign(n+1,0);
        set<pair<int,int>> s;
        

        for (int i = 1; i < n; i++) {
            int a,b;
            cin >> a>>b;
            degree[a]++;
            degree[b]++;
            // adj[a].push_back(b);
            // adj[b].push_back(a);
            s.insert({a,b});
            s.insert({b,a});
        }

        vector<pair<int,int>> arr;

        for(int i=1;i<=n;i++) arr.push_back({degree[i],i});
        sort(arr.rbegin(),arr.rend());
        int ans=0;

        for(int i=0;i<arr.size();i++){
            for(int j=i+1;j<arr.size();j++){

                if(s.find({arr[i].second,arr[j].second})==s.end()){
                    ans=max(ans,arr[i].first+arr[j].first-1);
                    break;
                }
            }
        }

        for(auto it:s){
            ans=max(ans,degree[it.first]+degree[it.second]-2);
        }
        cout<<ans<<endl;

        //dfs(1, -1);
        
       
       
    }



    return 0;
}

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

vector<vector<int>> adj; 
vector<int> degree;

void dfs(int node, int par) {
    for (auto v : adj[node]) {
        if (v != par) {
            degree[node]++;
            dfs(v,node);
        }
    }
}


signed main() {
    int t;
    cin>>t;
    while (t--)
    {
        
        int n;
        cin>>n;
        adj.clear();
        adj.resize(n + 1);
        degree.assign(n+1,1);
        
        degree[1]=0;

        for (int i = 1; i < n; i++) {
            int a,b;
            cin >> a>>b;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }

        dfs(1, -1);

        int element=0,deg=0;

        for(int i=1;i<=n;i++){
            if(degree[i]>deg) element=i,deg=degree[i];
        }
        //cout<<deg<<" "<<element<<endl;
        

        if(deg==n-1) {
            cout<<n-2<<endl;
            continue;
        }


        int ans=0;
        ans+=deg;
        degree[element]=0;

        for(auto v:adj[element]) degree[v]--;

        element=0,deg=0;

        for(int i=1;i<=n;i++){
            if(degree[i]>deg) element=i,deg=degree[i];
        }

        ans+=deg-1;
        cout<<ans<<endl;



       
    }



    return 0;
}

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

Code 1:
#include<iostream>
#include<vector>
#include<set>
#define add(a,b) e[a].push_back(b)
#define rep for(int v:e[i])
#define siz(a) e[a].size()
using namespace std;

const int N=1e6+5;

int t;

vector<int>e[N];
multiset<int>s;


inline void work(){
	s.clear();
	int n,ans=0;
	cin>>n;
	for(int i=0;i<=n;++i)e[i].clear();
	for(int i=1;i<n;++i){
		int u,v;
		cin>>u>>v;
		add(u,v),add(v,u);
	}
	for(int i=1;i<=n;++i){
		s.insert(siz(i));
	}
	for(int i=1;i<=n;++i){
		s.erase(s.find(siz(i)));
		rep{
			s.erase(s.find(siz(v)));
			s.insert(siz(v)-1);
		}
		ans=max(ans,(int)siz(i)+*s.rbegin()-1);
		s.insert(siz(i));
		rep{
			s.erase(s.find(siz(v)-1));
			s.insert(siz(v));
		}
	}
	cout<<ans<<"\n";
	return ;
}

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

Code 2:
#include<iostream>
#include<vector>
#include<set>
#define add(a,b) e[a].push_back(b)
#define rep for(int v:e[i])
#define siz(a) e[a].size()
using namespace std;

const int N=1e5+5;

int t;

vector<int>e[N];
multiset<int>s;


inline void work(){
	s.clear();
	for(int i=0;i<N;++i)e[i].clear();
	int n,ans=0;
	cin>>n;
	for(int i=1;i<n;++i){
		int u,v;
		cin>>u>>v;
		add(u,v),add(v,u);
	}
	for(int i=1;i<=n;++i){
		s.insert(siz(i));
	}
	for(int i=1;i<=n;++i){
		s.erase(s.find(siz(i)));
		rep{
			s.erase(s.find(siz(v)));
			s.insert(siz(v)-1);
		}
		ans=max(ans,(int)siz(i)+*s.rbegin()-1);
		s.insert(siz(i));
		rep{
			s.erase(s.find(siz(v)-1));
			s.insert(e[v].size());
		}
	}
	cout<<ans<<"\n";
	return ;
}

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

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

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

using namespace std;
const int N = 2e5 + 5;
int t;
int n, u, v, ans, d[N];
set<pair<int, int> > c;
vector<int> a[N];
void dfs(int u, int p) {
    for(auto& v : a[u]) {
        if(v != p) {
            dfs(v, u);
            ans = max(ans, (int) a[v].size() + (int) a[u].size() - 2);
        }
    }
}
bool cmp(int x, int y) {
    if(a[x].size() == a[y].size())return x < y;
    return a[x].size() > a[y].size();
}
void solve() {
    ans = 0;
    dfs(1, 0);
    sort(d + 1, d + n + 1, cmp);
    for(int i = 1; i <= n; i++) {
        for(int j = i + 1; j <= n; j++) {
            if(c.find({d[i], d[j]}) == c.end()) {
                ans = max(ans, (int) a[d[i]].size() + (int) a[d[j]].size() - 1);
                break;
            }
        }
    }
    cout << ans << '\n';
}
void input() {
	cin >> t;
	while(t--) {
        cin >> n;
        c.clear();
        for(int i = 1; i <= n; i++) {
            a[i].clear();
            d[i] = i;
        }
        for(int i = 1; i <= n - 1; i++) {
            cin >> u >> v;
            c.insert({u, v});
            c.insert({v, u});
            a[u].push_back(v);
            a[v].push_back(u);
        }
		solve();
	}
}

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


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

using namespace std;
const int N = 2e5 + 5;
int t;
int n, u, v, ans, d[N], d1[N];
vector<int> a[N];
void dfs(int u, int p) {
    d[u] = a[u].size();
    int mx = 0, mx1 = 0;
    for(auto& v : a[u]) {
        if(v != p) {
            dfs(v, u);
            d[u] = max(d[u], d[v]);
            d1[u] = max(d1[u], (int) a[v].size());
            mx1 = max(mx1, d1[v]);
            ans = max(ans, (int) a[v].size() + (int) a[u].size() - 2);
            ans = max(ans, mx + d[v] - 1);
            mx = max(mx, d[v]);
        }
    }
    ans = max(ans, mx1 + (int) a[u].size() - 1);
}
void solve() {
    ans = 0;
    dfs(1, 0);
    cout << ans << '\n';
}
void input() {
	cin >> t;
	while(t--) {
        cin >> n;
        for(int i = 1; i <= n; i++) {
            a[i].clear();
            d[i] = 0;
            d1[i] = 0;
        }
        for(int i = 1; i <= n - 1; i++) {
            cin >> u >> v;
            a[u].push_back(v);
            a[v].push_back(u);
        }
		solve();
	}
}

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


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

Code 1:
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string.h>
#define ll long long
#define pii pair<int,int>
using namespace std;

const int LM=200100;
int n;
vector<int>G[LM];

int C(int x, int y){
    for(int i:G[x])if(y==i)return 1;
    return 0;
}

void solve(){
    scanf("%d",&n);
    for(int i=1; i<=n; i++)G[i].clear();
    for(int i=1; i<n; i++){
        int u,v;
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int si[LM], Ms=0;
    vector<int>v[LM];
    for(int i=1; i<=n; i++){
        si[i]=G[i].size();
        Ms=max(Ms,si[i]);
        v[si[i]].push_back(i);
    }
    int MsSi=v[Ms].size();
    if(MsSi>=3)
        printf("%d\n",Ms*2-1);
    if(MsSi==2){
        printf("%d\n",Ms*2-C(v[Ms][0],v[Ms][1])-1);
    }
    else{
        int ws=Ms-1;
        while(v[ws].size()==0)ws--;
        for(int i:v[ws]){
            if(C(i,v[Ms][0])==0){
                printf("%d\n",Ms+ws-1);
                return;
            }
        }
        printf("%d\n",Ms+ws-2);
    }
}

int main(){
    int tc;
    scanf("%d", &tc);
    for(;tc>0;tc--)solve();
}


Code 2:
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string.h>
#define ll long long
#define pii pair<int,int>
using namespace std;

const int LM=200100;
int n;
vector<int>G[LM];
set<int> S[LM];
vector<int>v[LM];

int C(int x, int y){
    return S[x].find(y)!=S[x].end();
}

void solve(){
    scanf("%d",&n);
    for(int i=1; i<=n; i++)G[i].clear(),v[i].clear(),S[i].clear();
    for(int i=1; i<n; i++){
        int x,y;
        scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
        S[x].insert(y);
        S[y].insert(x);
    }
    int si[LM], Ms=0;
    
    for(int i=1; i<=n; i++){
        si[i]=G[i].size();
        Ms=max(Ms,si[i]);
        v[si[i]].push_back(i);
    }
    
    int MsSi=v[Ms].size();
    if(MsSi>=3)
        printf("%d\n",Ms*2-1);
    else if(MsSi==2){
        printf("%d\n",Ms*2-C(v[Ms][0],v[Ms][1])-1);
    }
    else{
        int ws=Ms-1;
        while(v[ws].empty())ws--;
        for(int i:v[ws]){
            if(C(i,v[Ms][0])==0){
                printf("%d\n",Ms+ws-1);
                return;
            }
        }
        printf("%d\n",Ms+ws-2);
    }
}

int main(){
    int tc;
    scanf("%d", &tc);
    for(;tc>0;tc--)solve();
}


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

Code 1:
#include <bits/stdc++.h>
#define FOR(i,j,k) for(int i=j, r##i=k; i<=r##i; ++i)
#define ROF(i,j,k) for(int i=j, l##i=k; i>=l##i; --i)
inline int read (void) {
  int x = 0, f = 1, ch = getchar();
  while(!isdigit(ch)) { if(ch == '-') f = -f; ch = getchar(); }
  while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
  return x * f;
}
const int maxn = 200005;
int tot_edge, head[maxn], to[maxn<<1], link[maxn<<1];
inline void add_edge (int x, int y) {
  to[++tot_edge] = y;
  link[tot_edge] = head[x];
  head[x] = tot_edge;
}
using pii = std::pair <int, int>;
int d[maxn];
pii p[maxn];
bool flag[maxn];
int main (void) {
  int t = read();
  while(t--) {
    int n = read();
    FOR(i,2,n) {
      int x = read(), y = read();
      add_edge (x, y);
      add_edge (y, x);
      ++ d[x], ++ d[y];
    }
    FOR(i,1,n) p[i] = pii (d[i], i);
    std::sort(p+1, p+n+1, std::greater<pii>());
    int c = -1 << 20, c2 = -1 << 20, uc = -1 << 20;
    for(int now=head[p[1].second]; now; now=link[now])
      flag[to[now]] = 1;
    FOR(i,2,n)
      if(flag[p[i].second])  {
        if(c == -1 << 20) c = p[i].first;
        else if(c2 == -1 << 20) c2 = p[i].first;
      } else
        if(uc == -1 << 20) uc = p[i].first;
    printf("%d\n", std::max (c + c2 - 1, std::max (p[1].first + c - 2, p[1].first + uc - 1)));
    FOR(i,1,n) d[i] = flag[i] = head[i] = 0;
    tot_edge = 0;
  }
  return 0;
}


Code 2:
#include <bits/stdc++.h>
#define FOR(i,j,k) for(int i=j, r##i=k; i<=r##i; ++i)
#define ROF(i,j,k) for(int i=j, l##i=k; i>=l##i; --i)
inline int read (void) {
  int x = 0, f = 1, ch = getchar();
  while(!isdigit(ch)) { if(ch == '-') f = -f; ch = getchar(); }
  while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
  return x * f;
}
const int maxn = 200005;
int tot_edge, head[maxn], to[maxn], link[maxn];
inline void add_edge (int x, int y) {
  to[++tot_edge] = y;
  link[tot_edge] = head[x];
  head[x] = tot_edge;
}
using pii = std::pair <int, int>;
int d[maxn];
pii p[maxn];
bool flag[maxn];
int main (void) {
  int t = read();
  while(t--) {
    int n = read();
    FOR(i,2,n) {
      int x = read(), y = read();
      add_edge (x, y);
      add_edge (y, x);
      ++ d[x], ++ d[y];
    }
    FOR(i,1,n) p[i] = pii (d[i], i);
    std::sort(p+1, p+n+1, std::greater<pii>());
    int c = -1 << 20, c2 = -1 << 20, uc = -1 << 20;
    for(int now=head[p[1].second]; now; now=link[now])
      flag[to[now]] = 1;
    FOR(i,2,n)
      if(flag[p[i].second])  {
        if(c == -1 << 20) c = p[i].first;
        else if(c2 == -1 << 20) c2 = p[i].first;
      } else
        if(uc == -1 << 20) uc = p[i].first;
    printf("%d\n", std::max (c + c2 - 1, std::max (p[1].first + c - 2, p[1].first + uc - 1)));
    FOR(i,1,n) d[i] = flag[i] = 0;
  }
  return 0;
}


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

Code 1:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5;
void solve(){
	int n;
	cin>>n;
	set<pair<int,int> > s;
	map<int,int> mp;
	vector<pair<int,int> > vc;
	for(int i=1;i<=n-1;i++){
		int u,v;
		cin>>u>>v;
		s.insert({u,v});
		s.insert({v,u});
		mp[u]++;
		mp[v]++;
	}
	for(auto it:mp){
		vc.push_back({it.second,it.first});
	}
	sort(vc.begin(),vc.end());
	int ans=0;
	for(int i=n-1;vc[i].first>=vc[n-1].first&&i>=0;i--){
		for(int j=i-1;vc[j].first>=vc[i-1].first&&j>=0;j--){
			int temp=vc[i].first+vc[j].first;
			//cout<<temp<<endl;
			int u=vc[i].second;
			int v=vc[j].second;
			if(s.find({u,v})==s.end()){   //不相邻 
				temp--;
				ans=max(ans,temp);
				cout<<ans<<endl;
				return;
			}else{
				temp-=2;
			}
			ans=max(ans,temp);
		}
	}
	cout<<ans<<endl;
}
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;
	cin>>t;
	while(t--){
		solve();
	}
}

Code 2:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5;
void solve(){
	int n;
	cin>>n;
	set<pair<int,int> > s;
	map<int,int> mp;
	vector<pair<int,int> > vc;
	for(int i=1;i<=n-1;i++){
		int u,v;
		cin>>u>>v;
		s.insert({u,v});
		s.insert({v,u});
		mp[u]++;
		mp[v]++;
	}
	for(auto it:mp){
		vc.push_back({it.second,it.first});
	}
	sort(vc.begin(),vc.end());
	int ans=0;
	for(int i=n-1;vc[i].first>=vc[n-1].first&&i>=0;i--){
		for(int j=i-1;vc[j].first>=vc[i-1].first&&j>=0;j--){
			int temp=vc[i].first+vc[j].first;
			//cout<<temp<<endl;
			int u=vc[i].second;
			int v=vc[j].second;
			if(s.find({u,v})==s.end()){   //不相邻 
				temp--;
			}else{
				temp-=2;
			}
			ans=max(ans,temp);
		}
	}
	cout<<ans<<endl;
}
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;
	cin>>t;
	while(t--){
		solve();
	}
}

Answer: 1