E. Centroids

Codeforces
IDCF709E
Time4000ms
Memory512MB
Difficulty
dfs and similardpgraphstrees
English · Original
Chinese · Translation
Formal · Original
_Tree_ is a connected acyclic graph. Suppose you are given a tree consisting of _n_ vertices. The vertex of this tree is called _centroid_ if the size of each connected component that appears if this vertex is removed from the tree doesn't exceed . You are given a tree of size _n_ and can perform no more than one edge replacement. _Edge replacement_ is the operation of removing one edge from the tree (without deleting incident vertices) and inserting one new edge (without adding new vertices) in such a way that the graph remains a tree. For each vertex you have to determine if it's possible to make it centroid by performing no more than one edge replacement. ## Input The first line of the input contains an integer _n_ (2 ≤ _n_ ≤ 400 000) — the number of vertices in the tree. Each of the next _n_ - 1 lines contains a pair of vertex indices _u__i_ and _v__i_ (1 ≤ _u__i_, _v__i_ ≤ _n_) — endpoints of the corresponding edge. ## Output Print _n_ integers. The _i_\-th of them should be equal to 1 if the _i_\-th vertex can be made centroid by replacing no more than one edge, and should be equal to 0 otherwise. [samples] ## Note In the first sample each vertex can be made a centroid. For example, in order to turn vertex 1 to centroid one have to replace the edge (2, 3) with the edge (1, 3).
_树_ 是一个连通无环图。假设你被给定一棵包含 #cf_span[n] 个顶点的树。如果移除该顶点后,生成的每个连通分量的大小均不超过 #cf_span[n / 2],则称该顶点为 _重心_。 你被给定一棵大小为 #cf_span[n] 的树,可以执行至多一次边替换操作。_边替换_ 是指从树中移除一条边(不删除其关联的顶点),并插入一条新的边(不添加新顶点),使得图仍保持为一棵树。对于每个顶点,你需要判断是否可以通过执行至多一次边替换,使其成为重心。 输入的第一行包含一个整数 #cf_span[n] (#cf_span[2 ≤ n ≤ 400 000]) —— 树中顶点的数量。接下来的 #cf_span[n - 1] 行每行包含一对顶点编号 #cf_span[ui] 和 #cf_span[vi] (#cf_span[1 ≤ ui, vi ≤ n]) —— 对应边的两个端点。 请输出 #cf_span[n] 个整数。第 #cf_span[i] 个整数应为 #cf_span[1],如果第 #cf_span[i] 个顶点可以通过替换至多一条边成为重心;否则应为 #cf_span[0]。 在第一个样例中,每个顶点都可以被变为重心。例如,为了将顶点 #cf_span[1] 变为重心,需将边 #cf_span[(2, 3)] 替换为边 #cf_span[(1, 3)]。 ## Input 输入的第一行包含一个整数 #cf_span[n] (#cf_span[2 ≤ n ≤ 400 000]) —— 树中顶点的数量。接下来的 #cf_span[n - 1] 行每行包含一对顶点编号 #cf_span[ui] 和 #cf_span[vi] (#cf_span[1 ≤ ui, vi ≤ n]) —— 对应边的两个端点。 ## Output 请输出 #cf_span[n] 个整数。第 #cf_span[i] 个整数应为 #cf_span[1],如果第 #cf_span[i] 个顶点可以通过替换至多一条边成为重心;否则应为 #cf_span[0]。 [samples] ## Note 在第一个样例中,每个顶点都可以被变为重心。例如,为了将顶点 #cf_span[1] 变为重心,需将边 #cf_span[(2, 3)] 替换为边 #cf_span[(1, 3)]。
**Definitions** Let $ T = (V, E) $ be a tree with $ n = |V| $ vertices and $ n-1 $ edges. For a vertex $ v \in V $, let $ C_v $ denote the multiset of sizes of connected components of $ T - v $. A vertex $ v $ is a *centroid* if $ \max(C_v) \leq \left\lfloor \frac{n}{2} \right\rfloor $. An *edge replacement* is an operation removing an edge $ e \in E $ and adding a new edge $ e' \notin E $ such that the resulting graph remains a tree. **Constraints** 1. $ 2 \leq n \leq 400{,}000 $ 2. The input graph is a tree. **Objective** For each vertex $ v \in V $, determine whether there exists at most one edge replacement such that $ v $ becomes a centroid in the resulting tree. Output a binary vector $ (b_1, b_2, \dots, b_n) \in \{0,1\}^n $, where: $$ b_v = \begin{cases} 1 & \text{if } v \text{ can be made a centroid via } \leq 1 \text{ edge replacement}, \\ 0 & \text{otherwise}. \end{cases} $$
Samples
Input #1
3
1 2
2 3
Output #1
1 1 1
Input #2
5
1 2
1 3
1 4
1 5
Output #2
1 0 0 0 0
API Response (JSON)
{
  "problem": {
    "name": "E. Centroids",
    "description": {
      "content": "_Tree_ is a connected acyclic graph. Suppose you are given a tree consisting of _n_ vertices. The vertex of this tree is called _centroid_ if the size of each connected component that appears if this ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 4000,
      "memory_limit": 524288
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF709E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "_Tree_ is a connected acyclic graph. Suppose you are given a tree consisting of _n_ vertices. The vertex of this tree is called _centroid_ if the size of each connected component that appears if this ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "_树_ 是一个连通无环图。假设你被给定一棵包含 #cf_span[n] 个顶点的树。如果移除该顶点后,生成的每个连通分量的大小均不超过 #cf_span[n / 2],则称该顶点为 _重心_。\n\n你被给定一棵大小为 #cf_span[n] 的树,可以执行至多一次边替换操作。_边替换_ 是指从树中移除一条边(不删除其关联的顶点),并插入一条新的边(不添加新顶点),使得图仍保持为一棵树。对于每个顶点,...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ T = (V, E) $ be a tree with $ n = |V| $ vertices and $ n-1 $ edges.  \nFor a vertex $ v \\in V $, let $ C_v $ denote the multiset of sizes of connected components of $ T - v $.  ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments