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

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        vector<set<ll>> adj(n);
        vector<ll> deg(n);
        for(ll i = 0; i < n - 1; i++) {
            ll u, v;
            cin >> u >> v;
            u--;
            v--;
            adj[u].insert(v);
            adj[v].insert(u);
            deg[u]++;
            deg[v]++;
        }
        vector<pair<ll, ll>> e;
        for(ll i = 0; i < n; i++) {
            e.push_back({deg[i], i});
        }
        sort(e.rbegin(), e.rend());
        ll ans = 0;
        for(ll i = 0; i < n; i++) {
            for(ll j = i + 1; j < n; j++) {
                ll u = e[i].second;
                ll v = e[j].second;
                if(adj[u].count(v)) {
                    continue;
                }
                ans = max(ans, deg[u] + deg[v] - 1);
                break;
            }
        }
        for(ll i = 0; i < n; i++) {
            for(ll j: adj[i]) {
                ans = max(ans, deg[i] + deg[j] - 2);
            }
        }
        cout << ans << '\n';
    }
    return 0;
}

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

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        vector<set<ll>> adj(n);
        vector<ll> deg(n);
        for(ll i = 0; i < n - 1; i++) {
            ll u, v;
            cin >> u >> v;
            u--;
            v--;
            adj[u].insert(v);
            adj[v].insert(u);
            deg[u]++;
            deg[v]++;
        }
        vector<pair<ll, ll>> e;
        for(ll i = 0; i < n; i++) {
            e.push_back({deg[i], i});
        }
        sort(e.rbegin(), e.rend());
        ll ans = 0;
        for(ll i = 0; i < n; i++) {
            for(ll j = i + 1; j < n; j++) {
                ll u = e[i].second;
                ll v = e[j].second;
                if(adj[u].count(v)) {
                    continue;
                }
                ans = max(ans, deg[u] + deg[v] - 1);
            }
        }
        for(ll i = 0; i < n; i++) {
            for(ll j: adj[i]) {
                ans = max(ans, deg[i] + deg[j] - 2);
            }
        }
        cout << ans << '\n';
    }
    return 0;
}

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

Code 1:
#include<stdio.h>
#include<set>
struct edge
{
	int to,next;
}e[400010];
int head[200010];
int sum;
void add(int from,int to)
{
	e[++sum].to=to;
	e[sum].next=head[from];
	head[from]=sum;
}
int size[200010];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n;
		sum=0;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			size[i]=0;
			head[i]=0;
		}
		for(int i=1;i<n;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			add(u,v);
			add(v,u);
			size[u]++;
			size[v]++;
		}
		std::multiset<int> mul;
		for(int i=1;i<=n;i++)
			mul.insert(size[i]);
		int maxn=0;
		for(int i=1;i<=n;i++)
		{
			int sze=size[i];
			mul.erase(size[i]);
			for(int j=head[i];j;j=e[j].next)
			{
				mul.erase(size[e[j].to]);
				mul.insert(size[e[j].to]-1);
			}
			std::multiset<int>::iterator it;
			it=mul.end();
			it--;
			maxn=std::max(maxn,sze+*it-1);
			for(int j=head[i];j;j=e[j].next)
			{
				mul.erase(size[e[j].to]-1);
				mul.insert(size[e[j].to]);
			}
			mul.insert(size[i]);
		}
		printf("%d\n",maxn);
	}
	return 0;
}


