Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #include<conio.h>

using namespace::std;
using namespace::chrono;
using namespace __gnu_pbds;
using ull = unsigned long long int;
using ll = long long int;
using ul = unsigned long int;
using l = long int;
using ld= long double;
using d = double;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

#define MOD 1000000007
#define len_array(array,datatype) sizeof(array)/sizeof(datatype)
#define printl(x) cout << x << "\n";
#define prints(x) cout << x << " ";
#define print_with_lines(name,n) for(auto x: name){cout << x << "\n";}
#define print_with_spaces(name,n) for(auto x: name){cout << x << " ";} cout << "\n" ;
#define pb push_back
#define mp make_pair
#define ppb pop_back
#define vi(name) vector<int> name
#define vivi(name) vector<vector<int>> name
#define vi_ll(name) vector<ll> name
#define vivi_ll(name) vector<vector<ll>> name
#define sort_vector(v) sort(v.begin() , v.end())
#define rsort_vector(v,datatype) sort(v.begin() , v.end() , greater<datatype>())
#define merge_vectors(v1,v2,v3) merge(v1.begin() , v1.end() , v2.begin() , v2.end() , v3.begin())
#define merge_vectors_greater(v1,v2,v3,datatype) merge(v1.begin() , v1.end() , v2.begin() , v2.end() , v3.begin() , greater<datatype>())
#define f(var,ll,ul) for(int var=ll ; var<ul ; var++)
#define fe(var,ll,ul) for(int var=ll ; var<=ul ; var++)
#define rf(var,ll,ul) for(int var=ul-1 ; var>=ll ; var--)

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(ld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull 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.first); cerr << ","; _print(p.second); 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 << "]";}

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll min(ll a,int b) { if (a<b) return a; return b; }
ll min(int a,ll b) { if (a<b) return a; return b; }
ll max(ll a,int b) { if (a>b) return a; return b; }
ll max(int a,ll b) { if (a>b) return a; return b; }
ll gcd(ll a,ll b) { if (b==0) return a; return gcd(b, a%b); }
ll nCr(ll n,ll r) { if(r>n/2){ if(r>n){ return 0;}} if(r>n/2){ return nCr(n,n-r); }ll prod=1; for(ll i=1;i<=r;i++){ prod=(prod*(n-r+i))/i; } return prod; }
ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
ll fact(ll n) { ll res=1; for(int i=2;i<=n;i++) { res*=i; } return res;}
string to_upper(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A'; return a; }
string to_lower(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A'; return a; }
bool prime(ll a) { if (a==1) return 0; for (int i=2;i<=round(sqrt(a));++i) if (a%i==0) return 0; return 1; }
void yes() { cout<<"YES\n"; }
void no() { cout<<"NO\n"; }

void solve(){
    int n; cin >> n;
    vector<int> cnt(n);
    f(i,0,n-1){
        int a,b; cin >> a >> b;
        cnt[a-1]++;
        cnt[b-1]++;
    }
    rsort_vector(cnt, int);
    bool all_1 = true;
    f(i,1,n){
        if(cnt[i] != 1) all_1 = false;
    }
    if(!all_1) printl(cnt[0] + cnt[1] - 1)
    else printl(cnt[0] - 1)
}

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

    #ifndef ONLINE_JUDGE
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
    freopen("Error.txt", "w", stderr);
    #else
    // online submission
    #endif

    auto start1 = high_resolution_clock::now();

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

    // solve();

    auto stop1 = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop1 - start1);
    #ifndef ONLINE_JUDGE
        cerr << "Time: " << duration . count() / 1000 << " ms " << "\n";
    #endif

    // _getch(); 
}

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #include<conio.h>

using namespace::std;
using namespace::chrono;
using namespace __gnu_pbds;
using ull = unsigned long long int;
using ll = long long int;
using ul = unsigned long int;
using l = long int;
using ld= long double;
using d = double;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

#define MOD 1000000007
#define len_array(array,datatype) sizeof(array)/sizeof(datatype)
#define printl(x) cout << x << "\n";
#define prints(x) cout << x << " ";
#define print_with_lines(name,n) for(auto x: name){cout << x << "\n";}
#define print_with_spaces(name,n) for(auto x: name){cout << x << " ";} cout << "\n" ;
#define pb push_back
#define mp make_pair
#define ppb pop_back
#define vi(name) vector<int> name
#define vivi(name) vector<vector<int>> name
#define vi_ll(name) vector<ll> name
#define vivi_ll(name) vector<vector<ll>> name
#define sort_vector(v) sort(v.begin() , v.end())
#define rsort_vector(v,datatype) sort(v.begin() , v.end() , greater<datatype>())
#define merge_vectors(v1,v2,v3) merge(v1.begin() , v1.end() , v2.begin() , v2.end() , v3.begin())
#define merge_vectors_greater(v1,v2,v3,datatype) merge(v1.begin() , v1.end() , v2.begin() , v2.end() , v3.begin() , greater<datatype>())
#define f(var,ll,ul) for(int var=ll ; var<ul ; var++)
#define fe(var,ll,ul) for(int var=ll ; var<=ul ; var++)
#define rf(var,ll,ul) for(int var=ul-1 ; var>=ll ; var--)

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(ld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull 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.first); cerr << ","; _print(p.second); 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 << "]";}

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll min(ll a,int b) { if (a<b) return a; return b; }
ll min(int a,ll b) { if (a<b) return a; return b; }
ll max(ll a,int b) { if (a>b) return a; return b; }
ll max(int a,ll b) { if (a>b) return a; return b; }
ll gcd(ll a,ll b) { if (b==0) return a; return gcd(b, a%b); }
ll nCr(ll n,ll r) { if(r>n/2){ if(r>n){ return 0;}} if(r>n/2){ return nCr(n,n-r); }ll prod=1; for(ll i=1;i<=r;i++){ prod=(prod*(n-r+i))/i; } return prod; }
ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
ll fact(ll n) { ll res=1; for(int i=2;i<=n;i++) { res*=i; } return res;}
string to_upper(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A'; return a; }
string to_lower(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A'; return a; }
bool prime(ll a) { if (a==1) return 0; for (int i=2;i<=round(sqrt(a));++i) if (a%i==0) return 0; return 1; }
void yes() { cout<<"YES\n"; }
void no() { cout<<"NO\n"; }

void solve(){
    int n; cin >> n;
    vector<int> cnt(n);
    set<pair<int,int>> s;
    f(i,0,n-1){
        int a,b; cin >> a >> b;
        s.insert({min(a,b), max(a,b)});
        cnt[a-1]++;
        cnt[b-1]++;
    }
    int max_index = 0;
    f(i,0,n){
        if(cnt[i] > cnt[max_index]){
            max_index = i;
        }
    }
    bool all_1 = true;
    f(i,0,n){
        if(i != max_index){
            if(cnt[i] != 1) all_1 = false;
        }
    }
    if(all_1){
        printl(cnt[max_index] -1)
        return;
    }
    for(auto [first, second] : s){
        if(first == max_index + 1) cnt[second - 1]--;
        if(second == max_index + 1) cnt[first - 1]--;
    }
    int res = cnt[max_index];
    int maxm = 0;
    f(i,0,n){
        if(i != max_index){
            maxm = max(maxm, cnt[i]);
        }
    }
    res += maxm - 1;
    printl(res)
    // rsort_vector(cnt, int);
    // for(auto [first, second]: s){
    //     if(first == )
    // }
    // bool all_1 = true;
    // f(i,1,n){
    //     if(cnt[i] != 1) all_1 = false;
    // }
    // if(!all_1) printl(cnt[0] + cnt[1] - 1)
    // else printl(cnt[0] - 1)
}

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

    #ifndef ONLINE_JUDGE
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
    freopen("Error.txt", "w", stderr);
    #else
    // online submission
    #endif

    auto start1 = high_resolution_clock::now();

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

    // solve();

    auto stop1 = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop1 - start1);
    #ifndef ONLINE_JUDGE
        cerr << "Time: " << duration . count() / 1000 << " ms " << "\n";
    #endif

    // _getch(); 
}