package codeforces;
import java.util.*;
public class CodeForces_764C_Timofey_and_a_tree {
static final int N=(int) (2e5+10);
@SuppressWarnings("unchecked")
static ArrayList<Integer> a[]=new ArrayList[N];
static int book[]=new int[N];
static int c[]=new int[N];
static void dfs(int u,int fa)
{
int v;
for(int i=0; i<a[u].size(); i++)
{
v=(int) a[u].get(i);
if(v!=fa)
{
dfs(v,u);
if(c[v]!=c[u])
{
book[u]++;
book[v]++;
}
}
}
}
static void solve(){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){ int n = sc.nextInt();
int u,v;
for(int i=1; i<n; i++)
{
u=sc.nextInt();
v=sc.nextInt();
if(a[u]==null) a[u]=new ArrayList<Integer>();
if(a[v]==null) a[v]=new ArrayList<Integer>();
a[u].add(v);
a[v].add(u);
}
Arrays.fill(book,0);
for(int i=1; i<=n; i++) c[i]=sc.nextInt();
dfs(1,-1);
int ans2=n,flag=0,maxx=0;
for(int i=1; i<=n; i++)
{
if(book[i]>maxx)
{
maxx=book[i];
ans2=i;
}
}
for(int i=0; i<a[ans2].size(); i++)
{
v=(int)a[ans2].get(i);
book[ans2]-=book[v];
book[v]=0;
}
if(book[ans2]!=0) System.out.println("NO");
else
{
for(int i=1; i<=n; i++)
{
if(book[i]!=0)
{
flag=1;
break;
}
}
if(flag==1) System.out.println("NO");
else
System.out.println("YES\n"+ans2);
}
}
}
public static void main(String args[]){
solve();
}
}

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

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

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

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

  3. Codeforces Round #395 (Div. 2)

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

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

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

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

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

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

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

  8. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  9. Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分

    D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...

随机推荐

  1. svchost.exe 占用内存过多

    http://www.tomshardware.com/forum/20583-63-svchost-netsvcs-speed By Lokesh Chandra: Just Go to Contr ...

  2. Objective-C 继承与类

    创建: 2018/01/20 完成: 2018/01/21 更新: 2018/01/22 标题前增加 [Objective-C]  继承的概念  父类与子类 ●继承: 继承其他类 ●父类: 被继承的类 ...

  3. 策略设计测试用例实践(2)--Pairwise(转)

    一.关于”好的“测试用例 在设计测试用例的时候有多种设计方法和策略可以使用,使得测试用例设计得更丰富,尽可能覆盖到更多的程序路径和功能场景.常见的测试用例设计方法被提到最多的就是等价类划分.边界值分析 ...

  4. Python机器学习算法 — 支持向量机(SVM)

    SVM--简介 <α∗j<C,可得:          构造决策函数:  5.求最优解         要求解的最优化问题如下:          考虑使用序列最小最优化算法(SMO,se ...

  5. $Hdu1381\ Crazy\ Search$

    前置芝士 :string 的 基本用法 string s = "hello world" ; string tmp(s,0,5) ; cout << tmp <& ...

  6. 有关lower_bound()函数的使用

    lower_bound()函数需要加载头文件#include<algorithm>,其基本用途是查找有序区间中第一个大于或等于某给定值的元素的位置,其中排序规则可以通过二元关系来表示. 函 ...

  7. ACM_求N^N的前5位数和后5位数(数论)

    NNNNN Time Limit: 2000/1000ms (Java/Others) Problem Description: 对于整数N,求N^N的前5位和后5位(1057题加强版) Input: ...

  8. [转]我要学ASP.NET MVC 3.0(十二): MVC 3.0 使用自定义的Html控件

    本文转自:http://www.cnblogs.com/lukun/archive/2011/08/05/2128693.html 概述   在ASP.NET MVC框架中已经封装了很多基于Html标 ...

  9. Quartz.Net学习笔记(1)-完整的例子

    一.开发环境 系统:Win10 编译器:VS2013 .Net版本:4.5 Quartz版本:2.3.3 二.涉及程序集 Common.Logging.Core.dll Common.Logging. ...

  10. 设置打印机共享,适用Win7、Vista、xp,不用密码

    此处以HP M1213nf为例,其他型号大同小异. 1.设置打印机共享: "开始"→  "控制面板"→  "设备和打印机" 2.找到&quo ...