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

Code 1:
#include <bits/stdc++.h>
using namespace std;
#define Sort(a) sort(a.begin(),a.end());
#define all(a) a.begin(),a.end()
#define pb(x) push_back(x)
#define printvec(v)  for(auto x : v) cout<<x<<' ';cout<<endl;
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(__null);
#define input(a) {for(auto &x : a) cin>>x;}
 
typedef long long ll;
#define int long long int
const int mod=1e9+7;
const int N=2e5+7;
vector<int> graph[N];
vector<int> vis(N,0);
 struct val{
    int self=0,child=0;
 };
void solve(){
 
              //Start writing from here
              int n;cin>>n;
              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 best=0;
              vector<int> deg(n+1,0);
              for(int i=1;i<=n;i++) deg[i]=tree[i].size();
              multiset<int> mst;
              for(auto x: deg)  mst.insert(x);
              for(int node=1;node<=n;node++){
                   //if remove this vertex
                   //choose to remove the second vertex that is adj
                   mst.erase(mst.find(deg[node]));
                   for(auto x : tree[node]){
                        best=max(best,deg[node]+deg[x]-2);
                   } 
                   //choose to remove the vertex not adj
                   vector<int> temp(1,deg[node]);
                   for(auto x : tree[node]){
                    temp.push_back(deg[x]);
                    mst.erase(mst.find(deg[x]));
                   }
                   best=max(best,*prev(mst.end())+deg[node]-1);
                    for(auto x: temp)   mst.insert(x);
              }
              cout<<best<<endl;
              

              


}
signed main()
{
  fastio()
    int t=1;
       //comment below line for single testcase
    cin >> t;
    while (t--)
    {
        
         solve();
    }
    return 0;
}

Code 2:
#include <bits/stdc++.h>
using namespace std;
#define Sort(a) sort(a.begin(),a.end());
#define all(a) a.begin(),a.end()
#define pb(x) push_back(x)
#define printvec(v)  for(auto x : v) cout<<x<<' ';cout<<endl;
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(__null);
#define input(a) {for(auto &x : a) cin>>x;}
 
typedef long long ll;
#define int long long int
const int mod=1e9+7;
const int N=2e5+7;
vector<int> graph[N];
vector<int> vis(N,0);
 struct val{
    int self=0,child=0;
 };
void solve(){
 
              //Start writing from here
              int n;cin>>n;
              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 best=0;
              vector<val> temp(n+1);
              function<int(int,int)> dfs1=[&](int node,int par)->int {
                int total=0;
                for(auto child : tree[node]){
                    if(child !=par){
                        total=max(dfs1(child,node),total);
                    }
                }
                temp[node].child=total;
                total=max(total,(int)tree[node].size());
                temp[node].self=total;
                return total;
              };
              dfs1(1,0);
              function<void(int,int)> dfs=[&](int node,int par)->void {
                for(auto child : tree[node]){
                    if(child !=par){
                        best=max(best,(int)(temp[child].child+tree[node].size()-1));
                        best=max(best,(int)(tree[node].size()+tree[child].size()-2));
                        dfs(child,node);
                    }
                }
              };
              dfs(1,0);
              //non adjacent
              cout<<best<<endl;
              

              


}
signed main()
{
  fastio()
    int t=1;
       //comment below line for single testcase
    cin >> t;
    while (t--)
    {
        
         solve();
    }
    return 0;
}

Answer: 1