Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;   
using namespace std;
typedef long long ll;

//
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
typedef tree<pair<ll,ll>, null_type, less<pair<ll,ll>>, rb_tree_tag, tree_order_statistics_node_update> ordered_pset;
//
#define ld long double
#define pb push_back
/* You may enter other macros defined by you */
#define vi vector<ll>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rep1(i,a,b) for(ll i=a;i<=b;i++)
#define ff first
#define ss second
#define pll pair<ll, ll>
#define vpll vector<pair<ll,ll> >
#define vvl vector<vector<ll> >
#define printv(a) for(auto x: a){cout<<x<<" ";}cout<<"\n";
#define input(a,n) rep(i,0,n)cin>>a[i];
#define input1(a,n) rep1(i,1,n)cin>>a[i];
#define mp make_pair

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define minv(a) *min_element(all(a))
#define maxv(a) *max_element(all(a))
void yes() { cout<<"YES\n"; }
void no() { cout<<"NO\n"; }
// random number generator
mt19937 rnd(593); 

  
const ll mod = 1e9+7;
//const ll mod = 998244353;
const ld PI = acos(-1);
const ld EPS = 1e-9;
const ll INF = 1e18;
const ll N=3e5+10;
inline int add(int x, int y) { int ret = x + y; if (ret >= mod) ret -= mod; return ret; }
inline int sub(int x, int y) { int ret = (x - y); if (ret < 0) ret += mod; return ret; }
inline int mul(int x, int y) { int ret = (1ll * x * y) % mod; return ret; }
int exp(int a, int b) { int ret = 1; while (b) { if (b & 1) ret = mul(ret, a); a = mul(a, a); b >>= 1;} return ret; }
int dv(int a, int b) { int ret = exp(b, mod - 2); ret = mul(ret, a); return ret; }

vvl adj;

multiset<ll> s;

ll ans=-1;

void dfs(ll u,ll e){
  s.erase(s.find(adj[u].size()));
  if(u!=e){
    s.erase(s.find(adj[e].size()));
  }
  for(auto x: adj[u]){
    if(x==e)continue;
    ans=max(ans,adj[u].size()+adj[x].size()-2ll);
    s.erase(s.find(adj[x].size()));
  }
  if(s.size()!=0){
    ans=max(ans,adj[u].size()+*s.rbegin()-1ll);
  }

  s.insert(adj[u].size());
  if(u!=e)
  s.insert(adj[e].size());
  for(auto x: adj[u]){
    if(x==e)continue;
    s.insert(adj[x].size());
  }
  for(auto x: adj[u]){
    if(x==e)continue;
    dfs(x,u);
  }
}


void solve(){   
  ll n;cin>>n;
  s.clear();
  adj.assign(n+1,vi(0));
  rep(i,0,n-1){
    ll u,v;cin>>u>>v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  rep1(i,1,n){
    s.insert(adj[i].size());
  }
  dfs(1,1);
  cout<<ans<<endl;

}
    
int main(){

  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  int t;
  cin>>t;while(t--)
    solve();
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";

  return 0; 
}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;   
using namespace std;
typedef long long ll;

//
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
typedef tree<pair<ll,ll>, null_type, less<pair<ll,ll>>, rb_tree_tag, tree_order_statistics_node_update> ordered_pset;
//
#define ld long double
#define pb push_back
/* You may enter other macros defined by you */
#define vi vector<ll>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rep1(i,a,b) for(ll i=a;i<=b;i++)
#define ff first
#define ss second
#define pll pair<ll, ll>
#define vpll vector<pair<ll,ll> >
#define vvl vector<vector<ll> >
#define printv(a) for(auto x: a){cout<<x<<" ";}cout<<"\n";
#define input(a,n) rep(i,0,n)cin>>a[i];
#define input1(a,n) rep1(i,1,n)cin>>a[i];
#define mp make_pair

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define minv(a) *min_element(all(a))
#define maxv(a) *max_element(all(a))
void yes() { cout<<"YES\n"; }
void no() { cout<<"NO\n"; }
// random number generator
mt19937 rnd(593); 

  
const ll mod = 1e9+7;
//const ll mod = 998244353;
const ld PI = acos(-1);
const ld EPS = 1e-9;
const ll INF = 1e18;
const ll N=3e5+10;
inline int add(int x, int y) { int ret = x + y; if (ret >= mod) ret -= mod; return ret; }
inline int sub(int x, int y) { int ret = (x - y); if (ret < 0) ret += mod; return ret; }
inline int mul(int x, int y) { int ret = (1ll * x * y) % mod; return ret; }
int exp(int a, int b) { int ret = 1; while (b) { if (b & 1) ret = mul(ret, a); a = mul(a, a); b >>= 1;} return ret; }
int dv(int a, int b) { int ret = exp(b, mod - 2); ret = mul(ret, a); return ret; }

vvl adj;

multiset<ll> s;

ll ans=-1;

void dfs(ll u,ll e){
  s.erase(s.find(adj[u].size()));
  if(u!=e){
    s.erase(s.find(adj[e].size()));
  }
  for(auto x: adj[u]){
    if(x==e)continue;
    ans=max(ans,adj[u].size()+adj[x].size()-2ll);
    s.erase(s.find(adj[x].size()));
  }
  if(s.size()!=0){
    ans=max(ans,adj[u].size()+*s.rbegin()-1ll);
  }

  s.insert(adj[u].size());
  if(u!=e)
  s.insert(adj[e].size());
  for(auto x: adj[u]){
    if(x==e)continue;
    s.insert(adj[x].size());
  }
  for(auto x: adj[u]){
    if(x==e)continue;
    dfs(x,u);
  }
}


void solve(ll t){   
  ll n;cin>>n;
  s.clear();
  adj.assign(n+1,vi(0));
  rep(i,0,n-1){
    ll u,v;cin>>u>>v;
    adj[u].pb(v);
    adj[v].pb(u);
    if(t==41){
      cout<<u<<" "<<v<<endl;
    }
  }
  rep1(i,1,n){
    s.insert(adj[i].size());
  }
  dfs(1,1);
  cout<<ans<<endl;

}
    
int main(){

  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  int t;
  cin>>t;
  ll tt=t;
  while(t--)
    solve(tt-t);
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";

  return 0; 
}