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

Code 1:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<long long> vll;
#define reg(i,a,b) for(ll i=a;i<b;i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

void solve() {
    ll n;
    cin>>n;
    vector<vi> adjL(n);
    map<ll, ll> f;

    reg(i,0,n - 1){
        ll u, v;
        cin>>u>>v;
        u--;
        v--;
        adjL[u].push_back(v);
        adjL[v].push_back(u);
        f[u]++;
        f[v]++;
    }
    set<pair<ll,ll>> cont;
    for(auto it : f) cont.insert({it.second, it.first});
    ll node = (*cont.rbegin()).second;
    ll fre = f[node];
    ll ans = fre;
    cont.erase({fre, node});
    for(auto nxt: adjL[node]){
        cont.erase({f[nxt], nxt});
        f[nxt]--;
        cont.insert({f[nxt], nxt});
    }

    node =  (*cont.rbegin()).second;
    fre = f[node];
    ans += fre;
    ans--;
    cout<<ans<<endl;
    return;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll ts = 1;
    cin>>ts;
    while (ts--) {
        solve();
    }
    return 0;
}


Code 2:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<long long> vll;
#define reg(i,a,b) for(ll i=a;i<b;i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

ll s(set<pair<ll,ll>> &cont, ll node, map<ll, ll> &f, vector<vi> &adjL){
    ll ans = 0;
    ll fre = f[node];
    ans = fre;
    cont.erase({fre, node});
    for(auto nxt: adjL[node]){
        cont.erase({f[nxt], nxt});
        f[nxt]--;
        cont.insert({f[nxt], nxt});
    }

    ans += (*cont.rbegin()).first;
    ans--;
    for(auto nxt: adjL[node]){
        cont.erase({f[nxt], nxt});
        f[nxt]++;
        cont.insert({f[nxt], nxt});
    }
    return ans;
}

void solve() {
    ll n;
    cin>>n;
    vector<vi> adjL(n);
    map<ll, ll> f;

    reg(i,0,n - 1){
        ll u, v;
        cin>>u>>v;
        u--;
        v--;
        adjL[u].push_back(v);
        adjL[v].push_back(u);
        f[u]++;
        f[v]++;
    }
    set<pair<ll,ll>> cont;
    for(auto it : f) cont.insert({it.second, it.first});
    //ll node = (*cont.rbegin()).second;
    ll fre = (*cont.rbegin()).first;
    // cout<<fre<<endl;
    ll ans = -1;
    vector<pair<ll, ll>> temp(cont.begin(), cont.end());
    for(ll i = temp.size() - 1; i>=0; i--){
        if(temp[i].first < fre) break;
        // cout<<"chala"<<endl;
        // cout<<s(cont, temp[i].second, f, adjL)<<endl;
        ans = max(ans,s(cont, temp[i].second, f, adjL));
    }

    cout<<ans<<endl;
    return;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll ts = 1;
    cin>>ts;
    while (ts--) {
        solve();
    }
    return 0;
}


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

Code 1:
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <set>

using namespace std;
typedef long long ll;

void solve()
{
    int n;
    cin>>n;
    vector<vector<int>> g(n+1);
    vector<int> ind(n+1,0);

    for(int i=0,u,v;i<n-1;++i)
    {
        cin>>u>>v;
        g[u].push_back(v),g[v].push_back(u);
        ind[u]++,ind[v]++;
    }

    int tag=0,cnt=0;
    for(int i=1;i<=n;++i)
        if(ind[i]>ind[tag]) tag=i,cnt=1;
        else if(ind[i]==ind[tag]) cnt++;

    if(cnt>=3) cout<<ind[tag]*2-1<<'\n';
    else if(cnt==2)
    {
        bool is = false;
        for(int i=0;i<(int)g[tag].size();++i)
            if(ind[g[tag][i]]==ind[tag]) is = true;
        if(is) cout<< ind[tag]*2-2<< '\n';
        else cout<< ind[tag]*2-1<< '\n';
    }
    else
    {
        for(int i=0;i<(int)g[tag].size();++i) ind[g[tag][i]]--;
        int mx=0;
        for(int i=1;i<=n;++i)
            if(i!=tag) mx = max(mx,ind[i]);
        cout<< ind[tag]+mx-1<< '\n';
    }

}

int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //fflush(stdout);

    int t;
    cin>>t;
    while(t--) solve();

    return 0;
}


Code 2:
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <set>

using namespace std;
typedef long long ll;

