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

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

using namespace std;
using ll = long long;

int main(){
    ll t;
    cin>>t;
    while(t--){
        ll n, x, y, ans = 0, maxNeighbors = -1, eleRemoved = 1;
        cin>>n;
        vector<vector<ll>> adj(n + 1);
        vector<ll> neighbors(n + 1, 0);
        for(ll i = 0 ; i < n - 1 ; i++){
            cin>>x>>y;
            adj[x].push_back(y);
            adj[y].push_back(x);
        }
        for(ll i = 1 ; i <= n ; i++) neighbors[i] = adj[i].size();
        for(ll k = 1 ; k <= 2 ; k++){
            maxNeighbors = -1, eleRemoved = 1;
            for(ll i = 1 ; i <= n ; i++){
                if(maxNeighbors < neighbors[i]){
                    maxNeighbors = neighbors[i];
                    eleRemoved = i; 
                }
            }
            ans += maxNeighbors;
            for(auto it : adj[eleRemoved]) neighbors[it]--;
            neighbors[eleRemoved] = -1;
        }
        ans -= 1;
        cout<<ans<<endl;
    }
}

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

using namespace std;
using ll = long long;

int main(){
    ll t;
    cin>>t;
    while(t--){
        ll n, x, y, ans = 0, maxNeighbors = -1, eleRemoved = 1;
        cin>>n;
        vector<vector<ll>> adj(n + 1);
        vector<ll> neighbors(n + 1, 0);
        for(ll i = 0 ; i < n - 1 ; i++){
            cin>>x>>y;
            adj[x].push_back(y);
            adj[y].push_back(x);
        }
        for(ll i = 1 ; i <= n ; i++) neighbors[i] = adj[i].size();
        for(ll k = 1 ; k <= 2 ; k++){
            maxNeighbors = -1, eleRemoved = 1;
            for(ll i = 1 ; i <= n ; i++){
                if(maxNeighbors < neighbors[i]){
                    maxNeighbors = neighbors[i];
                    eleRemoved = i; 
                }
                else if(maxNeighbors == neighbors[i]){
                    ll sum1 = 0, sum2 = 0;
                    for(auto it : adj[eleRemoved]) sum1 += neighbors[it];
                    for(auto it : adj[i]) sum2 += neighbors[it];
                    if(sum2 < sum1) eleRemoved = i;
                }
            }
            ans += maxNeighbors;
            for(auto it : adj[eleRemoved]) neighbors[it]--;
            neighbors[eleRemoved] = -1;
        }
        ans -= 1;
        cout<<ans<<endl;
    }
}

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

Code 1:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve()
{
    int n;
    cin >> n;
    vector<vector<int>> e(n);
    vector<int> deg(n);
    for (int i = 1;i < n;i++)
    {
        int v,u;
        cin >> v >> u;
        v--;
        u--;
        e[u].push_back(v);
        e[v].push_back(u);
        deg[u]++;
        deg[v]++;
    }
    sort(e.begin(),e.end(),[](vector<int> a,vector<int> b) {return a.size() > b.size();});
    int mx = e[0].size();
    int premx = -1;
    int cntmx = 0;
    int cntpremx = 0;
    for (int i = 0;i < n;i++)
    {
        if (e[i].size() == mx) cntmx++;
        if (i && e[i - 1].size() == mx && e[i].size() != mx) premx = e[i].size();
        if (e[i].size() == premx) cntpremx++;
    }
    int ans = 0;
    for (int i = 0;e[i].size() == mx;i++)
    {
        int curcntmx = 0;
        int curcntpremx = 0;
        for (auto& adj : e[i])
        {
            if (deg[adj] == mx) curcntmx++;
            else if (deg[adj] == premx) curcntpremx++;
        }
        if (cntmx - curcntmx - 1 > 0) ans = max(ans,mx * 2 - 1);
        else if (cntpremx - curcntpremx > 0) ans = max(ans,mx + premx - 1);
        if (i != 0) ans = max(ans,mx * 2 - 2);
        else ans = max(ans,mx + premx - 2);
    }
    cout << ans << '\n';
} 
int main() 
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
	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>> e(n);
    vector<int> deg(n);
    for (int i = 1;i < n;i++)
    {
        int v,u;
        cin >> v >> u;
        v--;
        u--;
        e[u].push_back(v);
        e[v].push_back(u);
        deg[u]++;
        deg[v]++;
    }
    sort(e.begin(),e.end(),[](vector<int> a,vector<int> b) {return a.size() > b.size();});
    int mx = e[0].size();
    int premx = -1;
    int cntmx = 0;
    int cntpremx = 0;
    for (int i = 0;i < n;i++)
    {
        if (e[i].size() == mx) cntmx++;
        if (i && e[i - 1].size() == mx && e[i].size() != mx) premx = e[i].size();
        if (e[i].size() == premx) cntpremx++;
    }
    int ans = 0;
    for (int i = 0; i < n &&  e[i].size() == mx;i++)
    {
        int curcntmx = 0;
        int curcntpremx = 0;
        for (auto& adj : e[i])
        {
            if (deg[adj] == mx) curcntmx++;
            else if (deg[adj] == premx) curcntpremx++;
        }
        if (cntmx - curcntmx - 1 > 0) ans = max(ans,mx * 2 - 1);
        else if (cntpremx - curcntpremx > 0) ans = max(ans,mx + premx - 1);
        else if (i != 0) ans = max(ans,mx * 2 - 2);
        else ans = max(ans,mx + premx - 2);
    }
    cout << ans << '\n';
} 
int main() 
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
	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;

