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

Previous submission (verdict: Time limit exceeded on test 5):
#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll f(vector<vector<ll>>& adj, ll node, ll count, ll n, ll pre) {
    if (count == 0) {
        return 0;
    }
    if (node >= n) {
        return 0;
    }

    ll rem = 0, notrem = 0;

    if (pre == -1) {
        rem = adj[node].size() + f(adj, node + 1, count - 1, n, node);
    } else {
        rem = adj[node].size();
        if (find(adj[node].begin(), adj[node].end(), pre) != adj[node].end()) {
            rem--;
        }
        rem += f(adj, node + 1, count - 1, n, node);
    }

    notrem = f(adj, node + 1, count, n, pre);
    return max(rem, notrem);
}

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

    int t;
    cin >> t;

    while (t--) {
        ll n;
        cin >> n;
        vector<vector<ll>> adj(n);

        for (int i = 0; i < n - 1; i++) { 
            ll a, b;
            cin >> a >> b;
            a--;
            b--;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }

        if (n == 2) {
            cout << 0 << endl;
            continue;
        }

        cout << f(adj, 0, 2, n, -1)-1 << endl;
    }

    return 0;
}


Submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll f(vector<vector<ll>>& adj, ll node, ll count, ll n, ll pre) {
    if (count == 0) {
        return 0;
    }
    if (node >= n) {
        return 0;
    }

    ll rem = 0, notrem = 0;

    if (pre == -1) {
        rem = adj[node].size() + f(adj, node + 1, count - 1, n, node);
    } else {
        rem = adj[node].size();
        if (find(adj[node].begin(), adj[node].end(), pre) != adj[node].end()) {
            rem--;
        }
        rem += f(adj, node + 1, count - 1, n, node);
    }

    notrem = f(adj, node + 1, count, n, pre);
    return max(rem, notrem);
}

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

    int t;
    cin >> t;

    while (t--) {
        ll n;
        cin >> n;
        vector<vector<ll>> adj(n);

        for (int i = 0; i < n - 1; i++) { 
            ll a, b;
            cin >> a >> b;
            a--;
            b--;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }

        if (n == 2) {
            cout << 0 << endl;
            continue;
        }

        //cout << f(adj, 0, 2, n, -1)-1 << endl;
        vector<pair<ll,ll>> temp;
        for(int i=0;i<n-1;i++)
        {
          temp.push_back({adj[i].size(),i});
        }
        sort(temp.begin(),temp.end());
        ll ans=0;
        ans+=temp[temp.size()-1].first;
        ll pre=temp[temp.size()-1].second;
        ll temp1=LLONG_MIN;
        
        for(int i=0;i<n-1;i++)
        {
          if(pre==i)
          {
            continue;
          }
          else{
            if(find(adj[i].begin(), adj[i].end(), pre) != adj[i].end())
            {
              temp1 = max(temp1, static_cast<long long>(adj[i].size()) - 1 + ans);

            }
            else{
              temp1 = max(temp1, static_cast<long long>(adj[i].size())  + ans);

            }
          }
        }
        cout<<temp1-1<<endl;
    }

    return 0;
}