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

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
#include <chrono> 
using namespace std;
using namespace chrono; 
#define TxtIO freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define int long long
#define Varun ios_base::sync_with_stdio(false); cin.tie(NULL);
const int M = 1e9+7;
// functions
int ceil_div(int a,int b){return a%b==0?(a/b):(a/b)+1;}
int gcd(int a, int b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
int expo(int a, int b, int mod) {int res = 1; while (b > 0) {if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
void extendgcd(int a, int b, int *v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return;} extendgcd(b, a % b, v); int x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an array of size 3
int mminv(int a, int b) {int arr[3]; extendgcd(a, b, arr); return arr[0];} 
int mminvprime(int a, int b) {return expo(a, b - 2, b);}
bool revsort(int a, int b) {return a > b;}
int combination(int n, int r, int m, int *fact, int *ifact) {int val1 = fact[n]; int val2 = ifact[n - r]; int val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}
void google(int t) {cout << "Case #" << t << ": ";}
vector<int> sieve(int n) {int *arr = new int[n + 1](); vector<int> vect; for (int i = 2; i <= n; i++) if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i) arr[j] = 1;} return vect;}
int mod_add(int a, int b, int m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
int mod_mul(int a, int b, int m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
int mod_sub(int a, int b, int m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
int mod_div(int a, int b, int m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;} //only for prime m
int phin(int n) {int number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (int i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0) n /= i; number = (number / i * (i - 1));}} if (n > 1) number = (number / n * (n - 1)); return number;} //O(sqrt(N))
 
void solve(){
    int n;
    cin>>n;
    set<pair<int,int>>edges;
    vector<int>deg(n+1);
    
    for(int i = 0; i < n-1; ++i){
        int u,v;
        cin >> u>>v;
        edges.insert({u,v});
        edges.insert({v,u});
        deg[u]++;
        deg[v]++;        
    }

    int ans = 0;

    for(auto it:edges){
        int x = it.first;
        int y = it.second;
        ans = max(ans, deg[x]+deg[y]-2);
    }

    

    vector<pair<int,int>>v;
    for(int i = 1; i <= n; ++i){
        v.push_back({deg[i], i});
    }
    sort(v.rbegin(), v.rend());
    
    for(int i = 0; i < n; i++) { 
        for(int j = i+1; j < n; j++) {
            int x = v[i].second;
            int y = v[j].second;
            if(edges.find({x,y}) == edges.end()) {
                ans = max(ans, v[i].first+v[j].first-1);
                cout << ans  << "\n";
                return;
            }
        }
    }
    
    cout << ans  << "\n";
    
    



    
}

signed main(){
    Varun
    // TxtIO

    int t = 1; 
    cin >> t;
    auto start = high_resolution_clock::now(); 
    while(t--){
        solve();
    }
    auto stop = high_resolution_clock::now(); 
    auto duration = duration_cast<milliseconds>(stop - start); 
    
    // cerr << "Time: " << duration.count() << " ms" << endl;

    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
#include <chrono> 
using namespace std;
using namespace chrono; 
#define TxtIO freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define int long long
#define Varun ios_base::sync_with_stdio(false); cin.tie(NULL);
const int M = 1e9+7;
// functions
int ceil_div(int a,int b){return a%b==0?(a/b):(a/b)+1;}
int gcd(int a, int b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
int expo(int a, int b, int mod) {int res = 1; while (b > 0) {if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
void extendgcd(int a, int b, int *v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return;} extendgcd(b, a % b, v); int x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an array of size 3
int mminv(int a, int b) {int arr[3]; extendgcd(a, b, arr); return arr[0];} 
int mminvprime(int a, int b) {return expo(a, b - 2, b);}
bool revsort(int a, int b) {return a > b;}
int combination(int n, int r, int m, int *fact, int *ifact) {int val1 = fact[n]; int val2 = ifact[n - r]; int val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}
void google(int t) {cout << "Case #" << t << ": ";}
vector<int> sieve(int n) {int *arr = new int[n + 1](); vector<int> vect; for (int i = 2; i <= n; i++) if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i) arr[j] = 1;} return vect;}
int mod_add(int a, int b, int m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
int mod_mul(int a, int b, int m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
int mod_sub(int a, int b, int m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
int mod_div(int a, int b, int m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;} //only for prime m
int phin(int n) {int number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (int i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0) n /= i; number = (number / i * (i - 1));}} if (n > 1) number = (number / n * (n - 1)); return number;} //O(sqrt(N))
 
void solve(){
    int n;
    cin>>n;
    set<pair<int,int>>edges;
    vector<int>deg(n+1);
    
    for(int i = 0; i < n-1; ++i){
        int u,v;
        cin >> u>>v;
        edges.insert({u,v});
        edges.insert({v,u});
        deg[u]++;
        deg[v]++;        
    }

    int ans = 0;    

    vector<pair<int,int>>v;
    for(int i = 1; i <= n; ++i){
        v.push_back({deg[i], i});
    }
    sort(v.rbegin(), v.rend());
    
    for(int i = 0; i < n; i++) { 
        for(int j = i+1; j < n; j++) {
            int x = v[i].second;
            int y = v[j].second;
            if(edges.find({x,y}) == edges.end()) {
                ans = max(ans, v[i].first+v[j].first-1);
                break;
            }
        }
    }

    

    for(auto it:edges){
        int x = it.first;
        int y = it.second;
        ans = max(ans, deg[x]+deg[y]-2);
    }
    
    cout << ans  << "\n";
    
    



    
}

signed main(){
    Varun
    // TxtIO

    int t = 1; 
    cin >> t;
    auto start = high_resolution_clock::now(); 
    while(t--){
        solve();
    }
    auto stop = high_resolution_clock::now(); 
    auto duration = duration_cast<milliseconds>(stop - start); 
    
    // cerr << "Time: " << duration.count() << " ms" << endl;

    return 0;
}