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

Previous submission (verdict: Wrong answer on test 2):
#include <iostream>


//#define endl '\n'
#define pi acos(-1)
#define euler_e 2.71828
#define pii pair<int,int>
#define pb push_back
#define all(a) a.begin(),a.end()
#define ll long long
#define ull unsigned long long
#define io freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define read freopen("input.txt","r",stdin);
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
void mycode();
const ll int infLL=(1LL<<62);
const ll mod=1e10;
ll int ceil(ll int a,ll int b){return (a+b-1)/b;}
ll int min(ll int a, ll int b){if(a>b)return b;else return a;}
bool bit_check(ll int a,int i){
  if((a & (1LL<<i)))return 1;
  return 0;
}
ll int bit_toggle(ll int a,int i){
  return (a^(1LL<<i));
}
ll int bit_sum(ll int a,int i){
  return a+(1LL<<i);
}
ll int bit_sub(ll int a,int i){
  return a-(1LL<<i);
}
ll mod_power(ll x,ll y, ll p=mod){//x^y%p
  ll res = 1;
  x = x % p;
  while (y > 0) { 
    if (y & 1)res = (res*x) % p;
    y = y>>1;x = (x*x) % p;
  }
  return res;
}
ll int power_of(ll int a,int b){
  if(a==0)return -1;
  return 1+power_of(a/b,b);
}
ll power(ll int a, ll int b) {
    if(a==1)return 1;
    ll int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}
int main(){
    //io;
    boost;
    //read;
    //auto start = chrono::high_resolution_clock::now();
    mycode();
    //auto end = chrono::high_resolution_clock::now();cerr << "\nExecution time: "<< chrono::duration_cast<chrono::milliseconds>(end - start).count() << " ms" << endl;
    return 0;
}
#include <algorithm>
#include <set>
#include <vector>
void mycode() {
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        pair<ll,int> arr[n+5];
        vector<int> vc[n+2];
        int x,y;
        for(int i=0;i<n-1;i++){
            cin>>x>>y;
            vc[x].push_back(y);
            vc[y].push_back(x);
        }
        for(int i=1;i<=n;i++){
            arr[i] = {vc[i].size(),i};
        }
        sort(arr,arr+n);
        ll ans = 0;
        for(int i=1;i<=n;i++){
            set<int> st;
            ll tmp = 0;
            st.insert(i);
            for(auto &x:vc[i]){
                st.insert(x);
                tmp = max(tmp,vc[i].size() + vc[x].size() - 2LL);
            }
            for(int j = n;j>0;j--){
                if(!st.count(arr[j].second)){
                    tmp = max(tmp,vc[i].size() + arr[j].first - 1LL);
                    break;
                }
            }
            ans = max(ans, tmp);
        }
        cout<<ans<<endl;
    }
}


Submission (verdict: Accepted):
#include <iostream>


//#define endl '\n'
#define pi acos(-1)
#define euler_e 2.71828
#define pii pair<int,int>
#define pb push_back
#define all(a) a.begin(),a.end()
#define ll long long
#define ull unsigned long long
#define io freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define read freopen("input.txt","r",stdin);
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
void mycode();
const ll int infLL=(1LL<<62);
const ll mod=1e10;
ll int ceil(ll int a,ll int b){return (a+b-1)/b;}
ll int min(ll int a, ll int b){if(a>b)return b;else return a;}
bool bit_check(ll int a,int i){
  if((a & (1LL<<i)))return 1;
  return 0;
}
ll int bit_toggle(ll int a,int i){
  return (a^(1LL<<i));
}
ll int bit_sum(ll int a,int i){
  return a+(1LL<<i);
}
ll int bit_sub(ll int a,int i){
  return a-(1LL<<i);
}
ll mod_power(ll x,ll y, ll p=mod){//x^y%p
  ll res = 1;
  x = x % p;
  while (y > 0) { 
    if (y & 1)res = (res*x) % p;
    y = y>>1;x = (x*x) % p;
  }
  return res;
}
ll int power_of(ll int a,int b){
  if(a==0)return -1;
  return 1+power_of(a/b,b);
}
ll power(ll int a, ll int b) {
    if(a==1)return 1;
    ll int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}
int main(){
    //io;
    boost;
    //read;
    //auto start = chrono::high_resolution_clock::now();
    mycode();
    //auto end = chrono::high_resolution_clock::now();cerr << "\nExecution time: "<< chrono::duration_cast<chrono::milliseconds>(end - start).count() << " ms" << endl;
    return 0;
}
#include <algorithm>
#include <set>
#include <vector>
void mycode() {
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        pair<ll,int> arr[n+5];
        vector<int> vc[n+2];
        int x,y;
        for(int i=0;i<n-1;i++){
            cin>>x>>y;
            vc[x].push_back(y);
            vc[y].push_back(x);
        }
        for(int i=1;i<=n;i++){
            arr[i] = {vc[i].size(),i};
        }
        sort(arr+1,arr+n+1);
        ll ans = 0;
        for(int i=1;i<=n;i++){
            set<int> st;
            ll tmp = 0;
            st.insert(i);
            for(auto &x:vc[i]){
                st.insert(x);
                tmp = max(tmp,vc[i].size() + vc[x].size() - 2LL);
            }
            for(int j = n;j>0;j--){
                if(!st.count(arr[j].second)){
                    tmp = max(tmp,vc[i].size() + arr[j].first - 1LL);
                    break;
                }
            }
            ans = max(ans, tmp);
        }
        cout<<ans<<endl;
    }
}