void solve()
{
    int n;
    cin>>n;
    vector<vector<int>> g(n+1);
    vector<int> ind(n+1,0);

    for(int i=0,u,v;i<n-1;++i)
    {
        cin>>u>>v;
        g[u].push_back(v),g[v].push_back(u);
        ind[u]++,ind[v]++;
    }

    int tag=0,cnt=0;
    for(int i=1;i<=n;++i)
        if(ind[i]>ind[tag]) tag=i,cnt=1;
        else if(ind[i]==ind[tag]) cnt++;

    if(cnt>=3) cout<<ind[tag]*2-1<<'\n';
    else if(cnt==2)
    {
        bool is = false;
        for(int i=0;i<(int)g[tag][i];++i)
            if(ind[g[tag][i]]==ind[tag]) is = true;
        if(is) cout<< ind[tag]*2-2<< '\n';
        else cout<< ind[tag]*2-1<< '\n';
    }
    else
    {
        for(int i=0;i<(int)g[tag].size();++i) ind[g[tag][i]]--;
        int mx=0;
        for(int i=1;i<=n;++i)
            if(i!=tag) mx = max(mx,ind[i]);
        cout<< ind[tag]+mx-1<< '\n';
    }

}

int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //fflush(stdout);

    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 ll long long
#define pb push_back
bool cmp(pair<ll,ll>a,pair<ll,ll>b){
return a.second>b.second;
}
void solve(ll tc)
{
    ll n,i,x,y,s=0,qq,ww,s2=0,ee,m;
    cin>>n;
    vector<pair<ll,ll>>p(n+9);
    for(i=1;i<=n;i++) p[i].first=i,p[i].second=0;
    vector<set<ll>> ps(n+3);
    for(i=1;i<n;i++){
        cin>>x>>y;
        p[x].second++;
        p[y].second++;
        ps[x].insert(y);
        ps[y].insert(x);
    }
    sort(p.begin(),p.end(),cmp);

    s=p[0].second;
    qq=p[0].first;
    m=min(n,s+2);
    for(i=1;i<m;i++){
        ww=p[i].second;
        ee=p[i].first;
        if(ps[ee].count(qq)) ww--;
        s2=max(s2,ww);

    }
    ll ans=s+s2-1;
    s=p[1].second;
    qq=p[1].first;
    m=min(n,s+2);
    for(i=2;i<m;i++){
        ww=p[i].second;
        ee=p[i].first;
        if(ps[ee].count(qq)) ww--;
        s2=max(s2,ww);

    }
    ans=max(ans,s+s2-1);
    cout<<ans<<endl;






}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tc=1;
    cin>>tc;
    for(ll i=1; i<=tc; i++)
    {
        solve(i);
    }
}


Code 2:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
bool cmp(pair<ll,ll>a,pair<ll,ll>b){
return a.second>b.second;
}
void solve(ll tc)
{
    ll n,i,x,y,s=0,qq,ww,s2=0,ee;
    cin>>n;
    vector<pair<ll,ll>>p(n+3);
    for(i=1;i<=n;i++) p[i].first=i,p[i].second=0;
    vector<set<ll>> ps(n+3);
    for(i=1;i<n;i++){
        cin>>x>>y;
        p[x].second++;
        p[y].second++;
        ps[x].insert(y);
        ps[y].insert(x);
    }
    sort(p.begin(),p.end(),cmp);

    s=p[0].second;
    qq=p[0].first;
    for(i=1;i<n;i++){
        ww=p[i].second;
        ee=p[i].first;
        if(ps[ee].count(qq)) ww--;
        s2=max(s2,ww);

    }
    cout<<s+s2-1<<endl;






}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tc=1;
    cin>>tc;
    for(ll i=1; i<=tc; i++)
    {
        solve(i);
    }
}


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

Code 1:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define pb push_back
#define all(x) (x).begin(), (x).end()

void solve() {
    int n;
    cin >> n;
    vi adj[n + 1];
    vi f(n + 1, 0);

    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
        f[u]++;
        f[v]++;
    }
    
    if (n <= 2) {
        cout << 0 << endl;
        return;
    }
    if (n == 3) {
        cout << 1 << endl;
        return;
    }

    int ind = max_element(all(f)) - f.begin();
    int ans = f[ind];

    int ct = 0;
    for (auto it : adj[ind]) {    
        if (f[it] == ans)
            ct++;
    }

    if (ct >= 2) {
        cout << (ans + ans - 1) << endl;
        return;
    }

    for (auto it : adj[ind]) { 
        f[it]--;
    }
    f[ind] = 0;

    int ind1 = max_element(all(f)) - f.begin();
    ans += (f[ind1] - 1);
    cout << ans << endl;
}

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


