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

Solution 1 (runtime: 1031 ms):
#include <bits/stdc++.h>
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;
#define int long long
const int MOD = 1e9 + 7;
const int INF = LONG_MAX >> 1;
typedef pair<char, int> pchar;
typedef pair<int, int> pint;
typedef vector<int> v;
typedef vector<pint> vpint;
typedef vector<string> vs;
typedef unordered_map<int, int> ump;
typedef map<int, int> mp;
typedef tree<int, null_type, less<int>, rb_tree_tag,  
            tree_order_statistics_node_update>  
    ordered_set;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define fl(i,n) for(int i=0;i<n;i++)
#define rl(i,m,n) for(int i=n;i>=m;i--)
#define py cout << "YES\n";
#define pn cout << "NO\n";
#define pimp cout << "IMPOSSIBLE\n";
#define vr(v) v.begin(), v.end()
#define rv(v) v.end(), v.begin()
#define debug(x) cerr << #x << " "; cerr << x << " "; cerr << endl;
int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); }
int lcm(int a, int b) { return (a / gcd(a, b) * b); }
int moduloMultiplication(int a, int b, int mod) { int res = 0; a %= mod; while (b) { if (b & 1) res = (res + a) % mod; b >>= 1; a = (a * 2) % mod; } return res; }
int powermod(int x, int y, int p) { int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; }

// Utility functions
void pv(const vector<int>& v) { for(auto x: v) cout << x << " "; cout << "\n"; }
void pap(const pair<int,int> ap[],int n) { for(int i=0; i<n; i++) cout << ap[i].first << "->" << ap[i].second << "\n"; }
void pm(const map<int,int>& m) { for(auto x: m) cout << x.first << "->" << x.second << "\n"; }
void pa(const int a[], int n) { for (int i = 0; i < n; ++i) cout << a[i] << " "; cout << "\n"; }
bool sorta(const pair<int,int>& a, const pair<int,int>& b) { return (a.second < b.second); }
bool sortd(const pair<int,int>& a, const pair<int,int>& b) { return (a.second > b.second); }

// Bits
string decToBinary(int n) { string s=""; while (n > 0) { s = to_string(n % 2) + s; n = n / 2; } return s; }
int binaryToDecimal(const string& n) { int dec_value = 0; int base = 1; int len = n.length(); for(int i = len - 1; i >= 0; i--) { if (n[i] == '1') dec_value += base; base = base * 2; } return dec_value; }

// Check functions
bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for(int i = 5; i * i <= n; i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; }
bool isPowerOfTwo(int n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); }
bool isPerfectSquare(int x) { if (x >= 0) { int sr = sqrt(x); return (sr * sr == x); } return false; }
void nl() { cout << "\n"; }
void yn(int x) { if (x == 1) cout << "YES" << "\n"; else cout << "NO" << "\n"; }
void yes() { cout << "YES" << "\n"; }
void no() { cout << "NO" << "\n"; }
void print(int x) { cout << x << "\n"; }
bool sortbysec(const pair<int,int>& a, const pair<int,int>& b) { return (a.second < b.second); }


class dsu{

    vector<int> parent,size;
  public: 

    void make_set(int n){
       for (int i = 0; i <=n; ++i)
       {
         parent.push_back(i);
         size.push_back(1);
       }
    }

    int findPar(int node){
      if(parent[node]==node) return node;
      return parent[node]=findPar(parent[node]);//path compression
    }
     

    void union_sets(int a,int b){
      a=findPar(a);
      b=findPar(b);
      if(a!=b) parent[b]=a;
    }
    
    void unionSize(int u,int v){
      int pu=findPar(u);
      int pv=findPar(v);
      if(pu==pv) return;

      if(size[pu]<size[pv]){
        parent[pu]=pv;
        size[pv]+=size[pu];
      }else{
        parent[pv]=pu;
        size[pu]+=size[pv];
      }
    }

    void maximal(int a){
      int mx=a;
      while(parent[a]!=a){
          a=parent[a];
          mx=max(mx,a);
      }
      cout<<mx<<" ";
    }

