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

Previous submission (verdict: Wrong answer on test 2):
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cmath>
#include <queue>
#include <algorithm>
#include <sstream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <climits>

using namespace std;

#define pb push_back
#define int long long
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define LOG(n) 31 - __builtin_clz(n)
#define nl  "\n"
#define ok  cout<<"OK\n"
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define free freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define ff first
#define ss second
#define vi vector<int>
#define vvi vector<vector<int> >
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef pair<int,int> pii;
typedef long double ld;
const long long INF = 1ll << 32;
const long long MAX_N = 1e12 + 5;
const long long MOD = 1e9 + 7;
const long long N = 9999999;
long double pi = 3.1415926535;

void show(vector<int> a) {for(auto it : a) cout<<it<<","; cout<<nl;}
int gcd(int a, int b){if(a == 0) return b; if(b == 0) return a;int r = min(a, b);while(r>0){if(a%r==0 && b%r==0){break;}r--;}return r;}

int sq(int n){if(n==0)return 0LL;int l=1,r=n;while(l<=r){int m=l+(r-l)/2;int p=m*m;if(p==n)return m;if(p<n)l=m+1;else r=m-1;}return l;}
int I;

void solve() {
    int n;
    cin>>n;
    
    vector<vector<int> > adj(n + 1);
    vector<int> deg(n + 1, 0);
    int element = -1, mxDegree = 0;
    for (int i = 0 ; i < n - 1 ; i++) {
        int u, v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        deg[u]++;
        deg[v]++;
    }
    
    multiset<int> maxDegree;
    for (int i = 1 ; i <= n ; i++) {
        maxDegree.insert(deg[i]);
        if (mxDegree < deg[i]) {
            mxDegree = deg[i];
            element = i;
        }
    }
    
    maxDegree.erase(--maxDegree.end());
    
    for (int node : adj[element]) {
        maxDegree.erase(maxDegree.find(deg[node]));
        maxDegree.insert(deg[node] - 1);
    }
    
    cout<<mxDegree + *maxDegree.rbegin() - 1<<endl;
    
}

int32_t main(){
    ios;
//    freopen("visitfj.in","r",stdin);
//    freopen("visitfj.out","w",stdout);
    int Test = 1;
    cin>>Test;
    for(I = 1 ; I <= Test ; I++)
    {
        solve();
    }
 
 
}


Submission (verdict: Wrong answer on test 2):
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cmath>
#include <queue>
#include <algorithm>
#include <sstream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <climits>

using namespace std;

#define pb push_back
#define int long long
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define LOG(n) 31 - __builtin_clz(n)
#define nl  "\n"
#define ok  cout<<"OK\n"
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define free freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define ff first
#define ss second
#define vi vector<int>
#define vvi vector<vector<int> >
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef pair<int,int> pii;
typedef long double ld;
const long long INF = 1ll << 32;
const long long MAX_N = 1e12 + 5;
const long long MOD = 1e9 + 7;
const long long N = 9999999;
long double pi = 3.1415926535;

void show(vector<int> a) {for(auto it : a) cout<<it<<","; cout<<nl;}
int gcd(int a, int b){if(a == 0) return b; if(b == 0) return a;int r = min(a, b);while(r>0){if(a%r==0 && b%r==0){break;}r--;}return r;}

int sq(int n){if(n==0)return 0LL;int l=1,r=n;while(l<=r){int m=l+(r-l)/2;int p=m*m;if(p==n)return m;if(p<n)l=m+1;else r=m-1;}return l;}
int I;

void solve() {
    int n;
    cin>>n;
    
    vector<vector<int> > adj(n + 1);
    vector<int> deg(n + 1, 0);
    int element = -1, mxDegree = 0;
    for (int i = 0 ; i < n - 1 ; i++) {
        int u, v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        deg[u]++;
        deg[v]++;
    }
    
    multiset<int> maxDegree;
    for (int i = 1 ; i <= n ; i++) {
        maxDegree.insert(deg[i]);
        if (mxDegree < deg[i]) {
            mxDegree = deg[i];
            element = i;
        }
    }
    int maxComponents = 0;
    maxDegree.erase(--maxDegree.end());
    for (int i = 2 ; i <= n ; i++) {
        if (deg[i] != mxDegree) continue;
        
        for (int node : adj[i]) {
            maxDegree.erase(maxDegree.find(deg[node]));
            maxDegree.insert(deg[node] - 1);
        }
        maxComponents = max(maxComponents, mxDegree + *maxDegree.rbegin() - 1);
        for (int node : adj[i]) {
            maxDegree.erase(maxDegree.find(deg[node] - 1));
            maxDegree.insert(deg[node]);
        }
    }
    
    cout<<maxComponents<<endl;
    
}

int32_t main(){
    ios;
//    freopen("visitfj.in","r",stdin);
//    freopen("visitfj.out","w",stdout);
    int Test = 1;
    cin>>Test;
    for(I = 1 ; I <= Test ; I++)
    {
        solve();
    }
 
 
}