Code 2:
#include<stdio.h>
#include<set>
struct edge
{
	int to,next;
}e[400010];
int head[200010];
int sum;
void add(int from,int to)
{
	e[++sum].to=to;
	e[sum].next=head[from];
	head[from]=sum;
}
int size[200010];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n;
		sum=0;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			size[i]=0;
			head[i]=0;
		}
		for(int i=1;i<n;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			add(u,v);
			add(v,u);
			size[u]++;
			size[v]++;
		}
		std::multiset<int> mul;
		for(int i=1;i<=n;i++)
			mul.insert(size[i]);
		int maxn=0;
		for(int i=1;i<=n;i++)
		{
//			for(auto it:mul)
//				printf("%d ",it);
//			printf("\n");
			int sze=size[i];
			mul.erase(mul.find(size[i]));
			for(int j=head[i];j;j=e[j].next)
			{
				mul.erase(mul.find(size[e[j].to]));
				mul.insert(size[e[j].to]-1);
			}
			std::multiset<int>::iterator it;
			it=mul.end();
			it--;
			maxn=std::max(maxn,sze+*it-1);
			for(int j=head[i];j;j=e[j].next)
			{
				mul.erase(mul.find(size[e[j].to]-1));
				mul.insert(size[e[j].to]);
			}
			mul.insert(size[i]);
		}
		printf("%d\n",maxn);
	}
	return 0;
}


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

Code 1:
#include<iostream>
#include<queue>
#include<set>
#include<vector>
using namespace std;

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

	vector< vector<int> > g(n + 1);
	for(int i = 0; i < n - 1; i++) {
		int a, b;
		cin >> a >> b;

		g[a].push_back(b);
		g[b].push_back(a);
	}

	if(n == 2) {
		cout << 0 << endl;
		return;
	}

	if(n == 3) {
		cout << 1 << endl;
		return;
	}

	priority_queue<int> pq;

	for(auto &itr: g) {
		pq.push(itr.size());
	}

	int maxi = pq.top();
	pq.pop();
	int second_maxi = pq.top();
	pq.pop();

	int count = 0;
	for(auto &node: g) {
		if(node.size() == second_maxi) {
			count++;
		}
	}

	bool found = false;
	for(auto &node: g) {
		if(node.size() != maxi) continue;

		int current_count = 0;
		for(auto &neigh: node) {
			if(g[neigh].size() == second_maxi) {
				current_count++;
			}
		}

		if(maxi == second_maxi) {
			if(current_count < count - 1) {
				found = true;
				break;
			}
		} else {
			if(current_count < count) {
				found = true;
				break;
			}
		}
	}

	if(found) {
		cout << maxi + second_maxi - 1<< endl;
	} else {
		cout << maxi + second_maxi - 2 << endl;
	}

	return;
}



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

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

	return 0;
}


Code 2:
#include<iostream>
#include<queue>
#include<set>
#include<vector>
using namespace std;

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

	vector< vector<int> > g(n + 1);
	for(int i = 0; i < n - 1; i++) {
		int a, b;
		cin >> a >> b;

		g[a].push_back(b);
		g[b].push_back(a);
	}

	priority_queue<int> pq;

	for(auto &itr: g) {
		pq.push(itr.size());
	}

	int maxi = pq.top();
	pq.pop();
	int second_maxi = pq.top();
	pq.pop();

	bool global_found = false;
	for(auto &node: g) {
		if(node.size() != maxi) continue;

		bool found = false;
		for(auto &neigh: node) {
			if(g[neigh].size() == second_maxi) {
				found = true;
				break;
			}
		}

		if(found) continue;
		global_found = true;
	}

	if(global_found) {
		cout << maxi + second_maxi - (maxi == 1) - (second_maxi == 1) << endl;
	} else {
		cout << maxi + second_maxi - 1 - (second_maxi == 1) << endl;
	}

	return;
}



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

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

	return 0;
}


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

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

#define all(v) v.begin(), v.end()
#define debug(x) cout << #x << '=' << x << endl;
#define pb push_back
#define int long long
const long long M = 998244353;

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

    vector<vector<int>> vec(n+1);
    unordered_set<int> st;
    for(int i = 1; i <n; i++){
        int a, b; cin>>a>>b;
        vec[a].push_back(b);
        vec[b].push_back(a);
        st.insert((a*1e6+b));
        st.insert((b*1e6+a));
    }
    if(n<=4){
        cout<<n-2<<endl;
        return;
    }

    vector<pair<int, int>> sz;
    for(int i = 1; i <=n; i++){
        sz.push_back({vec[i].size(), i});
    }
    sort(sz.begin(), sz.end());

    for(int i = n-2; i >=0; i--){
        if(sz[i].first < sz[n-2].first){
            break;
        }
        if(st.count((sz[n-1].second*1e6 + sz[i].second)) == 0){
            cout<<sz[n-1].first+ sz[i].first-1<<endl;
            return;
        }
    }

    cout<<sz[n-1].first+ sz[n-2].first-2<<endl;

}

