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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
 
#define int long long 

int n;

int findMax(vector<int>& connect, vector<vector<int>>& edge, vector<int>& deg){
    int maxi = 0;
    for (int i = 0; i < n-1; i++){
        int u = edge[i][0], v = edge[i][1];
    
        if (deg[u] == 1) connect[v]++;
        if (deg[v] == 1) connect[u]++;
        if (connect[u] > connect[maxi]) maxi = u;
        if (connect[v] > connect[maxi]) maxi = v;
    }
    return maxi;
}

void dfs(int cur, vector<vector<int>>& adj, vector<bool>& visited){
    visited[cur] = true;
 
    for (int next:adj[cur]){
        if (visited[next]) continue;
        dfs(next, adj, visited);
    }
}

 
void solve(){
    cin >> n;
    vector<int> deg(n);
    vector<vector<int>> edge;

    for (int i = 0; i < n-1; i++){
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        edge.push_back({u, v});
        deg[u]++;
        deg[v]++;
    }

    vector<int> connect(n, 0);
    int maxi = findMax(connect, edge, deg);
    // cerr << maxi << endl;

    deg[maxi] = -1;
    

    for (int i = 0; i < n-1; i++){
        int u = edge[i][0], v = edge[i][1];

        if (u == maxi) deg[v]--;
        if (v == maxi) deg[u]--;
    }
    connect.assign(n, 0);
    connect[maxi] = -1;
    maxi = findMax(connect, edge, deg);
    // cerr << maxi << endl;

    deg[maxi] = -1;


    vector<vector<int>> adj(n);
 
    for (int i = 0; i < n-1; i++){
        int u = edge[i][0], v = edge[i][1];
 
        if (deg[u] == -1 || deg[v] == -1) continue;
        adj[u].push_back(v);
        adj[v].push_back(u);
 
    }
 
    vector<bool> visited(n, false);
    int ans = 0;
    for (int i = 0; i < n; i++){
        if (visited[i] || deg[i] == -1) continue;
        ans++;
        dfs(i, adj, visited);
    }
 
    cout << ans;

}
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1; 
    cin >> t; 
    while (t--){ 
        solve();
        cout << '\n';
    } 
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
 
#define int long long 

int n;

/*
First approach: select 2 vertices that have the most number of edge to a lonely vertext (lonely means degree 1)
-> NOT WORKING because of this: https://codeforces.com/blog/entry/138593?#comment-1240159



Second approach: 
di+dj−1 if i and j are not adjacent;
di+dj−2 if i and j are adjacent, because removing i will decrease deg of j by 1
.*/

 
void solve(){
    cin >> n;
    vector<int> deg(n);
    vector<unordered_set<int>> adj(n);
    vector<vector<int>> edge;
    for (int i = 0; i < n-1; i++){
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        deg[u]++;
        deg[v]++;
        adj[u].insert(v);
        adj[v].insert(u);
        edge.push_back({u, v});
    }

    if (n == 2){
        cout << 0;
        return;
    }

    vector<pair<int,int>> vec;
    for (int i = 0; i < n; i++){
        vec.push_back({deg[i], i});
    }
    sort(vec.begin(), vec.end(), [](auto &a, auto &b){
        return a.first > b.first;
    });

    int ans = 0;

    // u and v are not adjacent
    // iterate u -> get v so that deg[v] largest and not connected to u
    for (int i = 0; i < n; i++){
        int u = vec[i].second;

        for (int j = i+1; j < n; j++){
            int v = vec[j].second;

            if (adj[u].find(v) == adj[u].end()){
                ans = max(ans, deg[u] + deg[v] - 1);
                break;
            }
        }
    }

    // u and v are adjacent
    for (vector<int> e:edge){
        int u = e[0], v = e[1];

        ans = max(ans, deg[u] + deg[v]-2);
    }
    cout << ans;

}
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1; 
    cin >> t; 
    while (t--){ 
        solve();
        cout << '\n';
    } 
}