#define int long long
//
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define vi  vector<int>
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define vvi(v,n,m,k) vector<vi>v(n,vi(m,k));
#define endl "\n"
#define MOD 998244353      
#define print(v) for(int i=0;i<v.size();i++){cout<<v[i]<<" ";} cout<<endl;
#define printl(v) for(auto it :v){cout<<it<<endl;}
#define printp(v) for(auto it : v){cout<<it.ff<<" "<<it.ss<<endl;}
#define printvv(v) {for(int i=0;i<v.size();i++){print(v[i]);}}
//
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)

void solve(){
    int n;
    cin>>n;
    vector<vi>adj(n);
    for(int i=0;i<n-1;i++){
        int x,y;
        cin>>x>>y;
        x--;
        y--;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    vector<pii>v;
    vi incoming(n,0);
    for(int i=0;i<n;i++){
        incoming[i]=adj[i].size();
        v.pb({adj[i].size(),i});
    }
    sort(all(v));
    int cnt=v[n-1].ff;
    int node=v[n-1].ss;
    incoming[node]=0;
    for(int i=0;i<adj[node].size();i++){
        int ver=adj[node][i];
        incoming[ver]=adj[ver].size()-1;
    }
    sort(all(incoming));
    cnt+=incoming[n-1]-1;
    cout<<min(cnt,n-2)<<endl;
}


signed main(){
    fastio();
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}

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

#define int long long
//
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define vi  vector<int>
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define vvi(v,n,m,k) vector<vi>v(n,vi(m,k));
#define endl "\n"
#define MOD 998244353      
#define print(v) for(int i=0;i<v.size();i++){cout<<v[i]<<" ";} cout<<endl;
#define printl(v) for(auto it :v){cout<<it<<endl;}
#define printp(v) for(auto it : v){cout<<it.ff<<" "<<it.ss<<endl;}
#define printvv(v) {for(int i=0;i<v.size();i++){print(v[i]);}}
//
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)