signed main() {
    ios::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 all(v) v.begin(), v.end()
#define debug(x) cout << #x << '=' << x << endl;
#define pb push_back
#define int long long
const long long M = 998244353;

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

    vector<vector<int>> vec(n+1);
    unordered_set<int> st;
    for(int i = 1; i <n; i++){
        int a, b; cin>>a>>b;
        vec[a].push_back(b);
        vec[b].push_back(a);
        st.insert((a*1e6+b));
        st.insert((b*1e6+a));
    }
    if(n<=4){
        cout<<n-2<<endl;
        return;
    }

    vector<pair<int, int>> sz;
    for(int i = 1; i <=n; i++){
        // cout<<vec[i].size()<<" ";
        sz.push_back({vec[i].size(), i});
    } //cout<<endl;
    sort(sz.begin(), sz.end());

    int ans = sz[n-1].first+ sz[n-2].first-2;

    if(st.count((sz[n-1].second*1e6+sz[n-2].second)) == 0) ans++;

    for(int i = n-3; i >=0; i--){
        if(st.count((sz[n-1].second*1e6+sz[i].second)) == 0) ans = max(ans, sz[n-1].first+sz[i].first-1);
        else ans = max(ans, sz[n-1].first+sz[i].first-2);

        if(st.count((sz[n-2].second*1e6+sz[i].second)) == 0) ans = max(ans, sz[n-2].first+sz[i].first-1);
        else ans = max(ans, sz[n-2].first+sz[i].first-2);
    }


    cout<<ans<<endl;

}

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++23 

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

int main() {
	ll t;
	cin >> t;
	
	while (t--) {
		ll z;
		cin >> z;
		vector<set<ll>> tree(z + 1);
		
		for (ll i = 0; i < z - 1; i++) {
			ll x, y;
			cin >> x >> y;
			tree[x].insert(y);
			tree[y].insert(x);
		}
		vector<set<ll>> tree1=tree;
		ll connected = 0, idx = 1;
		
		// Finding the most connected node
		for (ll i = 1; i <= z; i++) {
			if (connected < (ll)tree[i].size()) {
				connected = tree[i].size();
				idx = i;
			}
		}

		ll ans = 1;
		ans += tree[idx].size()-1;
		// Remove first selected node's edges
		for (auto x : tree[idx]) {
			tree[x].erase(idx);
		}
		tree[idx].clear();

		// Find the next most connected node
		connected = 0, idx = 1;
		for (ll i = 1; i <= z; i++) {
			if (connected < (ll)tree[i].size()) {
				connected = tree[i].size();
				idx = i;
			}
		}

		ans += tree[idx].size()-1;
		
		
		 connected = 0;
		  idx = 1;
		
		// Finding the most connected node
		for (ll i = z; i >=1; i--) {
			if (connected < (ll)tree1[i].size()) {
				connected = tree1[i].size();
				idx = i;
			}
		}

		ll ans1 = 1;
		ans1 += tree1[idx].size()-1;
		// Remove first selected node's edges
		for (auto x : tree1[idx]) {
			tree1[x].erase(idx);
		}
		tree1[idx].clear();

		// Find the next most connected node
		connected = 0, idx = 1;
		for (ll i = 1; i <= z; i++) {
			if (connected < (ll)tree1[i].size()) {
				connected = tree1[i].size();
				idx = i;
			}
		}

		ans1 += tree1[idx].size()-1;
		cout <<max(ans,ans1)<< endl;
	}
	
	return 0;
}



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

