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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <stack>
#include <set>
#include <list>
#include <map>
#include <cstring>
#include <iterator>
#define ll long long
#define MOD 1000000007 
using namespace std;
vector<int>sieve(int n){
    vector<int>p;
    vector<int>isprime(n+1, 1);
    isprime[0] = isprime[1] = 0;
    for (int i = 2; i<=n; i++){
        if (isprime[i]){
            for (int j = 2*i; j<=n; j+=i){
                isprime[j] = 0;
            }
        }
    }
    for (int i = 1; i<=n; i++){
        if (isprime[i]){
            p.push_back(i);
        }
    }
    return p;
}
//---------------------------------------------------------------------------
// bool check(int k, vector<int>d, vector<int>a){

// }

void solve(){
    int n;
    cin>>n;
    vector<vector<int>>adj(n);
    map<int,int>ed;
    for (int i = 0; i<n-1; i++){
        int u, v;
        cin>>u>>v;
        u--;
        v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 0; i<n; i++){
        ed[i] = adj[i].size();
    }
    int edmax = 0;
    int maxver = 0;
    for (auto i:ed){
        if (i.second > edmax){
            edmax = i.second;
            maxver = i.first;
        }
    }
    for (int i:adj[maxver]){
        ed[i] -= 1;
    }
    int edmax1 = 0;
    int maxver1 = 0;
    for (auto i:ed){
        if (i.first != maxver && i.second > edmax1){
            edmax1 = i.second;
            maxver1 = i.first;
        }
    }
    cout<<edmax + edmax1 - 1<<endl;
}

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

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <stack>
#include <set>
#include <list>
#include <map>
#include <cstring>
#include <iterator>
#define ll long long
#define MOD 1000000007 
using namespace std;
vector<int>sieve(int n){
    vector<int>p;
    vector<int>isprime(n+1, 1);
    isprime[0] = isprime[1] = 0;
    for (int i = 2; i<=n; i++){
        if (isprime[i]){
            for (int j = 2*i; j<=n; j+=i){
                isprime[j] = 0;
            }
        }
    }
    for (int i = 1; i<=n; i++){
        if (isprime[i]){
            p.push_back(i);
        }
    }
    return p;
}
//---------------------------------------------------------------------------
// bool check(int k, vector<int>d, vector<int>a){

// }

void solve(){
    int n;
    cin>>n;
    vector<vector<int>>adj(n);
    map<int,int>ed;
    for (int i = 0; i<n-1; i++){
        int u, v;
        cin>>u>>v;
        u--;
        v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    if (n <= 4){
        cout<<n-2<<endl;
        return;
    }
    for (int i = 0; i<n; i++){
        ed[i] = adj[i].size();
    }
    int edmax = 0;
    int maxver = 0;
    for (auto i:ed){
        if (i.second > edmax){
            edmax = i.second;
            maxver = i.first;
        }
    }
    if (edmax == 2){
        cout<<3<<endl;
        return;
    }
    for (int i:adj[maxver]){
        ed[i] -= 1;
    }
    int edmax1 = 0;
    int maxver1 = 0;
    for (auto i:ed){
        if (i.first != maxver && i.second > edmax1){
            edmax1 = i.second;
            maxver1 = i.first;
        }
    }
    cout<<edmax + edmax1 - 1<<endl;
}

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