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

Solution 1 (runtime: 436 ms):
#include <bits/stdc++.h>
using namespace std;
#define NITRO ios_base::sync_with_stdio(false), cin.tie(NULL)
#define endl "\n"
typedef long long ll;
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
//using namespace __gnu_pbds;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

vector<int> graph[200005], backupGraph[200005];
bool vis[200005];

pair<int,int> findMaxConnected(int s, bool primary = 1)
{
    vis[s]=1;
    if(primary == 1)
    {
        pair<int,int> ans = { graph[s].size(),s};
        for(auto &i:graph[s])
        {
            if(!vis[i])
            {
                pair<int,int> temp = findMaxConnected(i);
                if(temp.first>ans.first)
                {
                    ans = temp;
                }
            }
        }
        return ans;
    }
    else
    {
        pair<int,int> ans = { backupGraph[s].size(),s};
        for(auto &i:backupGraph[s])
        {
            if(!vis[i])
            {
                pair<int,int> temp = findMaxConnected(i, 0);
                if(temp.first>ans.first)
                {
                    ans = temp;
                }
            }
        }
        return ans;
    }
}

pair<int,int> findMaxConnectedABitDifferently(int s)
{
    vis[s]=1;
    pair<int,int> ans = { graph[s].size(),s};
    for(auto &i:graph[s])
    {
        if(!vis[i])
        {
            pair<int,int> temp = findMaxConnectedABitDifferently(i);
            if(temp.first>=ans.first)
            {
                ans = temp;
            }
        }
    }
    return ans;
}

void deleteSingleNode(int s, int n, bool primary = 1)
{
    int i,j;
    if(primary==1)
    {
        graph[s].clear();
        for(i=1;i<=n;i++)
        {
            for(j=0;j<graph[i].size();j++)
            {
                if(graph[i][j]==s)
                {
                    graph[i].erase(graph[i].begin()+j);
                    break;
                }
            }
        }
    }
    else
    {
        backupGraph[s].clear();
        for(i=1;i<=n;i++)
        {
            for(j=0;j<backupGraph[i].size();j++)
            {
                if(backupGraph[i][j]==s)
                {
                    backupGraph[i].erase(backupGraph[i].begin()+j);
                    break;
                }
            }
        }
    }
    
}

int main()
{
    NITRO;
    // freopen("input.txt","r", stdin);
    // freopen("output.txt","w", stdout);
    int t = 1, h = 1,n;
    cin >> t;
    while (t--)
    {
        int n,i;
        cin>>n;
        for(i=0;i<=n;i++)
        {
            graph[i].clear();
            backupGraph[i].clear();
        }
        memset(vis, 0, sizeof vis);
        for(i=0;i<n-1;i++)
        {
            int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
            backupGraph[a].push_back(b);
            backupGraph[b].push_back(a);
        }
        pair<int,int> ans = findMaxConnected(1);
        //cout<<ans.first<<" "<<ans.second<<" ---> first"<<endl;
        
        int separateComponent = ans.first;
        if(separateComponent == 0)
        {
            cout<<0<<endl;
            continue;
        }

        memset(vis, 0, sizeof vis);
        pair<int,int> difans = findMaxConnectedABitDifferently(1);

        separateComponent--;
        int separateComponent2 = 0;

        if(ans == difans)
        {
            deleteSingleNode(ans.second,n);
            memset(vis, 0, sizeof vis);
            for(i=1;i<=n;i++)
            {
                if(!vis[i])
                {
                    pair<int,int> p = findMaxConnected(i);
                    separateComponent2 = max(separateComponent2, p.first);
                }
            }
            int res = separateComponent2 + separateComponent;
            cout<<res<<endl;
        }
        else
        {
            deleteSingleNode(ans.second,n);
            memset(vis, 0, sizeof vis);
            for(i=1;i<=n;i++)
            {
                if(!vis[i])
                {
                    pair<int,int> p = findMaxConnected(i);
                    separateComponent2 = max(separateComponent2, p.first);
                }
            }
            int res = separateComponent2 + separateComponent;
            
            int res2 = 0, sp=0;

            deleteSingleNode(difans.second, n, 0);
            memset(vis, 0, sizeof vis);
            for(i=1;i<=n;i++)
            {
                if(!vis[i])
                {
                    pair<int,int> p = findMaxConnected(i, 0);
                    sp = max(sp, p.first);
                }
            }

            res2 = sp + separateComponent;

            cout<<max(res, res2)<<endl;
        }
    }
    return 0;
}

Solution 2 (runtime: 312 ms):
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <set>
#include <iomanip>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define INT_MAX 2147483647
#define LL long long
#define LL_MAX 9223372036854775807
inline int read() {
    int ret = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-') f = -f; ch = getchar(); }
    while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar();
    return ret * f;
}
inline long long readLL()
{
    long long ret = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-') f = -f; ch = getchar(); }
    while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar();
    return ret * f;
}
inline void write(int x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
    return;
}
inline void write(long long x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
    return;
}
struct cmp
{
    bool operator ()(const pair<int, int>& a, const pair<int, int>& b)
    {
        if (a.first == b.first)
            return a.second < b.second;
        return a.first > b.first;
    }
};
int main()
{
    int T = read();
    while (T--)
    {
        int n = read();
        vector<unordered_set<int>> adj(n + 1);
        int ans = 1, mx = 0;
        for (int i = 1; i < n; ++i)
        {
            int a = read(), b = read();
            adj[a].insert(b);
            adj[b].insert(a);
            mx = max(mx, (int)adj[a].size());
            mx = max(mx, (int)adj[b].size());
        }
        //set<pair<int,int>> s
        int mn = INT_MAX, mnNode = 0;
        for (int i = 1; i <= n; ++i)
        {
            if (adj[i].size() < mx)
                continue;
            int cnt = 0;
            for (int j : adj[i])
                if (adj[j].size() == mx)
                    ++cnt;
            if (cnt < mn)
            {
                mn = cnt;
                mnNode = i;
            }
        }
        ans += mx - 1;
        for (int i : adj[mnNode])
            adj[i].erase(mnNode);
        adj[mnNode].clear();

        mx = -1;int mxNode = 0;
        for (int i = 1; i <= n; ++i)
        {
            if ((int)adj[i].size() > mx)
            {
                mx = adj[i].size();
                mxNode = i;
            }
        }
        ans += mx - 1;

        write(ans), putchar('\n');
    }
    
    return 0;
}