int main() {
	ll t;
	cin >> t;
	
	for(ll j=1;j<=t;j++) {
		ll z;
		cin >> z;
		vector<set<ll>> tree(z + 1);
		vector<ll> v;
		for (ll i = 0; i < z - 1; i++) {
			ll x, y;
			cin >> x >> y;
			v.push_back(x);
			v.push_back(y);
			tree[x].insert(y);
			tree[y].insert(x);
		}
		if(j==1142)
		{
			for(ll i=0;i<v.size();i++)
			{
				cout<<v[i];
			}
			cout<<endl;
		}
	  bool isall2=0;
	  ll count1=0,count2=0;
	  for(ll i=1;i<=z;i++)
	  {
	  	if(tree[i].size()==1)
		{
			count1++;
		}
		else if(tree[i].size()==2)
		{
			count2++;
		}
	  }

	  if(z>=5&&count2==z-2&&count1==2)
	  {
	  	isall2=1;
	  }
		ll connected = 0, idx = 1;
		
		// Finding the most connected node
		for (ll i = 1; i <= z; i++) {
			if (connected < (ll)tree[i].size()) {
				connected = tree[i].size();
				idx = i;
			}
		}

		ll ans = 1;
		ans += tree[idx].size()-1;
		// Remove first selected node's edges
		for (auto x : tree[idx]) {
			tree[x].erase(idx);
		}
		tree[idx].clear();

		// Find the next most connected node
		connected = 0, idx = 1;
		for (ll i = 1; i <= z; i++) {
			if (connected < (ll)tree[i].size()) {
				connected = tree[i].size();
				idx = i;
			}
		}

		ans += tree[idx].size()-1;
		if(isall2)
		{
			cout<<"3"<<endl;
		}
		else{
		cout << ans << endl;
		}
	}
	
	return 0;
}




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

Code 1:
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp(x,y) make_pair(x,y)
#define inf (1e9)
#define INF (1e18)
#define eps (1e-8)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ull unsigned long long
#define int long long
#define Int __int128
typedef long long LL;
mt19937 rnd(time(0));
const int N = 2e5 + 10, M = 1e6 + 10, mod = 1e9 + 7;
int n, in[N];
vector<int> g[N];

void solve()
{
    int ans = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        g[i].clear();
        in[i] = 0;
    }
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        in[y]++;
        g[y].push_back(x);
        in[x]++;
    }
    multiset<int> s;
    for (int i = 1; i <= n; i++) {
        s.insert(in[i]);
    }
    for (int i = 1; i <= n; i++) {
        int res = 0;
        s.erase(s.find(in[i]));
        for (auto j : g[i]) {
            s.erase(s.find(in[j]));
            s.insert(in[j] - 1);
        }
        res = in[i] + *s.rbegin() - 1;
        ans = max(ans, res);
        for (auto j : g[i]) {
            s.erase(s.find(in[j] - 1));
            s.insert(in[j]);
        }
        s.insert(in[i]);
    }
    cout << ans << endl;
    return;
}

signed main()
{
	// IOS;
    // init();
	int __;
    cin >> __;
    while (__ --)
    solve();
	
	return 0;
}
//-Wl,--stack=1998244353


/*
    
*/

Code 2:
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp(x,y) make_pair(x,y)
#define inf (1e9)
#define INF (1e18)
#define eps (1e-8)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ull unsigned long long
#define int long long
#define Int __int128
typedef long long LL;
mt19937 rnd(time(0));
const int N = 2e5 + 10, M = 1e6 + 10, mod = 1e9 + 7;
int n, in[N], ver, mx = 0;
vector<int> g[N];

void dfs(int u, int fa) {
    if (g[u].size() > mx) {
        mx = g[u].size();
        ver = u;
    }
    for (int v : g[u]) {
        if (v == fa) continue;
        dfs(v, u);
    }
}

