Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <limits>
#include <set>
#include <map>
#include <unordered_map>
#include <climits>
#include <queue>
using namespace std;

const long long MOD = 1000000007LL;

struct Node {
    int num;
    vector<Node*> edges;
    int size = 0;

    Node(int i) : num(i) {}

    void addEdge(Node* node) {
        edges.push_back(node);
        size++;

    }


};

int main() {
    int t;
    cin >> t;

    while (t--) {
        
        int n;
        cin >> n;
        vector<Node*> nodes;

        for (int i = 0; i < n; i++) {
            Node* p = new Node(i+1);
            nodes.push_back(p);
            
        }

        for (int i = 0; i < n - 1; i++) {
            int first, second;
            cin >> first >> second;
            nodes[first-1]->addEdge(nodes[second-1]);
            nodes[second-1]->addEdge(nodes[first-1]);
        }

        sort(nodes.begin(), nodes.end(), [](const Node* a, const Node* b) {
            if (a->size == b->size) return a->num < b->num;
            return a->size > b->size;
        });

        // for (auto k : nodes) cout << k->size << " ";
        // cout << endl;

        int connected = 1;

        int maxFirst = 0;
        auto first = nodes[0];
        auto second = nodes[1];

        for (Node* n : nodes[0]->edges) n->size--;

        sort(nodes.begin(), nodes.end(), [](const Node* a, const Node* b) {
            return a->size > b->size;
        });

        for (int i = 1; i < nodes.size(); i++) {
            int newConnect = nodes[0]->size + nodes[i]->size - 1;
            if (newConnect > maxFirst) {
                // cout << "New maxFirst: (" << first->num << ", " << nodes[i]->num << "): " << newConnect << endl;
                maxFirst = newConnect;
            }
        }
        for (Node* n : nodes[0]->edges) n->size++;

        sort(nodes.begin(), nodes.end(), [](const Node* a, const Node* b) {
            return a->size > b->size;
        });

        int maxSecond = 0;
        for (Node* n : nodes[1]->edges) n->size--;

        sort(nodes.begin(), nodes.end(), [](const Node* a, const Node* b) {
            return a->size > b->size;
        });
        for (int i = 2; i < nodes.size(); i++) {
            int newConnected = nodes[1]->size + nodes[i]->size - 1;
            if (newConnected > maxSecond) {
                maxSecond = newConnected;
                // cout << "New maxSecond: (" << second->num << ", " << nodes[i]->num << ") : " << newConnected << endl;
            }
        }

        cout << max(maxFirst, maxSecond) << endl;
        // cout << endl;




    }
}





Submission (verdict: Accepted):
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <limits>
#include <set>
#include <map>
#include <unordered_map>
#include <climits>
#include <queue>
using namespace std;

const long long MOD = 1000000007LL;

struct Node {
    int num;
    vector<Node*> edges;
    int size = 0;

    Node(int i) : num(i) {}

    void addEdge(Node* node) {
        edges.push_back(node);
        size++;

    }


};

int main() {
    int t;
    cin >> t;

    while (t--) {
        
        int n;
        cin >> n;
        vector<Node*> nodes;

        for (int i = 0; i < n; i++) {
            Node* p = new Node(i+1);
            nodes.push_back(p);
            
        }

        for (int i = 0; i < n - 1; i++) {
            int first, second;
            cin >> first >> second;
            nodes[first-1]->addEdge(nodes[second-1]);
            nodes[second-1]->addEdge(nodes[first-1]);
        }

        sort(nodes.begin(), nodes.end(), [](const Node* a, const Node* b) {
            if (a->size == b->size) return a->num < b->num;
            return a->size > b->size;
        });

        // for (auto k : nodes) cout << k->size << " ";
        // cout << endl;

        int connected = 1;

        int maxFirst = 0;
        auto first = nodes[0];
        auto second = nodes[1];

        for (Node* n : first->edges) n->size--;

        sort(nodes.begin(), nodes.end(), [](const Node* a, const Node* b) {
            if (a->size == b->size) return a->num < b->num;
            return a->size > b->size;
        });
        // for (auto k : nodes) cout << k->num << ": " << k->size << ", ";
        // cout << endl;
        for (int i = 0; i < nodes.size(); i++) {
            if (nodes[i] == first) continue;
            int newConnect = first->size + nodes[i]->size - 1;
            if (newConnect > maxFirst) {
                //cout << "New maxFirst: (" << first->num << ", " << nodes[i]->num << "): " << newConnect << endl;
                maxFirst = newConnect;
            }
        }
        for (Node* n : first->edges) n->size++;

        sort(nodes.begin(), nodes.end(), [](const Node* a, const Node* b) {
            if (a->size == b->size) return a->num < b->num;
            return a->size > b->size;
        });

        int maxSecond = 0;
        for (Node* n : second->edges) n->size--;

        sort(nodes.begin(), nodes.end(), [](const Node* a, const Node* b) {
            if (a->size == b->size) return a->num < b->num;
            return a->size > b->size;
        });
        //for (auto k : nodes) cout << k->num << ": " << k->size << ", ";
        //cout << endl;
        for (int i = 0; i < nodes.size(); i++) {
            if (nodes[i] == second) continue;
            int newConnected = nodes[1]->size + nodes[i]->size - 1;
            if (newConnected > maxSecond) {
                maxSecond = newConnected;
                //cout << "New maxSecond: (" << second->num << ", " << nodes[i]->num << ") : " << newConnected << endl;
            }
        }

        cout << max(maxFirst, maxSecond) << endl;
        // cout << endl;




    }
}