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

Previous submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>
using namespace std;
#ifdef local
    #include "/home/enzo/Documents/Prog/OBItraining/debug.h"
    #define pr(...) debug(#__VA_ARGS__, __VA_ARGS__)
    #define prs(...) debug_nameless(__VA_ARGS__)
#else
    #define pr(...) 69
    #define prs(...) 69
#endif
typedef long long ll;


void solve(){

    int n; cin >> n;
    vector<int> out(n+1);
    vector<vector<int>> grafo(n+1);
    vector<pair<int,int>> edges;
    for(int i = 0; i < n-1; i++){
        int a,b; cin >> a >> b;
        edges.push_back({a,b});
        out[a]++, out[b]++;
        grafo[a].push_back(b);
        grafo[b].push_back(a);
    }   

    int ans = 0;
    for(int i = 1; i <= n; i++){
        int cur = 1;  
        
        for(auto a : grafo[i]) out[a]--;
        cur += (out[i]-1);
        out[i] = 0;
        
        cur += (*max_element(out.begin(), out.end()) - 1);
        for(auto a : grafo[i]) out[a]++;
        out[i] = grafo[i].size();

        ans = max(ans, cur);
    }   

    cout << ans << '\n';

}


int main(){
    cin.tie(0), ios_base::sync_with_stdio(false);

    int t; cin >> t;
    while(t--) solve();

}   

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
#ifdef local
    #include "/home/enzo/Documents/Prog/OBItraining/debug.h"
    #define pr(...) debug(#__VA_ARGS__, __VA_ARGS__)
    #define prs(...) debug_nameless(__VA_ARGS__)
#else
    #define pr(...) 69
    #define prs(...) 69
#endif
typedef long long ll;

struct SegTree{ //segtree 0 indexada sem lazy
  int n;
  struct Node{
    int val;
    Node operator+(Node other) const{
      return {max(this->val,other.val)};
    }
    Node operator=(int x){
      return {this->val = x};
    }
  };
  Node neutral = {0};
  vector <Node> t;

  SegTree(vector <int> a){
    n = a.size();
    t.resize(4*n);
    build(a, 1, 0, n-1);
  }

  void build(vector <int>& a, int v, int tl, int tr) {
    if (tl == tr) {
      t[v] = a[tl];
    } else {
      int tm = (tl + tr) / 2;
      build(a, v*2, tl, tm);
      build(a, v*2+1, tm+1, tr);
      t[v] = t[v*2] + t[v*2+1];
    }
  }

  Node query(int l, int r){
    return query(1, 0, n-1, l, r);
  }

  Node query(int v, int tl, int tr, int l, int r){
    if (l > r) 
      return neutral;
    if (l == tl && r == tr) {
      return t[v];
    }
    int tm = (tl + tr) / 2;
    return query(v*2, tl, tm, l, min(r, tm))
      + query(v*2+1, tm+1, tr, max(l, tm+1), r);
  }

  void update(int pos, int val){
    update(1, 0, n-1, pos, val);
  }

  void update(int v, int tl, int tr, int pos, int new_val){
    if (tl == tr) {
      t[v] = new_val;
    } else {
      int tm = (tl + tr) / 2;
      if (pos <= tm)
        update(v*2, tl, tm, pos, new_val);
      else
        update(v*2+1, tm+1, tr, pos, new_val);
      t[v] = t[v*2] + t[v*2+1];
    }
  }
};


void solve(){

    int n; cin >> n;
    vector<int> out(n+1);
    vector<vector<int>> grafo(n+1);
    vector<pair<int,int>> edges;
    for(int i = 0; i < n-1; i++){
        int a,b; cin >> a >> b;
        edges.push_back({a,b});
        out[a]++, out[b]++;
        grafo[a].push_back(b);
        grafo[b].push_back(a);
    }   

    SegTree seg(out);

    int ans = 0;
    for(int i = 1; i <= n; i++){
        int cur = 1;  
        
        for(auto a : grafo[i]) seg.update(a, out[a]-1);
        cur += (out[i]-1);
        seg.update(i, 0);
        
        cur += seg.query(1, n).val - 1;
        for(auto a : grafo[i]) seg.update(a, out[a]);
        seg.update(i, grafo[i].size());

        ans = max(ans, cur);
    }   

    cout << ans << '\n';

}


int main(){
    cin.tie(0), ios_base::sync_with_stdio(false);

    int t; cin >> t;
    while(t--) solve();

}