---
id: 1796
lang: en
translation_of: index.md
source_sha256: 0bf7ed6c76a83ffc88822def107c63ff6f9de5054389e5321bd6b41907f4d656
title: "Codeforce 708C: Centroids — Solution"
description: "The problem asks whether attaching the largest subtree of size at most n/2 to the root still leaves the subtree above n/2; solved by a linear Up & Down DP."
tags: []
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "The source title spells the contest “Codeforce” (no s); kept as written, as in the post slug."
  - "次小值 (second-smallest value) in the third paragraph is kept literally, although the code and the previous paragraph use the second-largest value."
  - "按照第一遍的做法 rendered literally as “following the approach of the first pass”."
---

## The $\Theta(n)$ Up & Down DP

We notice that this problem is really just about deciding whether, when we take, within the subtree, the largest subtree not exceeding $\frac{n}{2}$ and attach it to the root, the subtree can still stay $> \frac{n}{2}$.

We do one DP following the approach of the first pass, recording the largest and second-largest values of the current subtree.

Then consider how to get the Up DP values: first transition from the parent, then use the maximum and the second-smallest value (they cannot be in the same subtree, and that is the whole point of the second-smallest value) to update the current Up DP value.

<!--more-->

## Code

```cpp
// CF708C.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 400100;

int head[MAX_N], current, n, siz[MAX_N], dp[MAX_N][2], id[MAX_N], g[MAX_N], gsiz[MAX_N];
bool ans[MAX_N];

struct edge
{
    int to, nxt;
} edges[MAX_N << 1];

void addpath(int src, int dst)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    head[src] = current++;
}

void dfs_siz(int u, int fa)
{
    siz[u] = 1;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
        {
            dfs_siz(edges[i].to, u), siz[u] += siz[edges[i].to];
            int term = (siz[edges[i].to] <= (n >> 1)) ? siz[edges[i].to] : dp[edges[i].to][0];
            if (term > dp[u][0])
                swap(dp[u][0], dp[u][1]), dp[u][0] = term, id[u] = edges[i].to;
            else if (term > dp[u][1])
                dp[u][1] = term;
        }
}

void dfs(int u, int fa)
{
    gsiz[u] = n - siz[u];
    int term = (gsiz[u] <= (n >> 1)) ? gsiz[u] : g[u];
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
        {
            if (id[u] != edges[i].to)
                g[edges[i].to] = dp[u][0];
            else
                g[edges[i].to] = dp[u][1];
            g[edges[i].to] = max(g[edges[i].to], term);
            dfs(edges[i].to, u);
        }
}

void redfs(int u, int fa)
{
    bool pans = false, flag = true;
    if (gsiz[u] > (n >> 1))
        pans |= (gsiz[u] - g[u] <= (n >> 1)), flag = false;
    else
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (edges[i].to != fa && siz[edges[i].to] > (n >> 1))
            {
                flag = false;
                pans |= ((siz[edges[i].to] - dp[edges[i].to][0]) <= (n >> 1));
                break;
            }
    pans |= flag;
    ans[u] = pans;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
            redfs(edges[i].to, u);
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d", &n);
    for (int i = 1, u, v; i <= n - 1; i++)
        scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u);
    dfs_siz(1, 0), dfs(1, 0), redfs(1, 0);
    for (int i = 1; i <= n; i++)
        printf("%d ", ans[i] == true ? 1 : 0);
    return 0;
}
```