Code 2:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define pb push_back
#define all(x) (x).begin(), (x).end()

void solve() {
    int n;
    cin >> n;
    vi adj[n + 1];
    vi f(n + 1, 0);

    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
        f[u]++;
        f[v]++;
    }
    f[1]=1;

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

    int mx = max_element(f.begin() + 1, f.end()) - f.begin();
    // cout<<mx<<endl;
    vi deg(n + 1);
    for (int i = 1; i <= n; i++) {
        deg[i] = adj[i].size();
    }

    int temp = deg[mx];

    for (auto ele : adj[mx]) {
        deg[ele]--;
    }
   

    deg[mx] = 0;
    //  for(int i=1;i<=n;i++ )
    // {
    //     cout<<i<<"->"<<deg[i]<<endl;
    // }
    int sc_mx = max_element(deg.begin() + 1, deg.end()) - deg.begin();
    // cout<<sc_mx<<endl;

    cout << deg[sc_mx] + temp - 1 << endl;
}

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


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

Code 1:
// Name-Anshu Raj Jaiswal
// Scholar id-2212172
#include<bits/stdc++.h>
using namespace std; 
typedef long long ll;
typedef long double ld;
#define pb push_back
#define ff first
#define ss second
#define mpq priority_queue<ll,vector<ll>,greater<ll>>
#define mpqp priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> 
#define Mpq priority_queue<ll>
#define Mpqp priority_queue<pair<ll,ll>>
#define all(x) (x).begin(), (x).end()
const ll MOD=998244353;
const ll N=200000;
ll ceil(ll a,ll b){return (a+b-1)/b;}
void solve()
{         
 ll n;
 cin>>n;
 vector<ll>temp(n+1,0);
 vector<ll>temp2(n+1,0);
 vector<pair<ll,ll>>arr(n-1);
 map<ll,ll>my;
 for(ll i=0;i<n-1;i++)
 {
  ll u,v;
  cin>>u>>v;
  temp[u]++;temp[v]++;
  arr[i]={u,v};
 } 
 ll ans=0;
 ll in=-1;
 for(ll i=1;i<=n;i++)
 {
   if(temp[i]>ans)
   {
    ans=temp[i];
    in=i;
   }
   my[temp[i]]++;
 }     
 if(my[ans]>2)
 {
  cout<<2*ans-1<<endl;
  return;
 }  
 ans--;
 for(ll i=0;i<n-1;i++) 
 {      
  ll u=arr[i].ff,v=arr[i].ss;       
  if(u==in || v==in)              
  continue;
  temp2[u]++;temp2[v]++;
 }
 ll pi=0;
 for(ll i=1;i<=n;i++)
 pi=max(pi,temp2[i]); 
 ans+=pi;
 cout<<ans<<endl;
} 
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);cout.tie(NULL);
  ll t;
  cin>>t;
  while(t--)
  {        
	  solve();
  }
  return 0;
} 

Code 2:
// Name-Anshu Raj Jaiswal
// Scholar id-2212172
#include<bits/stdc++.h>
using namespace std; 
typedef long long ll;
typedef long double ld;
#define pb push_back
#define ff first
#define ss second
#define mpq priority_queue<ll,vector<ll>,greater<ll>>
#define mpqp priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> 
#define Mpq priority_queue<ll>
#define Mpqp priority_queue<pair<ll,ll>>
#define all(x) (x).begin(), (x).end()
const ll MOD=998244353;
const ll N=200000;
ll ceil(ll a,ll b){return (a+b-1)/b;} 
ll pi=0; 
void dfs(ll node,vector<ll>&vis,vector<ll>&temp2,vector<vector<ll>>&adj)
{
  vis[node]=1;
  for(auto it:adj[node])
  {
    if(!vis[it])
    {
      temp2[node]++;
      temp2[it]++;
      dfs(it,vis,temp2,adj);
    }
  }
  return;
}
void solve()
{         
 ll n;
 cin>>n;
 vector<vector<ll>>adj(n+1);
 vector<ll>temp(n+1,0);
 vector<pair<ll,ll>>arr(n-1);
 for(ll i=0;i<n-1;i++)
 {
  ll u,v;
  cin>>u>>v;
  temp[u]++;temp[v]++;
  arr[i]={u,v};
 } 
 ll ans=0;
 ll in=-1;
 for(int i=0;i<n;i++)
 {
   if(temp[i]>ans)
   {
    ans=temp[i];
    in=i;
   }
 }
 ans--;
 for(ll i=0;i<n-1;i++)
 {
  ll u=arr[i].ff,v=arr[i].ss;
  if(u==in || v==in)continue;
  adj[u].pb(v);adj[v].pb(u);
 }
 vector<ll>vis(n+1,0);
 vector<ll>temp2(n+1,0);
 for(ll i=0;i<n;i++)
 {
   if(!vis[i])
   {
      dfs(i,vis,temp2,adj);
   }
 }
 ll pi=0;
 for(ll i=0;i<n;i++)
 {
   pi=max(pi,temp2[i]);
 }
 ans+=pi;
 cout<<ans<<endl;
} 
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);cout.tie(NULL);
  ll t;
  cin>>t;
  while(t--)
  {        
	  solve();
  }
  return 0;
} 

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

