Codeforces Round #395 C. Timofey and a tree
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的更多相关文章
- 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 ...
- Codeforces Round #395 (Div. 2)(未完)
2.2.2017 9:35~11:35 A - Taymyr is calling you 直接模拟 #include <iostream> #include <cstdio> ...
- Codeforces Round #395 (Div. 2)
今天自己模拟了一套题,只写出两道来,第三道时间到了过了几分钟才写出来,啊,太菜了. A. Taymyr is calling you 水题,问你在z范围内 两个序列 n,2*n,3*n...... ...
- 【树形DP】Codeforces Round #395 (Div. 2) C. Timofey and a tree
标题写的树形DP是瞎扯的. 先把1看作根. 预处理出f[i]表示以i为根的子树是什么颜色,如果是杂色的话,就是0. 然后从根节点开始转移,转移到某个子节点时,如果其子节点都是纯色,并且它上面的那一坨结 ...
- 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 ...
- 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 ...
- 【分类讨论】Codeforces Round #395 (Div. 2) D. Timofey and rectangles
D题: 题目思路:给你n个不想交的矩形并别边长为奇数(很有用)问你可以可以只用四种颜色给n个矩形染色使得相接触的 矩形的颜色不相同,我们首先考虑可不可能,我们分析下最多有几个矩形互相接触,两个时可以都 ...
- 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 ...
- 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 ...
随机推荐
- 最长回文子串问题 O(n)算法 manacher URAL1297 HDU3068
先来看一道简单的题,ural1297 给定一个1000长度的字符串,求最长回文子串. 看起来很Naive,乱搞一下,O(n^2)都可以解决. 再来看这个题 HDU3068 120个110000长度的字 ...
- mysql数据库中的十进位是什么意思?
一般在用小数的时候才有用,比如类型你设置了double,十进位你设为2,那么你可以放0.22的值,但是放0.222的值它会自动四舍五入为0.22,相当于小数位数吧
- vue父组件访问子组件
1.父组件(父组件访问子组件的方法drop) <!--父组件访问子组件的方法v-ref:shopcart--> <template> <div id="pare ...
- android_app c++框架
找遍了全网,没有一个完整的可用的框架.ndk自带的android_native_app_glue确实不太好用,闭关几天,写出了一个框架.完全的消息队列调用,目前测试的主体框架是没有什么问题了,程序入口 ...
- ADSI和其他内容
ADSI (Active Directory Services Interface)是Microsoft推出的一项技术,它统一了许多底层服务的编程接口,程序员可以使用一致的对象技术来访问这些底层服务. ...
- LN : leetcode 283 Move Zeroes
lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...
- web安全测试--XSS(跨站脚本)与CSRF
XSS攻击原理 反射型 发出请求时,xss代码出现在URL中,作为输入提交到服务器端,服务器端解析后响应,xss代码随响应内容一起传回浏览器,最后浏览器解析执行xss代码.这个过程像一次反射,故叫反射 ...
- JAVA 学习笔记 - 基础语法1
1. 类的定义 public class 与 class的区别 一个java文件只有一个public class, 而且类名必须与文件名一致. 一个java文件可以有多个class定义,javac ...
- StyleAI厚积薄发: Android网络图片数据传输
在StyleAI上厚积了这么长时间,憋了这么久,本来想憋个更大的,不过还是薄发一次的好. 三.直接使用别人的工程 文章:Android学习之客户端上传图片到服务器 下载地址:https://downl ...
- 扩增子分析QIIME2-3数据导出Exporting data
# 激活工作环境 source activate qiime2-2017.8 # 建立工作目录 mkdir -p qiime2-exporting-tutorial cd qiime2-exporti ...