CF763A.Timofey and a tree

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

每年新年,季莫费和他的朋友们都会砍下一棵包含 nn 个顶点的树并带回家。之后,他们将这 nn 个顶点全部染色,其中第 ii 个顶点被染成颜色 cic_i。

现在到了季莫费的生日,他妈妈让他把这棵树处理掉。季莫费以如下方式移除这棵树:他用手握住某个顶点,其余所有顶点则向下移动,使整棵树以该被握住的顶点为根节点。随后,季莫费便将这棵树扔进垃圾桶。

季莫费不喜欢多种颜色混杂在一起。若一棵子树中包含不同颜色的顶点,则该子树会令他感到烦恼。季莫费希望找到一个顶点,当他用手握住它时,不会出现任何令他烦恼的子树。他不将整棵树本身视为子树,因为他无法看到根节点的颜色。

某个顶点的子树是指包含该顶点及其所有后代顶点的子图。

你的任务是判断是否存在这样一个顶点:当季莫费用手握住它时,不会有任何令他烦恼的子树。

输入格式

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.

第一行包含一个整数 nn(2≤n≤1052 \leq n \leq 10^5)——树中顶点的数量。

接下来的 n−1n-1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \leq u, v \leq n,u≠vu \neq v),表示顶点 uu 和 vv 之间存在一条边。保证给定的图是一棵树。

下一行包含 nn 个整数 c1,c2,…,cnc_1, c_2, \dots, c_n(1≤ci≤1051 \leq c_i \leq 10^5),表示各顶点的颜色。

输出格式

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.

如果蒂莫菲无法以不令他烦恼的方式拿起这棵树,则在单独一行中输出 "NO"。

否则,在第一行输出 "YES";在第二行输出蒂莫菲应手持的顶点编号。若存在多个答案,输出任意一个即可。

输入输出样例

  • 输入#1

    4
    1 2
    2 3
    3 4
    1 2 1 1

    输出#1

    YES
    2
  • 输入#2

    3
    1 2
    2 3
    1 2 3

    输出#2

    YES
    2
  • 输入#3

    4
    1 2
    2 3
    3 4
    1 2 1 2

    输出#3

    NO

输入解题思路,AI测评打分。不知道怎么写?

首页