void solve()
{
    int ans = 1;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        g[i].clear();
        in[i] = 0;
    }
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        in[y]++;
        g[y].push_back(x);
        in[x]++;
    }
    mx = 0;
    for (int i = 1; i <= n; i++) {
        if (g[i].size() == 1) {
            dfs(i, -1);
            break;
        }
    }
    ans = g[ver].size();
    for (int v : g[ver]) {
        in[v]--;
    }
    mx = 0;
    for (int i = 1; i <= n; i++) {
        if (i == ver) continue;
        mx = max(mx, in[i]);
    }
    ans += mx - 1;
    cout << ans << endl;
    return;
}

signed main()
{
	// IOS;
    // init();
	int __;
    cin >> __;
    while (__ --)
    solve();
	
	return 0;
}
//-Wl,--stack=1998244353


/*
    
*/

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

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

using namespace std;

#ifndef ONLINE_JUDGE
#include "Ash.cpp"
#else
#define dbg(...)
#endif

void solve(int cs) {
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  vector<int> deg(n, 0);
  for (int i = 0; i < n - 1; i++) {
    int u, v; 
    cin >> u >> v, --u, --v;
    ++deg[u], ++deg[v];
    g[u].push_back(v);
    g[v].push_back(u);
  }
  multiset<int> ms(deg.begin(), deg.end());
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ms.erase(ms.find(deg[i]));
    for (auto &j : g[i]) {
      ms.erase(ms.find(deg[j]));
      ms.insert(deg[j] - 1);
    }
    ans = max(ans, deg[i] + *ms.rbegin() - 1);
    for (auto &j : g[i]) {
      ms.erase(ms.find(deg[j] - 1));
      ms.insert(deg[j]);
    }
    ms.insert(deg[i]);
  }
  cout << ans << "\n";
}

int32_t main() {
  ios::sync_with_stdio(!cin.tie(0));
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; ++i) {
    solve(i);
  }
  return 0;
}

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

using namespace std;

#ifndef ONLINE_JUDGE
#include "Ash.cpp"
#else
#define dbg(...)
#endif

void solve(int cs) {
  int n;
  cin >> n;
  vector<set<int>> g(n);
  vector<int> deg(n, 0);
  for (int i = 0; i < n - 1; i++) {
    int u, v; 
    cin >> u >> v, --u, --v;
    ++deg[u], ++deg[v];
    g[u].insert(v);
    g[v].insert(u);
  }
  if (n == 2) {
    cout << "0\n"; return;
  }
  int mx = *max_element(deg.begin(), deg.end());
  int who = -1;
  for (int i = 0; i < n; i++) {
    if (deg[i] == mx) {
      who = i;
    }
  }
  for (auto &them : g[who]) {
    g[them].erase(who);
  }
  g[who].clear(), deg.clear();
  for (int i = 0; i < n; i++) {
    deg[i] = g[i].size();
  }
  mx = *max_element(deg.begin(), deg.end());
  assert(deg[who] == 0);
  int second = -1;
  for (int i = 0; i < n; i++) {
    if (deg[i] == mx && i != who) {
      second = i;
    }
  }
  for (auto &them : g[second]) {
    g[them].erase(second);
  }
  g[second].clear();
  int ans = 0;
  vector<int> vis(n, false);
  auto dfs = [&](auto &&self, int node) -> void {
    vis[node] = true;
    for (auto &son : g[node]) {
      if (!vis[son]) {
        self(self, son);
      }
    }
  };
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      ++ans, dfs(dfs, i);
    }
  }
  cout << ans - 2 << "\n";
}

int32_t main() {
  ios::sync_with_stdio(!cin.tie(0));
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; ++i) {
    solve(i);
  }
  return 0;
}

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

Code 1:
#include <bits/stdc++.h>
#define ull unsigned long long
#define ul unsigned long
#define l long
#define ll long long
using namespace std;
struct vertex{
	vector<int> v;
	int degree;
};
vertex graph[200010];
int main()
{
	ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		multiset<int> dg;
		dg.clear();
		int n;
		cin>>n;
		int maxdegree=0;
		for(int i=1;i<=n;i++)
		{
			graph[i].v.clear();
			graph[i].degree=0;
		}
		for(int i=1;i<n;i++)
		{
			int u,v;
			cin>>u>>v;
			graph[u].v.push_back(v);
			graph[u].degree++;
			graph[v].v.push_back(u);
			graph[v].degree++;
			if(graph[u].degree>maxdegree)
				maxdegree=graph[u].degree;
			if(graph[v].degree>maxdegree)
				maxdegree=graph[v].degree;
		}
		for(int i=1;i<=n;i++)dg.insert(graph[i].degree);
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			dg.erase(dg.find(graph[i].degree));
			for(auto it=graph[i].v.begin();it!=graph[i].v.end();it++)
			{
				int vv=*it;
				dg.erase(dg.find(graph[vv].degree));
				dg.insert(graph[vv].degree-1);
			}
			ans=max(ans,graph[i].degree+*dg.rbegin()-1);
			for(auto it=graph[i].v.begin();it!=graph[i].v.end();it++)
			{
				int vv=*it;
				dg.erase(dg.find(graph[vv].degree-1));
				dg.insert(graph[vv].degree);
			}
			dg.insert(graph[i].degree);
		}
 		cout<<ans<<endl;
	}
	return 0;
}

Code 2:
#include <bits/stdc++.h>
#define ull unsigned long long
#define ul unsigned long
#define l long
#define ll long long
using namespace std;
struct vertex{
	vector<int> v;
	int degree;
};
vertex graph[200010];
int main()
{
	ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int maxdegree=0,maxsite;
		for(int i=1;i<=n;i++)
		{
			graph[i].v.clear();
			graph[i].degree=0;
		}
		for(int i=1;i<n;i++)
		{
			int u,v;
			cin>>u>>v;
			graph[u].v.push_back(v);
			graph[u].degree++;
			graph[v].v.push_back(u);
			graph[v].degree++;
			if(graph[u].degree>maxdegree)
			{
				maxdegree=graph[u].degree;
				maxsite=u;
			}
			if(graph[v].degree>maxdegree)
			{
				maxdegree=graph[v].degree;
				maxsite=v;
			}
		}
		int ans=1;
		ans+=maxdegree-1;
		graph[maxsite].degree=0;
		for(auto it=graph[maxsite].v.begin();it!=graph[maxsite].v.end();it++)
		{
			int vv=*it;
			graph[vv].degree--;
		}
		maxdegree=0;
		for(int i=1;i<=n;i++)
		{
			if(graph[i].degree>maxdegree)
				maxdegree=graph[i].degree;
		}
		ans+=maxdegree-1;
		cout<<ans<<endl;
	}
	return 0;
}

Answer: 1
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<unordered_set<int>> adj(n + 1);
    vector<pair<int, int>> indeg; 
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].insert(v);
        adj[v].insert(u);
    }
    for (int i = 1; i <= n; i++) {
        indeg.push_back({(int)adj[i].size(), i});
    }
    sort(indeg.rbegin(), indeg.rend()); 
    int ans = 0;
    for (int i = 0; i <n; i++) {
        int node1 = indeg[i].second;
        for (int j = i + 1; j < n; j++) {
            int node2 = indeg[j].second;
            if (node1 == node2) continue;
            if (adj[node1].count(node2)) {
                ans = max(ans, indeg[i].first + indeg[j].first - 2);
            } else { 
                ans = max(ans, indeg[i].first + indeg[j].first - 1);
                break;
            }
        }
    }

    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int T;
    cin >> T;
    while (T--) solve();
}


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

void solve() {
    int n;
    cin >> n;
    vector<unordered_set<int>> adj(n + 1);
    map<int, int, less<int>> indeg; 
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].insert(v);adj[v].insert(u);
        indeg[u]++; indeg[v]++;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (auto &[node, degree] : indeg) {
            if(node==i) continue;
            else if (adj[i].count(node) ) { 
                ans = max(ans, indeg[node] + indeg[i] - 2);
            } else { 
                ans = max(ans, indeg[node] + indeg[i] - 1);
            }
        }
    }
    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--) solve();
}


Answer: 1