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

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define ll long long
using namespace std;

ll n,m,t,d[200005],q,mn,sum,ans,indx,r,l,k,p[200005],pr[200005];

ll mx;

//pair <int,int> p[20005];

bool flag;

string s;

set <long long> st,st1;

set <long long> :: iterator it,it1;

vector <long long> v,edg[200005];

map <ll,ll> mp;

bool b[200005];

void treeDp(int a,int par){
    d[a] = 0;
    for(int i = 0; i < edg[a].size(); i++){
        if(edg[a][i] != par){
            treeDp(edg[a][i], a);
            mx = max((ll) mx,max((ll) (d[edg[a][i]] + edg[a].size() - 1),(ll) (edg[edg[a][i]].size() + edg[a].size() - 2)));

        }
    }
    v.clear();
    for(int i = 0; i < edg[a].size(); i++){
        if(edg[a][i] != par)
        v.pb(max((ll)d[edg[a][i]],(ll)edg[edg[a][i]].size()));
    }
    sort(v.begin(),v.end());
    if(v.size() > 2){
        mx = max(mx,v[v.size() - 1] + v[v.size() - 2] + 1 - 2);
    }
    for(int i = 0; i < edg[a].size(); i++){
        if(edg[a][i] != par)
        d[a] = max(d[a],max(d[edg[a][i]],(ll) edg[edg[a][i]].size()));
    }
}

long long y,x,e,u;

int main(){
    cin >> t;
    while(t--){
        for(int i = 1; i <= n; i++){
            edg[i].clear();
        }
        cin >> n;
        for(int i = 1; i < n; i++){
            cin >> e >> u;
            edg[e].pb(u);
            edg[u].pb(e);
        }
        mx = 0;
        treeDp(1,0);
    //for(int i = 1; i <= n; i++) cout << d[i] << " ";
    //cout << endl;
        cout << mx << endl;
    }
}


Submission (verdict: Accepted):
#include<bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define ll long long
using namespace std;

ll n,m,t,d[200005],q,mn,sum,ans,indx,r,l,k,p[200005],pr[200005];

ll mx;

//pair <int,int> p[20005];

bool flag;

string s;

set <long long> st,st1;

set <long long> :: iterator it,it1;

vector <long long> v,edg[200005];

map <ll,ll> mp;

bool b[200005];

void treeDp(int a,int par){
    d[a] = 0;
    for(int i = 0; i < edg[a].size(); i++){
        if(edg[a][i] != par){
            treeDp(edg[a][i], a);
            mx = max((ll) mx,max((ll) (d[edg[a][i]] + edg[a].size() - 1),(ll) (edg[edg[a][i]].size() + edg[a].size() - 2)));

        }
    }
    v.clear();
    for(int i = 0; i < edg[a].size(); i++){
        if(edg[a][i] != par)
        v.pb(max((ll)d[edg[a][i]],(ll)edg[edg[a][i]].size()));
    }
    sort(v.begin(),v.end());
    if(v.size() >= 2){
        mx = max(mx,v[v.size() - 1] + v[v.size() - 2] + 1 - 2);
    }
    for(int i = 0; i < edg[a].size(); i++){
        if(edg[a][i] != par)
        d[a] = max(d[a],max(d[edg[a][i]],(ll) edg[edg[a][i]].size()));
    }
}

long long y,x,e,u;

int main(){
    cin >> t;
    while(t--){
        for(int i = 1; i <= n; i++){
            edg[i].clear();
        }
        cin >> n;
        for(int i = 1; i < n; i++){
            cin >> e >> u;
            edg[e].pb(u);
            edg[u].pb(e);
        }
        mx = 0;
        treeDp(1,0);
    //for(int i = 1; i <= n; i++) cout << d[i] << " ";
    //cout << endl;
        cout << mx << endl;
    }
}