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

Previous submission (verdict: Wrong answer on test 4):

#pragma GCC optimize("O3,unroll-loops")

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace chrono;
using namespace __gnu_pbds;

template<class T> using oset =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update >; // *find_by_order, order_of_key -- gives the number of elements smaller than the number searched

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define forn(i,a,n) for (long long i=a; i<n; ++i)

#define int long long
#define double long double

#define pb push_back
#define ppb pop_back
// #define mp make_pair
#define ff first
#define ss second
#define sz(x) ((int)(x).size())
#define nline "\n"
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define seea(a,n) for (long long i=0; i<n; ++i) {cin>>a[i]}
#define seev(v,n) for (long long i=0; i<n; ++i) {long long x; cin>>x; v.push_back(x);}
#define sees(s,n) for (long long i=0; i<n; ++i) {long long x; cin>>x; s.insert(x);}

#define yeah cout<<"YES\n"
#define nah cout<<"NO\n"
#define nl cout<<"\n"
#define endl "\n"

#define MOD 1000000007
#define MOD1 998244353  	                            
#define INF 1e18
#define PI 3.141592653589793238462


#define debug(x) cerr << #x<<" "; _print(x); cerr << endl;

void _print(int t) {cerr<<t;}
void _print(string t) {cerr<<t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(unsigned long long t) {cerr<<t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}


bool isPrime(int n){ for (int i = 2; i * i <= n; i++){ if (n % i == 0){ return false; }} return true; }
int nextPrime(int n){ n++; while(!isPrime(n)){ n++; } return n; }
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;}
int mminvprime(int a, int b) {return expo(a, b - 2, b);}
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 a[300001], b[300001];
void solve(){

int n; cin>>n;
map<int,int>degree;
map<int,list<int>>mp;
int m1 = 0,m2 = 0,maxx;
for (int i = 1; i < n; i++)
{
    int u,v;cin>>u>>v;degree[u]++;degree[v]++;
    mp[u].pb(v); mp[v].pb(u);
}
for (int i = 1; i <= n; i++)
{
    if(degree[i] >= m1){maxx = i; m1 = degree[i];}
    // else{m2 = max(degree[i],m2);}
}
    degree[maxx] = 0;

for (auto i : mp[maxx]){degree[i]--;}


for (int i = 1; i <= n; i++)
{

  if(degree[i] >= m2){ m2 = degree[i];}    
}






// cout<<m1<<m2<<endl;
cout<<(-1+m1+m2)<<endl;

   }

int32_t main()
{
fastio();
int t = 1;
cin>>t;

for (int i = 0; i < t; i++)
{
    solve();

}



 return 0;
}

Submission (verdict: Wrong answer on test 2):

#pragma GCC optimize("O3,unroll-loops")

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace chrono;
using namespace __gnu_pbds;

template<class T> using oset =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update >; // *find_by_order, order_of_key -- gives the number of elements smaller than the number searched

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define forn(i,a,n) for (long long i=a; i<n; ++i)

#define int long long
#define double long double

#define pb push_back
#define ppb pop_back
// #define mp make_pair
#define ff first
#define ss second
#define sz(x) ((int)(x).size())
#define nline "\n"
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define seea(a,n) for (long long i=0; i<n; ++i) {cin>>a[i]}
#define seev(v,n) for (long long i=0; i<n; ++i) {long long x; cin>>x; v.push_back(x);}
#define sees(s,n) for (long long i=0; i<n; ++i) {long long x; cin>>x; s.insert(x);}

#define yeah cout<<"YES\n"
#define nah cout<<"NO\n"
#define nl cout<<"\n"
#define endl "\n"

#define MOD 1000000007
#define MOD1 998244353  	                            
#define INF 1e18
#define PI 3.141592653589793238462


#define debug(x) cerr << #x<<" "; _print(x); cerr << endl;

void _print(int t) {cerr<<t;}
void _print(string t) {cerr<<t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(unsigned long long t) {cerr<<t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}


bool isPrime(int n){ for (int i = 2; i * i <= n; i++){ if (n % i == 0){ return false; }} return true; }
int nextPrime(int n){ n++; while(!isPrime(n)){ n++; } return n; }
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;}
int mminvprime(int a, int b) {return expo(a, b - 2, b);}
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 a[300001], b[300001];
void solve(){

int n,ans = 0;
 cin>>n;
map<int,int>degree;
vector<pair<int,int>>vec;
set<pair<int,int>>edge;
map<int,list<int>>mp;
int m1 = 0,m2 = 0,maxx = 0;
for (int i = 1; i < n; i++)
{
    int u,v;cin>>u>>v;degree[u]++;degree[v]++;
    mp[u].pb(v); mp[v].pb(u);
    edge.insert({u,v});
    edge.insert({v,u});
}

for (int i = 1; i <= n; i++)
{
    for (auto  j : mp[i])
    {
           ans = max(ans, degree[i] + degree[j] - 2);
    }
    vec.pb({degree[i],i});
    }
    
sort(vec.begin(),vec.end());

for (int i = 0; i < n; i++)
{   int j = i+1;
    for (; j < n && edge.find({vec[i].second,vec[j].second}) != edge.end(); j++)
    {
        1;
    }
    if(j < n){
        ans = max(ans, vec[i].first+vec[j].first - 1); 
    }
}


cout<<ans<<endl;

   }

int32_t main()
{
fastio();
int t = 1;
cin>>t;

for (int i = 0; i < t; i++)
{
    solve();

}



 return 0;
}