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

Previous submission (verdict: Time limit exceeded on test 9):
#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();
    }
    // for (auto i:adj){
    //     for (int j:i){
    //         cout<<j<<" ";
    //     }cout<<endl;
    // }
    int edmax = 0;
    int maxver = 0;
    for (auto i:ed){
        if (i.second > edmax){
            edmax = i.second;
            maxver = i.first;
        }
    }
    // cout<<edmax<<" "<<endl;
    vector<pair<int,int>>maxvers;
    for (auto i:ed){
        if (i.second == edmax){
    //         // cout<<i.first<<endl;
            maxvers.push_back(make_pair(i.second, i.first));
        }
    }
    // for (auto i:maxvers){
    //     cout<<i.first<<" "<<i.second<<endl;
    // }
    int ans = 0;
    for (auto i:maxvers){
        set<pair<int,int>, greater<pair<int,int>>>s;
        for (auto j:ed){
            s.insert(make_pair(j.second, j.first));
        }
        s.erase(make_pair(i.first, i.second));
        for (auto k:adj[i.second]){
            s.erase(make_pair(ed[k], k));
            s.insert(make_pair(ed[k]-1, k));
        }
        auto it = s.begin()->first;
        ans = max(ans, i.first + it - 1);
    }
    cout<<ans<<endl;
    
    // 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;
    // for (auto i:ed){
    //     if (i.first != maxver && i.second > edmax1){
    //         edmax1 = i.second;
    //     }
    // }
    // cout<<edmax + edmax1 - 1<<endl;
}

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

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();
}