void solve(){
    int n;
    cin>>n;
    vector<vi>adj(n);
    for(int i=0;i<n-1;i++){
        int x,y;
        cin>>x>>y;
        x--;
        y--;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    vector<pii>v;
    vi incoming(n,0);
    for(int i=0;i<n;i++){
        incoming[i]=adj[i].size();
        v.pb({incoming[i],i});
    }
    sort(all(v));
    int cnt=v[n-1].ff;
    int node=v[n-1].ss;
    incoming[node]=-1;
    for(int i=0;i<adj[node].size();i++){
        int ver=adj[node][i];
        incoming[ver]--;
    }
    sort(all(incoming));
    cnt+=incoming[n-1]-1;
    int maxi=cnt;
    for(int i=0;i<n;i++){
        incoming[i]=adj[i].size();
    }
    cnt=v[n-2].ff;
    node=v[n-2].ss;
    incoming[node]=-1;
    for(int i=0;i<adj[node].size();i++){
        int ver=adj[node][i];
        incoming[ver]--;
    }
    sort(all(incoming));
    cnt+=incoming[n-1]-1;
    cout<<max(cnt,maxi)<<endl;
}


signed main(){
    fastio();
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}

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

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

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

	vector<int> subTreeMax(n+1,0);
	vector<int> tree[n+1];

	for(int i = 0; i < n-1; ++i) {
		int u,v;
		cin>>u>>v;

		tree[u].push_back(v);
		tree[v].push_back(u);
	}

	int res = 0;

	function<void(int,int)> dfs = [&](int node, int parent) {
		for(auto child : tree[node]) {
			if(child == parent) continue;
			dfs(child,node);
			subTreeMax[node] = max(subTreeMax[node], (int)tree[child].size());
			subTreeMax[node] = max(subTreeMax[node], subTreeMax[child]);
		}
	};

	function<void(int,int)> dfs2 = [&](int node, int parent) {
		for(auto child : tree[node]) {
			if(child == parent) continue;
			res = max(res,(int)tree[child].size() + (int)tree[node].size() - 2ll);
			res = max(res,(int)tree[node].size() + subTreeMax[child] - 1);
			dfs(child,node);
		}
	};

	dfs(1,-1);
	dfs2(1,-1);

	// for(int i = 1; i <= n; ++i) {
	// 	cout<<subTreeMax[i]<<" ";
	// }
	// cout<<endl;

	cout<<res<<endl;
}

int32_t main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int tc;
	cin >> tc;
	while (tc--)
	{
		solve();
	}
	return 0;
}

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

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

	vector<int>tree[n+1];
	vector<int> degree(n+1);

	for(int i = 1; i < n; ++i) {
		int u,v;
		cin>>u>>v;

		tree[u].push_back(v);
		tree[v].push_back(u);

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

	vector<int> sorted_degree = degree;
	sort(sorted_degree.begin(),sorted_degree.end());

	int res = 1;
	int ans = 0;

	for(int first_node = 1; first_node <= n; ++first_node) {
		res = degree[first_node];

		vector<int> near_nodes;
		near_nodes.push_back(degree[first_node]);

		for(auto child : tree[first_node]) {
			near_nodes.push_back(degree[child]);
		}

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

		vector<int> rem;
		int mx = -1;

		for(auto deg : near_nodes) {
			if(deg == sorted_degree[(int)sorted_degree.size() - 1]){
				sorted_degree.pop_back();
				rem.push_back(deg);
			}
		}

		if(sorted_degree.size()) {
			mx = max(mx, sorted_degree[(int)sorted_degree.size() - 1]);
		}

		for(auto child : tree[first_node]) {
			mx = max(mx, degree[child] - 1);
		}

		reverse(rem.begin(),rem.end());

		for(auto it : rem) {
			sorted_degree.push_back(it);
		}

		ans = max(ans, res + mx - 1);
	}

	cout<<ans<<endl;
	
}

int32_t main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int tc;
	cin >> tc;
	while (tc--)
	{
		solve();
	}
	return 0;
}

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

Code 1:

// Author //

// Md Nehal // 




#include<bits/stdc++.h>



using namespace std;



