C. Timofey and a tree

Codeforces
IDCF764C
Time2000ms
Memory256MB
Difficulty
dfs and similargraphsimplementationtrees
English · Original
Chinese · Translation
Formal · Original
Each New Year Timofey and his friends cut down a tree of _n_ vertices and bring it home. After that they paint all the _n_ its vertices, so that the _i_\-th vertex gets color _c__i_. Now it's time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can. Timofey doesn't like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn't consider the whole tree as a subtree since he can't see the color of the root vertex. A subtree of some vertex is a subgraph containing that vertex and all its descendants. Your task is to determine if there is a vertex, taking which in hands Timofey wouldn't be annoyed. ## Input The first line contains single integer _n_ (2 ≤ _n_ ≤ 105) — the number of vertices in the tree. Each of the next _n_ - 1 lines contains two integers _u_ and _v_ (1 ≤ _u_, _v_ ≤ _n_, _u_ ≠ _v_), denoting there is an edge between vertices _u_ and _v_. It is guaranteed that the given graph is a tree. The next line contains _n_ integers _c_1, _c_2, ..., _c__n_ (1 ≤ _c__i_ ≤ 105), denoting the colors of the vertices. ## Output Print "_NO_" in a single line, if Timofey can't take the tree in such a way that it doesn't annoy him. Otherwise print "_YES_" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them. [samples]
每年新年,蒂莫菲和他的朋友们会砍倒一棵包含 #cf_span[n] 个顶点的树并带回家。之后,他们将这 #cf_span[n] 个顶点全部涂色,使得第 #cf_span[i] 个顶点被涂成颜色 #cf_span[ci]。 现在到了蒂莫菲的生日,他的母亲让他把树拿走。蒂莫菲按照以下方式移除树:他用手握住某个顶点,其余所有顶点会向下移动,使得树以所选顶点为根。然后蒂莫菲将树扔进垃圾桶。 蒂莫菲不喜欢多种颜色混合在一起。如果一个子树中包含不同颜色的顶点,他会感到厌烦。蒂莫菲希望找到一个顶点,他用手握住该顶点后,不会有任何让他厌烦的子树。他不将整棵树视为一个子树,因为他看不到根顶点的颜色。 某个顶点的子树是指包含该顶点及其所有后代的子图。 你的任务是判断是否存在一个顶点,使得蒂莫菲握住它后不会感到厌烦。 第一行包含一个整数 #cf_span[n](#cf_span[2 ≤ n ≤ 10^5])——树中顶点的数量。 接下来的 #cf_span[n - 1] 行,每行包含两个整数 #cf_span[u] 和 #cf_span[v](#cf_span[1 ≤ u, v ≤ n], #cf_span[u ≠ v]),表示顶点 #cf_span[u] 和 #cf_span[v] 之间有一条边。保证给定图是一棵树。 下一行包含 #cf_span[n] 个整数 #cf_span[c1, c2, ..., cn](#cf_span[1 ≤ ci ≤ 10^5]),表示每个顶点的颜色。 如果蒂莫菲无法找到一个合适的顶点使得他不会感到厌烦,请在一行中输出 "_NO_"。 否则,第一行输出 "_YES_",第二行输出蒂莫菲应握住的顶点编号。如果有多个答案,输出任意一个即可。 ## Input 第一行包含一个整数 #cf_span[n](#cf_span[2 ≤ n ≤ 10^5])——树中顶点的数量。接下来的 #cf_span[n - 1] 行,每行包含两个整数 #cf_span[u] 和 #cf_span[v](#cf_span[1 ≤ u, v ≤ n], #cf_span[u ≠ v]),表示顶点 #cf_span[u] 和 #cf_span[v] 之间有一条边。保证给定图是一棵树。下一行包含 #cf_span[n] 个整数 #cf_span[c1, c2, ..., cn](#cf_span[1 ≤ ci ≤ 10^5]),表示每个顶点的颜色。 ## Output 如果蒂莫菲无法找到一个合适的顶点使得他不会感到厌烦,请在一行中输出 "_NO_"。否则,第一行输出 "_YES_",第二行输出蒂莫菲应握住的顶点编号。如果有多个答案,输出任意一个即可。 [samples]
**Definitions:** - Let $ T = (V, E) $ be a tree with $ |V| = n $, $ n \geq 2 $. - Let $ c: V \to \mathbb{N} $ be a coloring function, where $ c(i) $ is the color of vertex $ i $. - For a rooted tree with root $ r $, the *subtree* of a vertex $ v \neq r $ is the induced subgraph consisting of $ v $ and all its descendants. - A subtree is *annoying* if it contains vertices of more than one distinct color. - The entire tree (rooted at $ r $) is **not** considered a subtree for the purpose of annoyance. **Given:** - Tree $ T $ with $ n $ vertices. - Coloring $ c: V \to \{1, 2, \dots, 10^5\} $. - For a candidate root $ r \in V $, we require: For every vertex $ v \in V \setminus \{r\} $, the subtree rooted at $ v $ is **monochromatic** (i.e., all vertices in the subtree have the same color). **Objective:** Determine whether there exists a vertex $ r \in V $ such that, when the tree is rooted at $ r $, **all subtrees** (of all non-root vertices) are monochromatic. If such an $ r $ exists, output: ``` YES r ``` Otherwise, output: ``` NO ``` --- **Formal Condition for Valid Root $ r $:** Let $ T_r $ denote the tree rooted at $ r $. For all $ v \in V \setminus \{r\} $, let $ S_v $ be the set of vertices in the subtree rooted at $ v $. Then $ r $ is valid if and only if: $$ \forall v \in V \setminus \{r\}, \quad \forall u, w \in S_v, \quad c(u) = c(w) $$ Equivalently: For every child $ v $ of $ r $, the entire connected component of $ T \setminus \{r\} $ containing $ v $ must be monochromatic. And recursively: for every descendant $ u $ of $ v $, the subtree rooted at $ u $ must also be monochromatic — but this is implied if every *direct* subtree under $ r $ is monochromatic and the coloring is consistent downward. **Note:** The condition reduces to: When removing $ r $, each connected component (i.e., subtree of each child of $ r $) must be monochromatic. Thus, the problem reduces to: > Does there exist a vertex $ r $ such that, in the forest $ T \setminus \{r\} $, every connected component is monochromatic? This is the formal mathematical statement of the problem.
Samples
Input #1
4
1 2
2 3
3 4
1 2 1 1
Output #1
YES
2
Input #2
3
1 2
2 3
1 2 3
Output #2
YES
2
Input #3
4
1 2
2 3
3 4
1 2 1 2
Output #3
NO
API Response (JSON)
{
  "problem": {
    "name": "C. Timofey and a tree",
    "description": {
      "content": "Each New Year Timofey and his friends cut down a tree of _n_ vertices and bring it home. After that they paint all the _n_ its vertices, so that the _i_\\-th vertex gets color _c__i_. Now it's time fo",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF764C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Each New Year Timofey and his friends cut down a tree of _n_ vertices and bring it home. After that they paint all the _n_ its vertices, so that the _i_\\-th vertex gets color _c__i_.\n\nNow it's time fo...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "每年新年,蒂莫菲和他的朋友们会砍倒一棵包含 #cf_span[n] 个顶点的树并带回家。之后,他们将这 #cf_span[n] 个顶点全部涂色,使得第 #cf_span[i] 个顶点被涂成颜色 #cf_span[ci]。\n\n现在到了蒂莫菲的生日,他的母亲让他把树拿走。蒂莫菲按照以下方式移除树:他用手握住某个顶点,其余所有顶点会向下移动,使得树以所选顶点为根。然后蒂莫菲将树扔进垃圾桶。\n\n蒂莫菲不喜...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions:**\n\n- Let $ T = (V, E) $ be a tree with $ |V| = n $, $ n \\geq 2 $.\n- Let $ c: V \\to \\mathbb{N} $ be a coloring function, where $ c(i) $ is the color of vertex $ i $.\n- For a rooted tree ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments