地址: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
  1. 4
    1 2
    2 3
    3 4
    1 2 1 1
output
  1. YES
    2
input
  1. 3
    1 2
    2 3
    1 2 3
output
  1. YES
    2
input
  1. 4
    1 2
    2 3
    3 4
    1 2 1 2
output
  1. NO

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

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

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define MP make_pair
  6. #define PB push_back
  7. typedef long long LL;
  8. typedef pair<int,int> PII;
  9. const double eps=1e-;
  10. const double pi=acos(-1.0);
  11. const int K=1e6+;
  12. const int mod=1e9+;
  13.  
  14. int n,num,cl[K],u[K],v[K],ex,ey,fx,fy,ans;
  15. PII ed[K];
  16. int main(void)
  17. {
  18. cin>>n;
  19. for(int i=;i<n;i++)
  20. scanf("%d%d",&u[i],&v[i]);
  21. for(int i=;i<=n;i++)
  22. scanf("%d",&cl[i]);
  23. for(int i=;i<n;i++)
  24. if(cl[u[i]]!=cl[v[i]])
  25. ed[++num]=MP(u[i],v[i]);
  26. if(num==)
  27. {
  28. printf("YES\n1\n");
  29. return ;
  30. }
  31. ex=ed[].first,ey=ed[].second;
  32. fx=fy=ans=;
  33. for(int i=;i<=num;i++)
  34. {
  35. if(fx && !(ex==ed[i].first||ex==ed[i].second)) fx=;
  36. if(fy && !(ey==ed[i].first||ey==ed[i].second)) fy=;
  37. if(!(fx||fy))
  38. {
  39. ans=;break;
  40. }
  41. }
  42. if(ans)
  43. {
  44. printf("YES\n");
  45. if(fx)
  46. printf("%d\n",ex);
  47. else
  48. printf("%d\n",ey);
  49. }
  50. else
  51. printf("NO\n");
  52. return ;
  53. }

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. 要创建一个EJB,必须要至少编写哪些Java类和接口?

    要创建一个EJB,必须要至少编写哪些Java类和接口? A. 定义远程(或业务)接口 B. 定义本地接口 C. 定义Bean接口 D. 编写Bean的实现 解答:ABC

  2. rgb2yuv

    1.rgb2yuv422p 代码的运算速度取决于以下几个方面 1. 算法本身的复杂度,比如MPEG比JPEG复杂,JPEG比BMP图片的编码复杂. 2. CPU自身的速度和设计架构 3. CPU的总线 ...

  3. webpack文档翻译

    https://segmentfault.com/a/1190000007568507

  4. Log4j 汇总

    一.概念 .1. log4j是 是线程安全的 日志框架,高度可配置,可通过在运行时的外部文件配置. 默认情况下,日志管理在CLASSPATH 查找一个名为 log4j.properties 的文件. ...

  5. 动画间隔AnimationInterval 场景切换、图层叠加

    从这一个月的学习进度上来看算比较慢的了,从开始学习C++到初试cocos,这也是我做过的比较大的决定,从工作中里挤出时间来玩玩自己喜欢的游戏开发也是一件非常幸福的事情,虽然现在对cocos的了解还只是 ...

  6. encodeURI() 的用法

    定义和用法 encodeURI() 函数可把字符串作为 URI 进行编码.[通用资源标识符(Uniform Resource Identifier, 简称"URI")] 语法 en ...

  7. SQLServer中计算周

    --本周最大值与最小值.平均值 DECLARE @WeekMax float,@WeekMin float,@WeekAvg float,@AddDate varchar(20) DECLARE @W ...

  8. 1.java中Comparor与Comparable的问题

    1.Comparator中compare()与Comparable中compareTo()方法的区别 Treeset集合创建对象后, A:如果是空构造,即TreeSet<Student> ...

  9. submit按钮修改宽高的坑

    近些天对h5非常感兴趣,边工作边学习,虽然比较累,但过得很踏实.每天都要学习一点东西,这样才能对得起自己.好了,废话不多说,进入今天的主题. 今天遇到了一个非常有趣的东西,就是在修改submit按钮的 ...

  10. IO流入门-第四章-FileReader

    FileReader基本用法和方法示例 /* java.io.Reader java.io.InputStreamReader 转换流(字节输入流---->字符输入流) java.io.File ...