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

Previous submission (verdict: Wrong answer on test 26):
// Created by Rishi Sharma (@rishisharmapro) on 08-02-2025 at 13:32
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>

using namespace std;

#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())

const ll MOD = 1e9 + 7;
const ll INF = 1e18;

void solve() {
    int n, maxComponents = 0;
    cin >> n;

    map<int , vector<int>> adjList;
    map<int, int> inDegree;
    int maxInDegree = 0;
    for(int i = 0; i < n-1; i++) {
        int u, v;
        cin >> u >> v;
        adjList[u].pb(v);
        adjList[v].pb(u);
        inDegree[u]++;
        inDegree[v]++;
        maxInDegree = max(maxInDegree, max(inDegree[u], inDegree[v]));
    }

    if(n ==  2) {
        cout << 0 << endl;
        return;
    }
    
    // calculating the nodes with maximum in degree
    set<int> nodes;
    for(auto i:inDegree) {
        if(i.second == maxInDegree) {
            nodes.insert(i.first);
        }
    }
    map<int, int> inDegreeAdjacent;
    // now finding the nodes with maximum in degree but there adjacent having minimum in degree
    for(auto i:nodes) {
        int degree = 0;
        for(auto j:adjList[i]) {
            degree += inDegree[j];
        }
        inDegreeAdjacent[degree] = i;
    }

    int maxIndegreeNode = 0;
    maxIndegreeNode = inDegreeAdjacent.begin()->second;
    maxComponents = inDegree[maxIndegreeNode];
    // removing the node with maximum in degree from inDegree
    for(auto i:adjList[maxIndegreeNode]) {
        inDegree[i]--;
    }
    inDegree.erase(maxIndegreeNode);
    // cout << endl << maxIndegreeNode << " " << maxComponents << endl;
    // if(sz(nodes) > 1) {
    //     maxComponents += (inDegreeAdjacent.begin()++)->first;
    //     cout << maxComponents << endl;
    //     return;
    // }
    
    int ans = maxComponents;
    maxInDegree = 0;
    int node = maxIndegreeNode;
    maxIndegreeNode = 0;
    for(auto i:inDegree) {
        if(i.second > maxInDegree) {
            maxInDegree = i.second;
            maxIndegreeNode = i.first;
        }
    }
    // the node is directly connected to the removed node
    if(find(all(adjList[node]), maxIndegreeNode) != adjList[node].end()) {
        // if(maxInDegree > 1) {
        //     ans += maxInDegree - 2;
        // }
        // else {
        //     ans += maxInDegree - 1;
        // }
        ans += maxInDegree - 1;
        // auto it = find(all(adjList[node]), maxIndegreeNode);
        // cout << maxIndegreeNode << " " << maxInDegree << endl;
    }
    // the node is not directly connected to the removed node
    else {
        ans += maxInDegree - 1;
    }

    cout << ans << endl;
}

int main() {
    // problem: https://codeforces.com/contest/2063/problem/C
    fast_io;
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Submission (verdict: Accepted):
// Created by Rishi Sharma (@rishisharmapro) on 08-02-2025 at 13:32
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>

using namespace std;

#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())

const ll MOD = 1e9 + 7;
const ll INF = 1e18;

void solve() {
    int n, maxComponents = 0;
    cin >> n;

    map<int , vector<int>> adjList;
    map<int, int> inDegree;
    int maxInDegree = 0;
    for(int i = 0; i < n-1; i++) {
        int u, v;
        cin >> u >> v;
        adjList[u].pb(v);
        adjList[v].pb(u);
        inDegree[u]++;
        inDegree[v]++;
        maxInDegree = max(maxInDegree, max(inDegree[u], inDegree[v]));
    }

    if(n ==  2) {
        cout << 0 << endl;
        return;
    }
    
    // calculating the nodes with maximum in degree
    set<int> nodes;
    for(auto i:inDegree) {
        if(i.second == maxInDegree) {
            nodes.insert(i.first);
        }
    }
    map<int, int> inDegreeAdjacent;
    // now finding the nodes with maximum in degree but there adjacent having minimum in degree
    for(auto i:nodes) {
        int degree = 0;
        for(auto j:adjList[i]) {
            if(inDegree[j] == maxInDegree)
                degree += inDegree[j];
        }
        inDegreeAdjacent[degree] = i;
    }

    int maxIndegreeNode = 0;
    maxIndegreeNode = inDegreeAdjacent.begin()->second;
    maxComponents = inDegree[maxIndegreeNode];
    // removing the node with maximum in degree from inDegree
    for(auto i:adjList[maxIndegreeNode]) {
        inDegree[i]--;
    }
    inDegree.erase(maxIndegreeNode);
    // cout << endl << maxIndegreeNode << " " << maxComponents << endl;
    
    int ans = maxComponents;
    maxInDegree = 0;
    int node = maxIndegreeNode;
    maxIndegreeNode = 0;
    for(auto i:inDegree) {
        if(i.second > maxInDegree) {
            maxInDegree = i.second;
            maxIndegreeNode = i.first;
        }
    }
    // the node is directly connected to the removed node
    // if(find(all(adjList[node]), maxIndegreeNode) != adjList[node].end()) {
    //     // if(maxInDegree > 1) {
    //     //     ans += maxInDegree - 2;
    //     // }
    //     // else {
    //     //     ans += maxInDegree - 1;
    //     // }
    //     // auto it = find(all(adjList[node]), maxIndegreeNode);
    //     // cout << maxIndegreeNode << " " << maxInDegree << endl;
    // }
    // // the node is not directly connected to the removed node
    // else {
    //     ans += maxInDegree - 1;
    // }

    ans += maxInDegree - 1;
    cout << ans << endl;
}

int main() {
    // problem: https://codeforces.com/contest/2063/problem/C
    fast_io;
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}