Codeforces 764C Timofey and a tree
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.
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 ≤ n, u ≠ 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.
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.
4
1 2
2 3
3 4
1 2 1 1
YES
2
3
1 2
2 3
1 2 3
YES
2
4
1 2
2 3
3 4
1 2 1 2
NO 题解:
规定一个特殊边,也就是这条边的两个端点的颜色是不一样的,找出所有的特殊边,同时记录相关颜色
出现的次数,如果有一种颜色出现的次数和边的数目是相同的,那么就存在这么一个点。
#include<set>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+;
struct Node
{
int u,v;
};
int n;
int d[maxn];
int col[maxn];
Node e[maxn];
int main()
{
scanf("%d",&n);
for(int i=; i<n; i++)
scanf("%d%d",&e[i].u,&e[i].v);
for(int i=; i<=n; i++)
scanf("%d",&col[i]);
int tot=;
for(int i=; i<n; i++)
{
if(col[e[i].u]!=col[e[i].v])
{
tot++;
d[e[i].u]++,d[e[i].v]++;
}
}
for(int i=;i<=n;i++)
{
if(d[i]==tot)
{
printf("YES\n%d\n",i);
return ;
}
}
printf("NO\n");
return ;
}
Codeforces 764C Timofey and a tree的更多相关文章
- Codeforces 763A. Timofey and a tree
A. Timofey and a tree 题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样.$N,C \le 10^5$ 想法:这个叫换根吧.先求出一个点合法即其儿子的子树 ...
- 【codeforces 764C】Timofey and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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 ...
- 763A - Timofey and a tree
A. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)
codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- codeforces 220 C. Game on Tree
题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #395 C. Timofey and a tree
package codeforces; import java.util.*; public class CodeForces_764C_Timofey_and_a_tree { static fin ...
随机推荐
- 源码浅析:InnoDB聚集索引如何定位到数据的物理位置,并从磁盘读取
索引结构概述: MyISAM索引文件和数据文件是分离的,索引文件仅保存数据记录的地址.这与Oracle的索引结构相似,比较好理解.那么,常用的Innodb聚集索引结构是怎样的呢? InnoDB的数据文 ...
- mysql必知必会--用通配符进行过滤
LIKE 操作符 前面介绍的所有操作符都是针对已知值进行过滤的.不管是匹配一 个还是多个值,测试大于还是小于已知值,或者检查某个范围的值,共 同点是过滤中使用的值都是已知的.但是,这种过滤方法并不是任 ...
- IDEA 修改编码
IDEA: 一. 文件编码修改 IntelliJ IDEA可以在菜单中的File -> Settings -> Editor -> File Encoding下修改项目文件的编码 1 ...
- asp.net abp模块化开发之通用树2:设计思路及源码解析
一.前言 上一篇大概说了下abp通用树形模块如何使用,本篇主要分析下设计思路. 日常开发中会用到很多树状结构的数据,比如:产品的多级分类.省市区县,大多数系统也会用到类似“通用字典/数据字典”的功能, ...
- watch实现监听Vuex状态监听(利用computed)
Vuex 通过 store 选项,提供了一种机制将状态从根组件"注入"到每一个子组件中(需调用 Vue.use(Vuex)):通过在根实例中注册 store 选项,该 store ...
- Java源码系列2——HashMap
HashMap 的源码很多也很复杂,本文只是摘取简单常用的部分代码进行分析.能力有限,欢迎指正. HASH 值的计算 前置知识--位运算 按位异或操作符^:1^1=0, 0^0=0, 1^0=0, 值 ...
- ORACLE10G非归档模式下RMAN异机迁库
环境信息: 源库 目标库 操作系统 WIN7 WIN SVR 2012 R2 IP x.x.x.216 x.x.x.112 数据库版本 10.2.0.4.0 - 64bi 10.2.0.4.0 - 6 ...
- Magento2 观察者模式 之 插件
etc/di.xml 定义如下: <type name="Magento\Quote\Model\Quote\Item\ToOrderItem"> <plugin ...
- Uva1363(余数性质/减少枚举量)
题意: 输入正整数n和k(范围均为1e9),求∑(k mod i),i从1~n 解法: 首先这道题直接暴力亲测会超时. 之后我们写几组数据之后可以发现当k/i的商相同的时候他们的余数成一个等差数列,而 ...
- 多重集组合数 简单dp
#include <cstdio> #include <iostream> using namespace std; +; +; +; ; int n,m,M; int a[m ...