Code 1:
#include <bits/stdc++.h>
using i64 = long long;
#ifndef ATCODER
consteval auto operator"" _f(const char* str, std::size_t len) {
    return [fmt = std::string_view(str, len)](auto&&... args) {
        return std::vformat(fmt, std::make_format_args(args...));
    };
}
#endif

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

    std::vector<std::vector<int>> adj(n);
    std::vector<int> max(n);
    for (int i = 1, u, v;i < n;i ++) {
        std::cin >> u >> v;
        u --, v --;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    int ans = 0;
    auto dfs = [&](this auto &&self, int ind, int p) -> void{
        int mx1 = 0, mx2 = 0;
        for (auto &to : adj[ind]) {
            if (to == p) continue;
            self(to, ind);
            max[ind] = std::max(max[ind], std::max(max[to], int(adj[to].size())));
            ans = std::max(ans, int(adj[ind].size() + adj[to].size()) - 2);
            ans = std::max(ans, int(adj[ind].size()) + max[to] - 1);
            int smx = std::max(max[to], int(adj[to].size()));
            if (mx1 <= smx) {
                mx2 = mx1;
                mx1 = smx;
            } else if (mx2 < smx) {
                mx2 = smx;
            }
        }
        ans = std::max(ans, mx1 + mx2 - 1);
    };
    dfs(0, -1);

    std::cout << ans << "\n";
}
int32_t main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t = 1;
    std::cin >> t;

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

Code 2:
#include <bits/stdc++.h>
using i64 = long long;
#ifndef ATCODER
consteval auto operator"" _f(const char* str, std::size_t len) {
    return [fmt = std::string_view(str, len)](auto&&... args) {
        return std::vformat(fmt, std::make_format_args(args...));
    };
}
#endif

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

    std::vector<std::vector<int>> adj(n);
    std::vector<std::array<int, 2>> a(n);
    for (int i = 1, u, v;i < n;i ++) {
        std::cin >> u >> v;
        u --, v --;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 0;i < n;i ++) {
        a[i] = {int(adj[i].size()), i};
    }

    std::sort(a.begin(), a.end());
    auto [d, id] = a.back();
    a.pop_back();
    std::sort(adj[id].begin(), adj[id].end());

    int ans = 0;
    while (a.size()) {
        int to = a.back()[1];
        if (auto it = std::lower_bound(adj[id].begin(), adj[id].end(), a.back()[1]); it != adj[id].end() and *it == a.back()[1]) {
            ans = std::max(ans, d + int(adj[to].size()) - 2);
        } else {
            ans = std::max(ans, d + int(adj[to].size()) - 1);
        }
        a.pop_back();
    }
    std::cout << ans << "\n";
}
int32_t main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t = 1;
    std::cin >> t;

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

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

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        ll n;
        cin >> n;
        vector<vector<ll>> adj(n);
        set<pair<ll, ll>> s;

        for (int i = 0; i < n - 1; i++) { 
            ll a, b;
            cin >> a >> b;
            a--; 
            b--;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }

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

        for (int i = 0; i < n; i++) {
            s.insert({adj[i].size(), i});
        }

        int maxi = INT_MIN;

        for (int i = 0; i < n; i++) {
            
             s.erase({adj[i].size(),i});
            for (auto x : adj[i]) {
                maxi = max(maxi, (int)(adj[i].size() + adj[x].size() - 1));
                s.erase({adj[x].size(), x});
            }

            if (!s.empty()) {
                auto maxElement = *s.rbegin(); 
                maxi = max(maxi, (int)(adj[i].size() + maxElement.first));
            }
            for (auto x : adj[i]) {
               
                s.insert({adj[x].size(), x});
            }
            s.insert({adj[i].size(),i});
            
           
        }

       cout << maxi - 1 << endl;
    }

    return 0;
}


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

