#include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int N = 400010; struct Node { int fi, se; void insert(int x) { if (x > fi) { se = fi; fi = x; } else if (x > se) se = x; } int get(int x) { if (x == fi) return se; else return fi; } } dn[N]; int calc(int u); void dfs1(int u); void dfs2(int u); void add(int u, int v); int head[N], nxt[N << 1], to[N << 1], cnt; int n, siz[N], fa[N], son[N], up[N]; int main() { int i, u, v, mx, sz; scanf("%d", &n); for (i = 1; i < n; ++i) { scanf("%d%d", &u, &v); add(u, v); add(v, u); } dfs1(1); dfs2(1); for (u = 1; u <= n; ++u) { v = son[u]; if (v == fa[u]) { sz = n - siz[u]; mx = up[u]; } else { sz = siz[v]; mx = dn[v].fi; } printf("%d ", sz - mx <= n / 2); } return 0; } void dfs1(int u) { int i, v; siz[u] = 1; for (i = head[u]; i; i = nxt[i]) { v = to[i]; if (v == fa[u]) continue; fa[v] = u; dfs1(v); siz[u] += siz[v]; dn[u].insert(calc(v)); } } void dfs2(int u) { int i, v; if (n - siz[u] <= n / 2) up[u] = n - siz[u]; else up[u] = max(up[fa[u]], dn[fa[u]].get(calc(u))); for (i = head[u]; i; i = nxt[i]) { v = to[i]; if (v == fa[u]) continue; dfs2(v); if (siz[v] > siz[son[u]]) son[u] = v; } if (n - siz[u] > siz[son[u]]) son[u] = fa[u]; } int calc(int u) { return siz[u] <= n / 2 ? siz[u] : dn[u].fi; } void add(int u, int v) { nxt[++cnt] = head[u]; head[u] = cnt; to[cnt] = v; }
|
评论正在加载中...如果评论较长时间无法加载,你可以 搜索对应的 issue 或者 新建一个 issue 。