void solve(){
	int n;
	cin>>n;
	int arr[n+10];
	for(int i=0;i<=n+6;++i){
		arr[i] =0;
	}
	vector<int>graph[n+10];
	for(int i=1;i<n;++i){
		int u ,v;
		cin>>u>>v;
		graph[v].push_back(u);
		graph[u].push_back(v);
		arr[u]++,arr[v]++;
	}
	int maxi =0;
	for(int i=1;i<=n;++i){
		maxi = max(arr[i],maxi);
	}
	int cnt =0;
	for(int i=1;i<=n;++i){
		if(arr[i] == maxi){
			cnt++;
		}
	}
	if(cnt>2){
		cout<<maxi*2 - 1<<endl;
		return ;
	}
	int result =maxi,ind=0;
	for(int i=1;i<=n;++i){
		if(arr[i] == maxi){
			ind = i;
			for(auto &child:graph[i]){
				arr[child]--;
			}
			break ;
		}
	}
	maxi =0;
	for(int i=1;i<=n;++i){
		if(i == ind){
			continue;
		}
		maxi = max(maxi,arr[i]);
	}
	cout<<result+maxi-1<<endl;
	return ;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

Code 2:

// Author //

// Md Nehal // 




#include<bits/stdc++.h>



using namespace std;



void solve(){
	int n;
	cin>>n;
	int arr[n+10];
	for(int i=0;i<=n+6;++i){
		arr[i] =0;
	}
	vector<int>graph[n+10];
	for(int i=1;i<n;++i){
		int u ,v;
		cin>>u>>v;
		graph[v].push_back(u);
		graph[u].push_back(v);
		arr[u]++,arr[v]++;
	}
	int maxi =0;
	for(int i=1;i<=n;++i){
		maxi = max(arr[i],maxi);
	}
	int result =maxi,ind=0;
	for(int i=1;i<=n;++i){
		if(arr[i] == maxi){
			ind = i;
			for(auto &child:graph[i]){
				arr[child]--;
			}
			break ;
		}
	}
	maxi =0;
	for(int i=1;i<=n;++i){
		if(i == ind){
			continue;
		}
		maxi = max(maxi,arr[i]);
	}
	cout<<result+maxi-1<<endl;
	return ;
}
int main(){
	ios_base::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 fi first
#define sec second

void solve(){
	int n;
	cin>>n;
	
	
	vector<int> adj[n+2];
	
	for(int i=0; i<n-1; i++){
		int u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	
	vector<int> ch(n+2);
	vector<pair<int,int>> x;
	
	for(int i=1; i<=n; i++){
		x.push_back({adj[i].size(), i});
	}
	
	sort(x.begin(), x.end());
	reverse(x.begin(), x.end());
	
	if(x[0].fi == 1){
		cout<<"0\n";
		return;
	}
	
	for(auto k : adj[x[0].sec]){
		ch[k] = 1;
	}
	int ans = x[0].fi;
	int mx = -2;
	
	for(int i=1; i<n; i++){
		if(ch[x[i].sec] == 0){
			mx = max(mx, x[i].fi - 1);
			continue;
		}
		
		if(x[i].fi == 1){
			mx = max(mx, -1);
		}else{
			mx = max(mx, x[i].fi - 2);
		}
	}
	
	cout<<ans+mx<<"\n";
	
	return;
}

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


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

#define fi first
#define sec second
#define ll long long

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

	vector<ll> adj[n+2];
	
	for(ll i=0; i<n-1; i++){
		ll u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	
	vector<pair<ll,ll>> x;
	
	for(ll i=1; i<=n; i++){
		x.push_back({adj[i].size(), i});
	}
	
	sort(x.begin(), x.end());
	reverse(x.begin(), x.end());
	
	if(x[0].fi == 1){
		cout<<"0\n";
		return;
	}
	
	ll real_ans = 0;
	for(ll j=0; j<min(n, 5LL); j++){
		vector<ll> ch(n+2);
		for(auto k : adj[x[j].sec]){
			ch[k] = 1;
		}
		
		ll ans = x[j].fi;
		ll mx = -2;
		
		for(ll i=j+1; i<n; i++){
			if(i == j)continue;
			if(ch[x[i].sec] == 0){
				mx = max(mx, x[i].fi - 1);
				continue;
			}
			
			if(x[i].fi == 1){
				mx = max(mx, -1LL);
			}else{
				mx = max(mx, x[i].fi - 2);
			}
		}
		
		real_ans = max(ans+mx, real_ans);
//		cout<<ans+mx<<"\n";
	}
	
	cout<<real_ans<<"\n";
	
	return;
}

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


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

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

const int inf = INT_MAX;
const int mod = 998244353;

void solve() {
    int n; cin >> n;
    vector<vector<int>> adj(n + 1);
    vector<int> deg(n + 1);
    vector<int> mx(n + 1), dp(n + 1);
    for (int i = 1, u, v;i < n;i++) {
        cin >> u >> v;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
        deg[u] += 1, deg[v] += 1;
    }
    auto dfs = [&](auto &&self, int u, int pre) -> void {
        vector<int> degs;
        for (auto &v : adj[u]) {
            if (v == pre) continue;
            self(self, v, u);
            degs.emplace_back(deg[v]);
            dp[u] = max(dp[u], dp[v]);
            dp[u] = max(deg[u] + deg[v] - 2, dp[u]);
            dp[u] = max(dp[u], deg[u] + mx[v] - 1);
            mx[u] = max({mx[u], mx[v], deg[v]});
        }
        sort(degs.begin(), degs.end(), greater<int>());
        if (degs.size() >= 2)
            dp[u] = max(dp[u], degs[0] + degs[1] - 1);
    };
    dfs(dfs, 1, 0);
    cout << dp[1] << endl;
} 

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

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

const int inf = INT_MAX;
const int mod = 998244353;

void solve() {
    int n; cin >> n;
    vector<vector<int>> adj(n + 1);
    vector<int> deg(n + 1);
    vector<int> mx(n + 1), dp(n + 1);
    for (int i = 1, u, v;i < n;i++) {
        cin >> u >> v;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
        deg[u] += 1, deg[v] += 1;
    }
    auto dfs = [&](auto &&self, int u, int pre) -> void {
        int maxdeg = 0, maxdeg2 = 0;
        for (auto &v : adj[u]) {
            if (v == pre) continue;
            self(self, v, u);
            dp[u] = max(dp[u], dp[v]);
            dp[u] = max(deg[u] + deg[v] - 2, dp[u]);
            dp[u] = max(dp[u], deg[u] + mx[v] - 1);
            dp[u] = max(dp[u], deg[v] + maxdeg - 1);
            dp[u] = max(dp[u], maxdeg2 + deg[v] - 1);
            dp[u] = max(dp[u], mx[v] + maxdeg - 1);
            dp[u] = max(dp[u], mx[v] + maxdeg2 - 1);
            mx[u] = max({mx[u], mx[v], deg[v]});
            maxdeg2 = max(maxdeg2, mx[u]); 
            maxdeg = max(maxdeg, deg[v]);
        }
    };
    dfs(dfs, 1, 0);
    cout << dp[1] << endl;
} 

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

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

Code 1:
#include<bits/stdc++.h>
using namespace std;
 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;
#define ordered_set tree<int,null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
 
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
#define int long long int
#define dec greater<pair<int,int>>()
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); }

void solve()
{
    int n;
    cin>>n;
    vector<int> in_degree(n + 1);
    map<pair<int,int>,int>mp;
    for(int i=0; i<n-1; i++)
    {
        int u,v;
        cin>>u>>v;
        in_degree[u]++;
        in_degree[v]++;
        mp[{u,v}]++;
        mp[{v,u}]++;
    }
    vector<pair<int,int>>vp;
    for(int i=1; i<=n; i++){
        vp.push_back({in_degree[i],i});
    }
    sort(all(vp),dec);
    //for(auto &[x,y] : vp)cout<<x<<' '<<y<<'\n';
    int res=0;
    for(int i=0; i<vp.size(); i++)
    {
        for(int j=i+1; j<vp.size(); j++)
        {
            if(!mp.count({vp[i].second,vp[j].second}))res=max(res,vp[i].first+vp[j].first-1);
        }
    }

    for(auto ii : mp)
    {
        int x=ii.first.first,y=ii.first.second;
        res=max(res,in_degree[x]+in_degree[y]-2);
    }
    cout<<res<<'\n';
}
 
signed main()
{
    fast;    
    int t=1;
    cin>>t;
    while(t--)
      solve();
}

Code 2:
#include<bits/stdc++.h>
using namespace std;
 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;
#define ordered_set tree<int,null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
 
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
#define int long long int
#define dec greater<pair<int,int>>()
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); }

