Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 624 ms):
#define _CRT_SECURE_NO_DEPRECATE
#include <bits/stdc++.h>
using namespace std;

const int N = 200005;
int nedge[N];

int cntEdge(vector<int> &v, set<int> &removed)
{
    int curedge = 0;
    for (int j = 0; j < v.size(); ++j)
    {
        if (removed.find(v[j]) == removed.end())
        {
            curedge += 1;
        }
    }
    return curedge;
}

int findVertexToRemove(int n, vector<vector<int>> &g, set<int> &removed, int time)
{
    int maxedge = 0;
    for (int i = 0; i < n; ++i)
    {
        if (removed.find(i) != removed.end())
        {
            nedge[i] = -1;
            continue;
        }
        int curedge = cntEdge(g[i], removed);
        nedge[i] = curedge;
        maxedge = max(maxedge, curedge);
    }

    for (int i = 0; i < n; ++i)
    {
        if (removed.find(i) != removed.end())
        {
            continue;
        }
        int curedge = cntEdge(g[i], removed);
        if (curedge == maxedge)
        {
            if (time == 1)
            {
                int nequal = 0;
                for (int j = 0; j < g[i].size(); ++j)
                {
                    if (nedge[g[i][j]] == curedge)
                    {
                        nequal += 1;
                    }
                }
                if (nequal >= 2)
                {
                    continue;
                }
            }
            return i;
        }
    }

    // Never come to this
    return -1;
}

void visit(int i, int n, vector<vector<int>> &g, set<int> &removed, vector<bool> &visited)
{
    visited[i] = true;
    for (int j = 0; j < g[i].size(); ++j)
    {
        if (visited[g[i][j]] || removed.find(g[i][j]) != removed.end())
        {
            continue;
        }
        visit(g[i][j], n, g, removed, visited);
    }
}

int cntComponent(int n, vector<vector<int>> &g, set<int> &removed)
{
    int ncomponent = 0;
    vector<bool> visited(n, false);
    for (int i = 0; i < n; ++i)
    {
        if (removed.find(i) != removed.end() || visited[i])
        {
            continue;
        }
        visit(i, n, g, removed, visited);
        ncomponent += 1;
    }
    return ncomponent;
}

int cntComponentAfterRemove(int n, vector<vector<int>> &g)
{
    set<int> removed;
    int vertex = findVertexToRemove(n, g, removed, 1);
    removed.insert(vertex);

    vertex = findVertexToRemove(n, g, removed, 2);
    removed.insert(vertex);

    int ncomponent = cntComponent(n, g, removed);
    return ncomponent;
}

int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    int tc;
    cin >> tc;
    for (int t = 1; t <= tc; ++t)
    {
        int n;
        cin >> n;

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

        int ncomponent = cntComponentAfterRemove(n, g);
        cout << ncomponent << "\n";
    }
}


Solution 2 (runtime: 328 ms):
#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
set<long long >adj[200000];
long long indeg[200000];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    long long t;
    cin>>t;
    while(t--)
    {
        long long n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            adj[i].clear();
            indeg[i]=0;
        }
        long long x,y;
        for(int i=0;i<n-1;i++)
        {
            cin>>x>>y;
            x--;
            y--;
            adj[x].insert(y);
            adj[y].insert(x);
            indeg[x]++;
            indeg[y]++;

        }
        vector<pair<long long , long long >>v;
        for(int i=0;i<n;i++)
        {
            v.push_back({indeg[i],i});
        }
        sort(v.begin(),v.end());
        if(n==2)cout<<0<<endl;
        else 
        {
            long long ind=-1;
            long long ans=0;
            bool check=false;
            for(int i=n-2;i>=0;i--)
            {
                if(adj[v[n-1].second].find(v[i].second)==adj[v[n-1].second].end())
                {
                    ind=i;
                    check=true;
                    ans=indeg[v[n-1].second]+indeg[v[i].second]-1;
                    break;
                }

            }
            if(ind<=n-4)
            {
               
                cout<<max(ans, max(indeg[v[n-1].second]+indeg[v[n-2].second]-2, indeg[v[n-2].second]+indeg[v[n-3].second]-1 ))<<endl;
            }
            else if(ind==n-2)
            {
                cout<<ans<<endl;
            }
            else
            {
                
                long long sita=indeg[v[n-1].second]+indeg[v[n-3].second]-1;
                long long rama=indeg[v[n-1].second]+indeg[v[n-2].second]-2;
                cout<<max(rama,sita)<<endl;
            }

        }
        

    }
}