ll f(vector<vector<ll>>& adj, ll node, ll count, ll n, ll pre) {
    if (count == 0) {
        return 0;
    }
    if (node >= n) {
        return 0;
    }

    ll rem = 0, notrem = 0;

    if (pre == -1) {
        rem = adj[node].size() + f(adj, node + 1, count - 1, n, node);
    } else {
        rem = adj[node].size();
        if (find(adj[node].begin(), adj[node].end(), pre) != adj[node].end()) {
            rem--;
        }
        rem += f(adj, node + 1, count - 1, n, node);
    }

    notrem = f(adj, node + 1, count, n, pre);
    return max(rem, notrem);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        ll n;
        cin >> n;
        vector<vector<ll>> adj(n);

        for (int i = 0; i < n - 1; i++) { 
            ll a, b;
            cin >> a >> b;
            a--;
            b--;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }

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

        cout << f(adj, 0, 2, n, -1)-1 << endl;
    }

    return 0;
}


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

Code 1:
#include <bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pii pair<int,int>
#define ll long long int
#define SIZE 200005
#define all(v) v.begin(), v.end()
#define pb push_back
#define MOD 1000000007
#define INF 1000000007
using namespace std;

int n , ma[SIZE] , val[SIZE] , ans;
vector<int> g[SIZE] , child[SIZE];

void init() {
    ans=0;
    for (int i = 1; i <= n; i++) {
        g[i].clear();
        child[i].clear();
        ma[i] = 0;
    }
}

void dfs(int here ,int pa) {
    val[here] = 1;
    for (auto t : g[here]) {
        if (t == pa) continue;
        val[here]++;
        child[here].pb(t);
        dfs(t , here);
    }
}

void solve(int here) {
    for (auto t : child[here]) solve(t);
    vector<int> tmp;
    for (auto t : child[here]) tmp.pb(ma[t]);
    sort(all(tmp));
    reverse(all(tmp));
    if (tmp.size() >= 2) ans = max(ans , tmp[0]+tmp[1]-1);
    for (auto t : child[here]) {
        ans = max(ans , ma[t]+val[here]-1-((here==1))-(child[t].size()==0));
    }
    ma[here] = max(val[here] , (tmp.size() > 0 ? tmp[0] : -1));
}

void solve() {
    cin >> n;
    for (int i = 1; i <= n-1; i++) {
        int u , v; cin >> u >> v;
        g[u].pb(v); g[v].pb(u);
    }

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

    dfs(1 , -1);
    val[1]--;
    //for (int i = 1; i <= n; i++) printf("%d " , val[i]);
    //printf("\n");
    solve(1);

    cout << ans << "\n";
}

int main() {
    FastIO;

    int t; cin >> t;
    while(t--) {
        init();
        solve();
    }

    return 0;
}

Code 2:
#include <bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pii pair<int,int>
#define ll long long int
#define SIZE 200005
#define all(v) v.begin(), v.end()
#define pb push_back
#define MOD 1000000007
#define INF 1000000007
using namespace std;

int n , ma1[SIZE] , deg[SIZE] , ans , ma2[SIZE];
vector<int> g[SIZE] , child[SIZE];

void init() {
    ans=0;
    for (int i = 1; i <= n; i++) {
        g[i].clear();
        child[i].clear();
        ma1[i] = ma2[i] = deg[i] = 0;
    }
}

void dfs(int here ,int pa) {
    multiset<int> st1 , st2;
    for (auto t : g[here]) {
        if (t == pa) continue;
        ans = max(ans , deg[here]+deg[t]-2);
        dfs(t , here);
        ma2[here] = max(ma2[here] , ma1[t]);
        st1.insert(-ma1[t]);
        st2.insert(ma2[t]);
    }
    if (st2.size() > 0) ans = max(ans , deg[here]+(*st2.rbegin())-1);
    if (st1.size() >= 2) {
        int ret=0;
        auto it = st1.begin();
        ret += -(*it);
        it++;
        ret += -(*it); ret--;
        ans = max(ans , ret);
    }
    ma1[here] = max(deg[here] , ma2[here]);
}

void solve() {
    cin >> n;
    for (int i = 1; i <= n-1; i++) {
        int u , v; cin >> u >> v;
        g[u].pb(v); g[v].pb(u);
        deg[u]++; deg[v]++;
    }

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

    dfs(1 , -1);

    cout << ans << "\n";
}

int main() {
    FastIO;

    int t; cin >> t;
    while(t--) {
        init();
        solve();
    }

    return 0;
}

Answer: 2