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. xargs 主要用于不支持管道的shell命令*****

    变量置换,主要用于不支持管道的shell命令,如:rm.sed等,但有些命令需要占位符“{}”需要注意.比如:删除文件- ls|xargs -i rm -rf {} 文件改名-   ls|xargs ...

  2. [App Store Connect帮助]三、管理 App 和版本(6.3)转让 App:接受 App 转让

    您必须在转让发起的 60 天内接受转让. 必要职能:“帐户持有人”职能.请参见职能权限. 以具有“帐户持有人”职能用户的身份登录至 App Store Connect. 系统会显示一条通知,指示 Ap ...

  3. robotframework - User key 操作

    一.用户关键字操作思路 a.创建model1资源 b.在model下创建用户关键字 - 循环 c.测试套件下创建test_case/case2 & 用户关键字 d.测试套件中导入Resourc ...

  4. SpringCloud(Finchley版本)中Zull过滤器ResponseBoby返回中文乱码解决方案

    Spring Cloud带有"Cloud"的字样,但它并不是云计算解决方案,而是在Spring Boot基础上构建的,用于快速构建分布式系统的通用模式的工具集.使用Spring C ...

  5. Linux学习之02_Linuxd的文件权限与目录配置

    这里一些基本介绍就不介绍了,还是来介绍一下相关的命令 这一节重要的命令有这些: chgrp chown chmod 1.改变文件属性和权限 chgrp----改变文件所属用户组 chown----改变 ...

  6. ASP.Net 知识点总结(三)

    1.描述一下C#中索引器的实现过程,是否只能根据数字进行索引? 答:不是.可以用任意类型. 2.<%# %> 和 <% %> 有什么区别? 答:<%# %>表示绑定 ...

  7. CodeForces - 7D Palindrome Degree

    最近接触了一点字符串算法,其实也就是一个简单的最大回文串算法,给定字符串s,求出最大字符串长度. 算法是这样的, 用'#'将s字符串中的每个字符分隔,比如s = "aba",分割后 ...

  8. 构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination

    题目传送门 /* 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! */ /************************************** ...

  9. tns no listener

    ip 应该为192.168 实际上是 196.168

  10. java excel poi导入 过滤空行的方法 判断是否是空行

    private boolean isRowEmpty(Row row){ for (int c = row.getFirstCellNum(); c < row.getLastCellNum() ...