   void minimal(int a){
      int mx=a;
      while(parent[a]!=a){
          a=parent[a];
          mx=min(mx,a);
      }
      cout<<mx<<" ";
    }

    void cnt(int a){
      int cnt=1;
      while(parent[a]!=a){
          a=parent[a];
          cnt++;
      }
      cout<<cnt<<" ";
    }

    void print(int a){
       if(a==parent[a]) return;
       cout<<a<<" ";
       print(parent[a]);
    }

};


int accurateFloor(int a, int b) {
  int val = a / b;
  while (val * b > a)
    val--;
  return val;
}

bool isSubSeqRec(const string& s1, const string& s2, int m, int n)
{
    // Base Cases
    if (m == 0)
        return true;
    if (n == 0)
        return false;

    // If last characters of two strings are matching
    if (s1[m - 1] == s2[n - 1])
        return isSubSeqRec(s1, s2, m - 1, n - 1);

    // If last characters are not matching
    return isSubSeqRec(s1, s2, m, n - 1);
}

// bool check(int n,int k,int sum,int a[]){
  
// }

void dfs(int u,map<int,set<int>> mp,set<int> &vis){
  vis.insert(u);
  for(auto x: mp[u]){
    if(vis.find(x)==vis.end()){
      dfs(x,mp,vis);
    }
  }
}
signed main()
 { 
ios::sync_with_stdio(false); 
cin.tie(NULL);


     int tc;
     cin>>tc;
     while(tc--){
       int n;
       cin>>n;
       int cnt[n+1]{};
       map<int,set<int>> mp;
       set<pair<int,int>> st;
       for (int i = 1; i < n; ++i)
       {
            int a,b;
            cin>>a>>b;
            st.insert({a,b});
            st.insert({b,a});
            mp[a].insert(b);
            mp[b].insert(a);
            cnt[a]++,cnt[b]++;
       }

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

       sort(rbegin(v),rend(v));

       int ans=-1;
       for (int i = 0; i < v.size(); ++i)
       {
          for(int j=i+1; j<v.size(); ++j){
            if(mp[v[j].second].find(v[i].second)==mp[v[j].second].end()){
              ans=max(ans,v[j].first+v[i].first-1);
              break;
            }
          }
       }
       
      for(auto x: st){
        ans=max(ans,cnt[x.first]+cnt[x.second]-2);
      }

       cout<<ans<<"\n";

     }
   }



//0 0
//0 0


Solution 2 (runtime: 280 ms):
#include <bits/stdc++.h>
using namespace std;
#define print(arr) for(auto& x:arr)cout<<x<<" ";cout<<"\n"
#define watch(x) cout<<#x<<"="<<x<<"\n"
#define all(x) x.begin(), x.end()
#define sz(x) ((int) x.size())
typedef pair<int, int> ii;
typedef long long ll;

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

const int maxn = 2e5+5;
vector<int> adj[maxn];
int dp[maxn][3]; // 10,11,2

void dfs(int u, int p=-1){
	dp[u][0]=dp[u][1]=dp[u][2]=0;
	vector<int> a1,a0;
	for(int v:adj[u]){
		if(v==p)continue;
		dfs(v,u);
		a0.push_back(dp[v][0]);
		a1.push_back(dp[v][1]);
		dp[u][2]=max(dp[u][2], dp[v][2]);
	}
	sort(a1.rbegin(), a1.rend());
	sort(a0.rbegin(), a0.rend());
	dp[u][1]=sz(adj[u]);
	if(sz(a1)){
		dp[u][0]=max({dp[u][0], a1[0], a0[0]});
		dp[u][1]=max({dp[u][1], a1[0], a0[0]});
		dp[u][2]=max({dp[u][2], a0[0]+sz(adj[u])-1, a1[0]+sz(adj[u])-2});
		if(sz(a1)>1)dp[u][2]=max(dp[u][2], a1[0]+a1[1]-1);
	}
}

void solve(){
	int n;cin>>n;
	for(int i=0;i<n;++i)adj[i].clear();
	for(int a,b,i=0;i<n-1;++i){
		cin>>a>>b;a--;b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(0);
	cout<<dp[0][2]<<"\n";
}