地址:http://codeforces.com/contest/764/problem/C

题目:

C. Timofey and a tree
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

题意:给你一棵n个节点的树,每个节点都染了色,现在让你找出存不存在一个节点作为根,使不包含根节点的子树的所有节点颜色都一样。

思路:直接枚举出哪些边的两端节点颜色不一样,然后判断这些边是否有相同一个端点,有的话则这个相同点是根,否则不存在。注意点所有边的端点颜色都一样时要特判。

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int n,num,cl[K],u[K],v[K],ex,ey,fx,fy,ans;
PII ed[K];
int main(void)
{
cin>>n;
for(int i=;i<n;i++)
scanf("%d%d",&u[i],&v[i]);
for(int i=;i<=n;i++)
scanf("%d",&cl[i]);
for(int i=;i<n;i++)
if(cl[u[i]]!=cl[v[i]])
ed[++num]=MP(u[i],v[i]);
if(num==)
{
printf("YES\n1\n");
return ;
}
ex=ed[].first,ey=ed[].second;
fx=fy=ans=;
for(int i=;i<=num;i++)
{
if(fx && !(ex==ed[i].first||ex==ed[i].second)) fx=;
if(fy && !(ey==ed[i].first||ey==ed[i].second)) fy=;
if(!(fx||fy))
{
ans=;break;
}
}
if(ans)
{
printf("YES\n");
if(fx)
printf("%d\n",ex);
else
printf("%d\n",ey);
}
else
printf("NO\n");
return ;
}

Codeforces Round #395 (Div. 2) C. Timofey and a tree的更多相关文章

  1. 【树形DP】Codeforces Round #395 (Div. 2) C. Timofey and a tree

    标题写的树形DP是瞎扯的. 先把1看作根. 预处理出f[i]表示以i为根的子树是什么颜色,如果是杂色的话,就是0. 然后从根节点开始转移,转移到某个子节点时,如果其子节点都是纯色,并且它上面的那一坨结 ...

  2. Codeforces Round #395 (Div. 2) D. Timofey and rectangles

    地址:http://codeforces.com/contest/764/problem/D 题目: D. Timofey and rectangles time limit per test 2 s ...

  3. Codeforces Round #395 (Div. 2)B. Timofey and cubes

    地址:http://codeforces.com/contest/764/problem/B 题目: B. Timofey and cubes time limit per test 1 second ...

  4. 【分类讨论】Codeforces Round #395 (Div. 2) D. Timofey and rectangles

    D题: 题目思路:给你n个不想交的矩形并别边长为奇数(很有用)问你可以可以只用四种颜色给n个矩形染色使得相接触的 矩形的颜色不相同,我们首先考虑可不可能,我们分析下最多有几个矩形互相接触,两个时可以都 ...

  5. Codeforces Round #395 (Div. 2)(未完)

    2.2.2017 9:35~11:35 A - Taymyr is calling you 直接模拟 #include <iostream> #include <cstdio> ...

  6. Codeforces Round #395 (Div. 2)

    今天自己模拟了一套题,只写出两道来,第三道时间到了过了几分钟才写出来,啊,太菜了. A. Taymyr is calling you 水题,问你在z范围内  两个序列  n,2*n,3*n...... ...

  7. Codeforces Round #395 (Div. 2)(A.思维,B,水)

    A. Taymyr is calling you time limit per test:1 second memory limit per test:256 megabytes input:stan ...

  8. Codeforces Round #395 (Div. 2) D

    Description One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On t ...

  9. Codeforces Round #395 (Div. 2) B

    Description Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his ...

随机推荐

  1. XML简单学习

    XML简单概述 1.Extensible Markup language可扩展标记语言; 2.作用:具有层次性的描述有关系的数据: 体现在:描述数据关系:软件配置,以描述程序模块之间的关系: 语法介绍 ...

  2. vi 的使用,很详细

    来源:鸟哥的Linux私房菜 网址:http://vbird.dic.ksu.edu.tw/linux_basic/0310vi_2.php

  3. hdu 2117:Just a Numble(水题,模拟除法运算)

    Just a Numble Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. WPF实用知识点

    1.一个基本的WPF程序, 需要引入的程序集WindowsBase, PresentationCore, PresentationFramework using System; using Syste ...

  5. sql实际应用-递归查询

    1.既然要谈到sql,数据库表是必须的 2.数据结构     3.获取某个节点的所有子节点     传统的写法(sql2000) 很麻烦,暂且就不写了     来看看CTE的写法 CREATE PRO ...

  6. pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  7. CentOS 7安装与配置jdk-8u162

    一.下载jdk-8u162版本 jdk-8u162-linux-x64.rpm 二.上传jdk到centos下 上传完后的文件如下,文件只有读写权限,没有执行权限 使用如下命令授权,如果文件已经有了执 ...

  8. 关于sqlSessionTemplate

    SqlSessionTemplate是MyBatis-Spring的核心.这个类负责管理MyBatis的SqlSession,调用MyBatis的SQL方法,翻译异常.SqlSessionTempla ...

  9. mysql "ON DUPLICATE KEY UPDATE" 的使用

    ON DUPLICATE KEY UPDATE 语法并不是SQL的标准语法,如果在句尾指定该语法,它会根据指定的主键或者唯一标示索引来更新数据库的内容 具体的操作是想根据唯一标示查看数据库是否存在该记 ...

  10. vertical-align 使用参考

    在实现文字与图片垂直对齐的时候,发现了vertical-align的值也是很纷繁复杂,博客讲解各种不懂,最终还是找了CSS权威指南-中文第三版,看了前6章,终于弄明白了,如果你也有不懂的地方,请去看书 ...