void solve()
{
    int n;
    cin>>n;
    vector<int> in_degree(n + 1);
    map<pair<int,int>,int>mp;
    for(int i=0; i<n-1; i++)
    {
        int u,v;
        cin>>u>>v;
        in_degree[u]++;
        in_degree[v]++;
        mp[{u,v}]++;
        mp[{v,u}]++;
    }
    vector<pair<int,int>>vp;
    for(int i=1; i<=n; i++){
        vp.push_back({in_degree[i],i});
    }
    sort(all(vp),dec);
    //for(auto &[x,y] : vp)cout<<x<<' '<<y<<'\n';
    int res=0;
    for(int i=0; i<vp.size(); i++)
    {
        for(int j=i+1; j<vp.size(); j++)
        {
            if(!mp.count({vp[i].second,vp[j].second})){
                res = max(res, vp[i].first + vp[j].first - 1);
                break;
            }
        }
    }

    for(auto ii : mp)
    {
        int x=ii.first.first,y=ii.first.second;
        res=max(res,in_degree[x]+in_degree[y]-2);
    }
    cout<<res<<'\n';
}
 
signed main()
{
    fast;    
    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;

#define fr(i,s,e) for(ll i=s ; i<e ; i++)
#define rf(i,s,e) for(ll i=s ; i>=e ; i--)
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
typedef vector<ll> vll;

ll MOD=998244353;

ll fastpow(ll base , ll power){
	ll ans=1;
	while(power>0){
		if(power&1){
			ans*=base;
			ans%=MOD;
		}
		power>>=1;
		base*=base;
		base%=MOD;
	}
	return ans;
}

void solve(){
	ll n;
	cin >> n;
	vector<vll> v(n);
	vll si(n);
	fr(i, 0, n-1){
		ll a, b;
		cin >> a >> b;
		si[--a]++;
		si[--b]++;
		v[a].pb(b);
		v[b].pb(a);
	}
	sort(si.begin(), si.end());
	ll cnt = 0, k=0;
	fr(i, 0 , n) if(si[n-2] == v[i].size()) cnt++;
	fr(i, 0 , n){
		if(si[n-1] == v[i].size()){
			ll c = 0;
			for(auto x : v[i]){
				if(v[x].size() == si[n-2]) c++;
			}
			if(c == cnt) k = 1;
			if(si[n-1] == si[n-2] && c != cnt-1) k=-1; 
		}
	}
	cout << si[n-1] + si[n-2] - 1 - (si[n-1]==si[n-2]) - k << endl;

}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	ll tt=1;
	cin >> tt;
	while(tt--){
		solve();
	}
}

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

