Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define mod 1000000007

vector<bool> is_prime;

void sieve(ll n) {
    is_prime.assign(n + 1, true);
    is_prime[0] = is_prime[1] = false;
    for (ll i = 2; i * i <= n; i++) {
        if (is_prime[i]) {
            for (ll j = i * i; j <= n; j += i)
                is_prime[j] = false;
        }
    }
}

ll lcm(ll a, ll b) {
    return (a / __gcd(a, b)) * b;
}

/*
1. Read question carefully
2. generalize the logic
3. convert it to mathematical equation
4. !!!!! don't think  (CHECK) again until you get wrong answer
5. write the code
*/

void solve() {
     ll n;
    cin>>n; // number of nodes;
    vector<pair<ll,ll>>edges(n-1); 
    map<pair<ll,ll>,ll>mp;
    for(ll i=0;i<n-1;i++){
        cin>>edges[i].first>>edges[i].second;
        if(edges[i].first>edges[i].second){
            swap(edges[i].first,edges[i].second);
        }
        mp[{edges[i].first,edges[i].second}]=1;
    }

    // prepraing adjacency list 
    vector<ll>degree(n,0);
    vector<vector<ll>>list(n,vector<ll>());
    for(ll i=0;i<n-1;i++){
        ll a=edges[i].first;
        ll b=edges[i].second;
        a--;
        b--;

        list[a].push_back(b);
        list[b].push_back(a);
        degree[a]++;
        degree[b]++;

    }


    vector<pair<ll,ll>>ans;

    // degree,node

    for(ll i=0;i<n;i++){
        ans.push_back({degree[i],i});
    }
    sort(ans.rbegin(),ans.rend());
    
    ll s1=ans[0].first; // max degree
     ll s2=ans[1].first;// low 

    ll max1=ans[0].second;

    vector<ll>help;

    for(ll i=1;i<ans.size();i++){
        if(ans[i].first==s2){
            help.push_back(ans[i].second);
        }
    }
    // for(ll i=0;i<ans.size();i++){
        
    //        cout<<ans[i].first<<" "<<ans[i].second<<endl;
        
    // }
    

    // cout<<endl;
    // cout<<endl;

    
    if(s1!=s2){
    vector<ll>unconet;
    vector<ll>con;

    for(ll i=0;i<help.size();i++){
        if(mp.find({max1+1,help[i]+1})!=mp.end() || mp.find({help[i]+1,max1+1})!=mp.end() ){
            con.push_back(help[i]);
        }
        else{
            unconet.push_back(help[i]);
            
        }
    }

    ll maxi=0;
    if(unconet.size()==0){

        ll a=con[0];
        ll dega=list[a].size();
       
         for(auto &it:con){
            maxi=max(maxi,degree[it]+s1-2);
        }
        cout<<maxi<<endl;

    }
    else{

        for(auto &it:unconet){
            maxi=max(maxi,degree[it]+s1-1);
        }
        // cout<<a<<" ";
        // ll dega=list[a].size();
        // cout<<s1+dega-1<<endl;
    cout<<maxi<<endl;
    }

    }
    else{
        ll one=ans[0].second;
        ll two=ans[1].second;
        ll maxi=-1e9;

    for(ll i=0;i<help.size();i++){
        
        if(mp.find({one+1,help[i]+1})!=mp.end() || mp.find({help[i]+1,one+1})!=mp.end() ){
            maxi=max(maxi,degree[help[i]]+s1-2);
        }
        else{
            maxi=max(maxi,degree[help[i]]+s1-1); 
        }
    }
    // cout<<two<<" ";

    for(ll i=0;i<help.size();i++){
        if(two!=help[i]){

        if(mp.find({two+1,help[i]+1})!=mp.end() || mp.find({help[i]+1,two+1})!=mp.end() ){
            maxi=max(maxi,degree[help[i]]+s1-2);
            cout<<maxi<<" ";
        }
        else{
            maxi=max(maxi,degree[help[i]]+s1-1);  
        } 
        }
    }

    cout<<maxi<<endl;


    }

    

   
   


  


  
}

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

    ll test = 1;
    cin >> test;
    while (test--) {
        solve();
    }
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define mod 1000000007

vector<bool> is_prime;

void sieve(ll n) {
    is_prime.assign(n + 1, true);
    is_prime[0] = is_prime[1] = false;
    for (ll i = 2; i * i <= n; i++) {
        if (is_prime[i]) {
            for (ll j = i * i; j <= n; j += i)
                is_prime[j] = false;
        }
    }
}

ll lcm(ll a, ll b) {
    return (a / __gcd(a, b)) * b;
}

/*
1. Read question carefully
2. generalize the logic
3. convert it to mathematical equation
4. !!!!! don't think  (CHECK) again until you get wrong answer
5. write the code
*/

void solve() {
     ll n;
    cin>>n; // number of nodes;
    vector<pair<ll,ll>>edges(n-1); 
    map<pair<ll,ll>,ll>mp;
    for(ll i=0;i<n-1;i++){
        cin>>edges[i].first>>edges[i].second;
        if(edges[i].first>edges[i].second){
            swap(edges[i].first,edges[i].second);
        }
        mp[{edges[i].first,edges[i].second}]=1;
    }

    // prepraing adjacency list 
    vector<ll>degree(n,0);
    vector<vector<ll>>list(n,vector<ll>());
    for(ll i=0;i<n-1;i++){
        ll a=edges[i].first;
        ll b=edges[i].second;
        a--;
        b--;

        list[a].push_back(b);
        list[b].push_back(a);
        degree[a]++;
        degree[b]++;

    }


    vector<pair<ll,ll>>ans;

    // degree,node

    for(ll i=0;i<n;i++){
        ans.push_back({degree[i],i});
    }
    sort(ans.rbegin(),ans.rend());
    
    ll s1=ans[0].first; // max degree
     ll s2=ans[1].first;// low 



    if(s1==s2){
    vector<ll>help;

    for(ll i=0;i<ans.size();i++){
        if(ans[i].first==s2){
            help.push_back(ans[i].second);
        }
    }
    ll maxi=-1e9;
    if(help.size()>=3){
        maxi=max(maxi,2*degree[help[0]]-1);
    }
    else{
        if(mp.find({help[0]+1,help[1]+1})!=mp.end() || mp.find({help[1]+1,help[0]+1})!=mp.end()){
            maxi=max(maxi,2*degree[help[0]]-2);
        }
        else{
            maxi=max(maxi,2*degree[help[0]]-1);

        }
    }
    cout<<maxi<<endl;

        
    }
    else{
    vector<ll>help;
    ll max1=ans[0].second;
    

    for(ll i=1;i<ans.size();i++){
        if(ans[i].first==s2){
            help.push_back(ans[i].second);
        }
    }

    ll maxi=-1;

    for(ll i=0;i<help.size();i++){
        if(mp.find({max1+1,help[i]+1})!=mp.end() || mp.find({help[i]+1,max1+1})!=mp.end() ){
           maxi=max(maxi,degree[max1]+degree[help[i]]-2);
        }
        else{
           maxi=max(maxi,degree[max1]+degree[help[i]]-1);
            
        }
    }
    cout<<maxi<<endl;

    }
  

   
   


  


  
}

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

    ll test = 1;
    cin >> test;
    while (test--) {
        solve();
    }
    return 0;
}