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 ci.

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 ≤ nu ≠ 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 c1, c2, ..., cn (1 ≤ ci ≤ 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.

Examples
input
4
1 2
2 3
3 4
1 2 1 1
output
YES
2
input
3
1 2
2 3
1 2 3
output
YES
2
input
4
1 2
2 3
3 4
1 2 1 2
output
NO

题解:

规定一个特殊边,也就是这条边的两个端点的颜色是不一样的,找出所有的特殊边,同时记录相关颜色
出现的次数,如果有一种颜色出现的次数和边的数目是相同的,那么就存在这么一个点。


 #include<set>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+;
struct Node
{
int u,v;
};
int n;
int d[maxn];
int col[maxn];
Node e[maxn];
int main()
{
scanf("%d",&n);
for(int i=; i<n; i++)
scanf("%d%d",&e[i].u,&e[i].v);
for(int i=; i<=n; i++)
scanf("%d",&col[i]);
int tot=;
for(int i=; i<n; i++)
{
if(col[e[i].u]!=col[e[i].v])
{
tot++;
d[e[i].u]++,d[e[i].v]++;
}
}
for(int i=;i<=n;i++)
{
if(d[i]==tot)
{
printf("YES\n%d\n",i);
return ;
}
}
printf("NO\n");
return ;
}

Codeforces 764C Timofey and a tree的更多相关文章

  1. Codeforces 763A. Timofey and a tree

    A. Timofey and a tree 题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样.$N,C \le 10^5$ 想法:这个叫换根吧.先求出一个点合法即其儿子的子树 ...

  2. 【codeforces 764C】Timofey and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. Codeforces Round #395 (Div. 2) C. Timofey and a tree

    地址:http://codeforces.com/contest/764/problem/C 题目: C. Timofey and a tree time limit per test 2 secon ...

  4. 763A - Timofey and a tree

    A. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)

    codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  6. codeforces 812E Sagheer and Apple Tree(思维、nim博弈)

    codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...

  7. codeforces 220 C. Game on Tree

    题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...

  8. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. Codeforces Round #395 C. Timofey and a tree

    package codeforces; import java.util.*; public class CodeForces_764C_Timofey_and_a_tree { static fin ...

随机推荐

  1. 目标检测:yolo-v3与faster-rcnn

    一. 算法背景 1. 机器视觉实际应用往往涉及包含多个物体的复杂场景,基于深度卷积神经网络的特征提取器,需要结合其他算法来准确定位多个目标,并进行识别. 2. 工业领域,目标检测算法在安防和质检系统都 ...

  2. php面试笔记(7)-php基础知识-文件及目录处理考点

    本文是根据慕课网Jason老师的课程进行的PHP面试知识点总结和升华,如有侵权请联系我进行删除,email:guoyugygy@163.com 在面试中,考官往往喜欢基础扎实的面试者,而文件及目录处理 ...

  3. codewars--js--Simple string expansion+ repeat(),includes()方法

    问题描述: Consider the following expansion: solve("3(ab)") = "ababab" -- "ab&qu ...

  4. 【STM32H7教程】第62章 STM32H7的MDMA,DMA2D和通用DMA性能比较

    完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第62章       STM32H7的MDMA,DMA2D和通 ...

  5. asp.net core 3.x Identity

    一.前言 这方面的资料很多,重复的写没必要,但是最近一直在学习身份验证和授权相关东东,为了成体系还是写一篇,主要是从概念上理解identity系统. 参考:https://www.cnblogs.co ...

  6. Orleans[NET Core 3.1] 学习笔记(四)( 3 )监控Orleans Silo的方式 OrleansDashboard

    简介 Orleans用起来的确很爽,更爽的是咱们有能监控它的工具. OrleansDashboard 这个工具是一个可视化的Silo监控工具,Silo和Grain的活跃状态一目了然,各个接口的响应速度 ...

  7. mysql必知必会--排序检索数据

    排序数据 其实,检索出的数据并不是以纯粹的随机顺序显示的.如果不排 序,数据一般将以它在底层表中出现的顺序显示.这可以是数据最初 添加到表中的顺序.但是,如果数据后来进行过更新或删除,则此顺 序将会受 ...

  8. Nginx 和Apache 中的虚拟主机的概念

    在部署环境的时候,有时候会引用到虚拟主机的概念,什么是虚拟主机呢,博主之前一直把虚拟主机的概念没搞清楚,导致在部署的时候,一直动不动就404 ,或者500,或者服务器不通 所以,什么是虚拟主机呢? 虚 ...

  9. LeetCode 面试题 02.06. 回文链表

    题目链接:https://leetcode-cn.com/problems/palindrome-linked-list-lcci/ 编写一个函数,检查输入的链表是否是回文的. 示例 1: 输入: 1 ...

  10. Python 实现选择排序

    选择排序算法步骤: 找到数组中最小的那个元素中, 将它和数组的第一个元素交换位置, 在剩下的元素中找到最小的元素,将它和数组的第二个元素交换位置, 如此往复,知道将整个数组排序. 逐步分析: 假设一个 ...