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

Previous submission (verdict: Time limit exceeded on test 3):
#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;
    map<pair<int,int>,int>ed1;
    for (int i = 0; i<n-1; i++){
        int u, v;
        cin>>u>>v;
        u--;
        v--;
        ed1[make_pair(u,v)] = 1;
        ed1[make_pair(v,u)] = 1;
        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 ans = 0;
    for (int i = 0; i<n; i++){
        for (int j:adj[i]){
            ans = max(ans, ed[i] + ed[j] - 2);
        }
    }
    vector<pair<int,int>>ed2;
    for (auto i:ed){
        ed2.push_back(make_pair(i.second, i.first));
    }
    sort(ed2.begin(), ed2.end());
    for (int i = n-1; i>=0; i--){
        for (int j = n-1; j>=0; j--){
            if (j != i && ed1.find(make_pair(ed2[i].second, ed2[j].second)) == ed1.end()){
                ans = max(ans, ed[ed2[i].second] + ed[ed2[j].second]-1);
            }
        }
    }
    cout<<ans<<endl;


    
}

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

Submission (verdict: Accepted):
#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;
    map<pair<int,int>,int>ed1;
    for (int i = 0; i<n-1; i++){
        int u, v;
        cin>>u>>v;
        u--;
        v--;
        ed1[make_pair(u,v)] = 1;
        ed1[make_pair(v,u)] = 1;
        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 ans = 0;
    for (int i = 0; i<n; i++){
        for (int j:adj[i]){
            ans = max(ans, ed[i] + ed[j] - 2);
        }
    }
    vector<pair<int,int>>ed2;
    for (auto i:ed){
        ed2.push_back(make_pair(i.second, i.first));
    }
    sort(ed2.begin(), ed2.end());
    for (int i = n-1; i>=0; i--){
        for (int j = n-1; j>=0; j--){
            if (j != i && ed1.find(make_pair(ed2[i].second, ed2[j].second)) == ed1.end()){
                ans = max(ans, ed[ed2[i].second] + ed[ed2[j].second]-1);
                break;
            }
        }
    }
    cout<<ans<<endl;


    
}

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