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

Previous submission (verdict: Wrong answer on test 2):
#include <cstdio>
#include <queue>
#include <vector>
#define ll long long
using namespace std;
const int maxn = 2e5 + 50;
inline int rd() {
    int sum = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        sum = sum * 10 + ch - '0';
        ch = getchar();
    }
    return f * sum;
}
int n, t, ans;
struct F {
    int a, u;
} f[maxn];
vector<int> g[maxn];
void dfs(int u, int fa) {
    priority_queue<int> q;
    for (auto v : g[u]) {
        if (v == fa)
            continue;
        dfs(v, u);
        if (f[v].a > f[u].a)
            f[u].a = f[v].a, f[u].u = f[v].u;
        q.push(f[v].a);
        if (f[v].u == v)
            ans = max(ans, f[v].a + (int)g[u].size() - 1);
        else
            ans = max(ans, f[v].a + (int)g[u].size());
    }
    if ((int)g[u].size() - 1 > f[u].a)
        f[u].a = (int)g[u].size() - 1, f[u].u = u;
    if ((int)g[u].size() == 1)
        f[u].u = u;
    if (q.size() >= 2) {
        int x = q.top();
        q.pop();
        ans = max(ans, x + q.top());
    }
    while (!q.empty())
        q.pop();
}
void work() {
    n = rd();
    for (int i = 1; i < n; i++) {
        int u = rd(), v = rd();
        g[u].push_back(v), g[v].push_back(u);
    }
    ans = 0;
    dfs(1, 1);
    // for (int i = 1; i <= n; i++)
    //     printf("%d %d\n", f[i].u, f[i].a);
    for (int i = 1; i <= n; i++)
        f[i].u = f[i].a = 0, g[i].clear();
    printf("%d\n", ans);
}
int main() {
    // freopen("a.in","r",stdin);
    t = rd();
    while (t--)
        work();
    return 0;
}

Submission (verdict: Accepted):
#include <cstdio>
#include <queue>
#include <vector>
#define ll long long
using namespace std;
const int maxn = 2e5 + 50;
inline int rd() {
    int sum = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        sum = sum * 10 + ch - '0';
        ch = getchar();
    }
    return f * sum;
}
int n, t, ans;
struct F {
    int a, u;
} f[maxn];
vector<int> g[maxn];
void dfs(int u, int fa) {
    priority_queue<int> q;
    for (auto v : g[u]) {
        if (v == fa)
            continue;
        dfs(v, u);
        if (f[v].a > f[u].a)
            f[u].a = f[v].a, f[u].u = f[v].u;
        q.push(f[v].a);
        if (f[v].u == v)
            ans = max(ans, f[v].a + (int)g[u].size() - 1);
        else
            ans = max(ans, f[v].a + (int)g[u].size());
    }
    if ((int)g[u].size() - 1 > f[u].a)
        f[u].a = (int)g[u].size() - 1, f[u].u = u;
    if ((int)g[u].size() == 1)
        f[u].u = u;
    if (q.size() >= 2) {
        int x = q.top();
        q.pop();
        ans = max(ans, x + q.top() + 1);
    }
    while (!q.empty())
        q.pop();
}
void work() {
    n = rd();
    for (int i = 1; i < n; i++) {
        int u = rd(), v = rd();
        g[u].push_back(v), g[v].push_back(u);
    }
    ans = 0;
    dfs(1, 1);
    // for (int i = 1; i <= n; i++)
    //     printf("%d %d\n", f[i].u, f[i].a);
    for (int i = 1; i <= n; i++)
        f[i].u = f[i].a = 0, g[i].clear();
    printf("%d\n", ans);
}
int main() {
    // freopen("a.in","r",stdin);
    t = rd();
    while (t--)
        work();
    return 0;
}