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. Centos7 使用Docker 部署mssql 2017

    mssql是.NET的标配,一般使用.NET的人基本都用mssql. 以前mssql只能支持windows平台,从微软打出 拥抱开源 的口号开始,mssql的2017 版本,开始支持linux系统. ...

  2. Objective-C编程 — 类和继承

    讲述面向对象中的一个重要概念——继承,使用继承 可以方便地在已有类的基础上进行扩展,定义一个具有父 类全部功能的新类. 父类和子类 我们在定义一个新类的时候,经常会遇到要定义的新类是某个类的扩展或者是 ...

  3. 虚拟机VMware官网最新版附密钥,kali,ubuntu,centos,deepin迅雷下载地址。

    以下全部都是官网的迅雷复制链接 版本都是当前时间可下载的最新版本 VMware官网迅雷下载链接: https://download3.vmware.com/software/wkst/file/VMw ...

  4. node.remove()

    node.remove()方法是在DOM 4规范中实现的,由于糟糕的浏览器支持,不建议使用.应该使用removeChild方法,但是该方法必须要清楚父元素,通常的做法是:node.parentNode ...

  5. 洛谷P1331-搜索基础-什么是矩形?(我的方案)

    原题链接:https://www.luogu.com.cn/problem/P1331 简单来说就是给出一个由‘#’和‘.‘组成的矩阵.需要识别存在几个矩形(被完全填充的).如果有矩形相互衔接则认为出 ...

  6. [Git:commit错误] Fatal: cannot do a partial commit during a merge

    注:本文出自博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 问题场景 今天进行Spring Boot版本升级,解决冲突后进行代码文件提交时出现这个错误. 上午 ...

  7. Python3 协程相关 - 学习笔记

    什么是协程 协程的优势 Python3中的协程 生成器 yield/send yield + send(利用生成器实现协程) 协程的四个状态 协程终止 @asyncio.coroutine和yield ...

  8. 【python基础语法】元祖、字典、集合、数据类型的分类(第4天课堂笔记)

    """ 列表:可变数据类型,对列表内部的元素可以进行修改 元祖: tuple,不可变数据类型,是不可以进行增删查改 元祖的方法: count 查找元素的数量 index ...

  9. 11、C++之const类成员变量,const成员函数

    //转载 类的成员函数后面加 const,表明这个函数不会对这个类对象的数据成员(准确地说是非静态数据成员)作任何改变. 在设计类的时候,一个原则就是对于不改变数据成员的成员函数都要在后面加 cons ...

  10. CSS实现同一行中图片和文本垂直居中

    1.为图片和文本都设置vertical-align:middle