#define fr(i,s,e) for(ll i=s ; i<e ; i++)
#define rf(i,s,e) for(ll i=s ; i>=e ; i--)
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
typedef vector<ll> vll;

ll MOD=998244353;

ll fastpow(ll base , ll power){
	ll ans=1;
	while(power>0){
		if(power&1){
			ans*=base;
			ans%=MOD;
		}
		power>>=1;
		base*=base;
		base%=MOD;
	}
	return ans;
}

void solve(){
	ll n;
	cin >> n;
	vector<vll> v(n+1);
	vll used(n+1);
	fr(i, 1, n+1) v[i].pb(i);
	fr(i, 0, n-1){
		ll a,b;
		cin >> a >> b;
		v[a].pb(b);
		v[b].pb(a);
	}
	vector<vll> v1(v.begin(), v.end());
	sort(v.begin(), v.end(), [](vll a, vll b){return a.size() < b.size();});
	fr(i, 1, v[n].size()){
		v1[v[n][i]].erase( find(v1[v[n][i]].begin(), v1[v[n][i]].end(), v[n][0]) );
	}
	v1.erase(v1.begin() + v[n][0]);
	vector<vll> v2(v1.begin(), v1.end());
	sort(v1.begin(), v1.end(), [](vll a, vll b){return a.size() < b.size();});
	fr(i, 1, v1[n-1].size()){
		fr(j, 1, v2.size()){
			if(v2[j][0] == v1[n-1][i]) v2[j].erase( find(v2[j].begin(), v2[j].end(), v1[n-1][0]) );
		}
	}
	fr(j, 1, v2.size()){
		if(v2[j][0] == v1[n-1][0]) v2.erase( v2.begin() + j);
	}
	if(v2.size()==1){cout << "0\n"; return;}
	ll cnt = 0;
	fr(i, 1, v2.size()){
		if( !used[v2[i][0]] ){
			cnt++;
			for(auto x : v2[i]) used[x] = 1;;
		}
	}
	cout << cnt << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	ll tt=1;
	cin >> tt;
	while(tt--){
		solve();
	}
}

Answer: 1