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

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i, j, n) for(ll i = j; i <n; i++)
#define pb push_back
#define mod 1000000007
#define up upper_bound
#define lb lower_bound
#define all(v) v.begin(), v.end()
#define srt(v) sort(all(v))
typedef vector<int>vi;
typedef vector<vector<int>>vvi;
typedef vector<long long>vll;
typedef vector<vector<long long>>vvll; 



ll binpow(ll a,ll b, ll m)
{
if(b==0)
{
    return 1;
}
if(b%2)
{
    return (a*binpow(a,b-1, m))%m;
}
return binpow((a*a)%m,b/2, m);
}
void vInp(auto & v){
    for(auto & it : v)cin>>it;
}


void vvInp(auto & v){
    for(auto & it : v){
        for(auto & it2 : it)cin>>it2;
    }
}

int solve(){
    int n;
    cin>>n;
    vector<int>indegree(n);
    vector<vector<int>>mp(n);
    
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        u--;v--;
        indegree[u]++;
        indegree[v]++;
        mp[u].pb(v);
        mp[v].pb(u);
    }
    int maxi=0;
    int node=-1;
    for(int  i=0;i<n;i++){
        if(indegree[i]>maxi){
            maxi=indegree[i];
            node=i;
        }
    }
    indegree[node]-=maxi;
    map<int,int>mp1;
    for(auto it:mp[node]){
            indegree[it]--;
            mp1[it]++;
    }
    int ans=0;
    int mx=0;
    int nd=-1;
    for(int i=0;i<n;i++){
        if(indegree[i]>=mx){
            mx=indegree[i];
            if(mp1.find(i)==mp1.end()){
                ans=max(ans,maxi+mx-1);
            }
            else{
                ans=max(ans,maxi+mx-2);
            }
        }
    }
    return ans;

    

}

int main(){
int t;
cin>>t;
while(t--){
    cout<<solve()<<endl;
}
     return 0;
}
    






/*   /\/\ 
*   (= ..) 
*   / >  \> 
*/

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i, j, n) for(ll i = j; i <n; i++)
#define pb push_back
#define mod 1000000007
#define up upper_bound
#define lb lower_bound
#define all(v) v.begin(), v.end()
#define srt(v) sort(all(v))
typedef vector<int>vi;
typedef vector<vector<int>>vvi;
typedef vector<long long>vll;
typedef vector<vector<long long>>vvll; 



ll binpow(ll a,ll b, ll m)
{
if(b==0)
{
    return 1;
}
if(b%2)
{
    return (a*binpow(a,b-1, m))%m;
}
return binpow((a*a)%m,b/2, m);
}
void vInp(auto & v){
    for(auto & it : v)cin>>it;
}


void vvInp(auto & v){
    for(auto & it : v){
        for(auto & it2 : it)cin>>it2;
    }
}
int dfs(vector<int>&indegree,int maxi,vector<vector<int>>&adj){
    queue<int>q;
    int n=indegree.size();
    for(int i=0;i<n;i++){
        if(indegree[i]==1)q.push(i);
    }
    vector<int>vis(n,0);
    while(!q.empty()){
        int node=q.front();
        q.pop();
        vis[node]=1;
        if(indegree[node]==maxi)return node;
        for(auto it:adj[node]){
            if(!vis[it]){
                q.push(it);
            }
        }
    }
    return 0;
}

int solve(){
    int n;
    cin>>n;
    vector<int>indegree(n);
    vector<vector<int>>mp(n);
    
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        u--;v--;
        indegree[u]++;
        indegree[v]++;
        mp[u].pb(v);
        mp[v].pb(u);
    }
    int maxi=0;
    int node=-1;
    for(int  i=0;i<n;i++){
        if(indegree[i]>maxi){
            maxi=indegree[i];
        }
    }
    node=dfs(indegree,maxi,mp);
    indegree[node]-=maxi;
    map<int,int>mp1;
    for(auto it:mp[node]){
            indegree[it]--;
            mp1[it]++;
    }
    int ans=0;
    int mx=0;
    int nd=-1;
    
    for(int i=0;i<n;i++){
        if(indegree[i]>=mx){
            mx=indegree[i];
            if(mp1.find(i)==mp1.end()){
                ans=max(ans,maxi+mx-1);
            }
            else{
                ans=max(ans,maxi+mx-2);
            }
        }
    }
    return ans;

    

}

int main(){
int t;
cin>>t;
while(t--){
    cout<<solve()<<endl;
}
     return 0;
}
    






/*   /\/\ 
*   (= ..) 
*   / >  \> 
*/