763A - Timofey and a tree
2 seconds
256 megabytes
standard input
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.
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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define pi acos(-1.0)
#include<vector>
typedef long long ll;
const int N=1e5+;
using namespace std;
int c[N],f[N];
struct Node {
int v1, v2;
Node(int x = -, int y = -){//这是一个函数,v1,v2初始化为x,y
v1=x;
v2=y;
}
};
vector<Node> e;
int main()
{
int n;
int s,t,cnt=;
scanf("%d",&n);
memset(f,,sizeof(f));
for(int i=;i<n-;i++){
scanf("%d%d",&s,&t);
e.push_back(Node(s-,t-));//存入边
}
for(int i=;i<n;i++){
scanf("%d",&c[i]);//i个点的颜色
}
for(int i=;i<n-;i++){
Node& u=e[i];
if(c[u.v1]!=c[u.v2]){
cnt++;
f[u.v1]++;
f[u.v2]++;
}
}
for(int i=;i<n;i++){
if(f[i]==cnt){
printf("YES\n");
printf("%d\n",i+);
return ;
}
}
printf("NO\n");
return ;
}
763A - Timofey and a tree的更多相关文章
- Codeforces 763A. Timofey and a tree
A. Timofey and a tree 题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样.$N,C \le 10^5$ 想法:这个叫换根吧.先求出一个点合法即其儿子的子树 ...
- cf 763A. Timofey and a tree
呵呵呵,直接判断是不是一个点连起来所有的特殊边(连接2不同颜色的点的边) (一开始还想各种各样奇怪的dfs...垃圾) #include<bits/stdc++.h> #define LL ...
- 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 764C】Timofey and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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 th ...
- 【树形DP】Codeforces Round #395 (Div. 2) C. Timofey and a tree
标题写的树形DP是瞎扯的. 先把1看作根. 预处理出f[i]表示以i为根的子树是什么颜色,如果是杂色的话,就是0. 然后从根节点开始转移,转移到某个子节点时,如果其子节点都是纯色,并且它上面的那一坨结 ...
- C. Timofey and a tree 观察题 + dfs模拟
http://codeforces.com/contest/764/problem/C 题意:在n个顶点中随便删除一个,然后分成若干个连通子图,要求这若干个连通子图的颜色都只有一种. 记得边是双向的, ...
- Codeforces Round #395 C. Timofey and a tree
package codeforces; import java.util.*; public class CodeForces_764C_Timofey_and_a_tree { static fin ...
- codeforces 764 C. Timofey and a tree(dfs+思维)
题目链接:http://codeforces.com/contest/764/problem/C 题意:给出一个树,然后各个节点有对应的颜色,问是否存在以一个点为根节点子树的颜色都一样. 这里的子树颜 ...
随机推荐
- C# DateTime变量不能赋null值
使用定时器的时候: DateTime aimtime=DateTime.Now.AddSeconds(1); CheckTimer() { if(DateTime.Now.CompareTo(aimt ...
- Linux安装php的Redis扩展
1.安装redis 下载:https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz 上传phpredis-2.2.4.tar.gz到/usr ...
- (简单) POJ 3076 Sudoku , DLX+精确覆盖。
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- Android Material各种颜色设置
Blogpost about support appcompat v21 from Chris Banes
- CSS中link与import的区别
一.import的用法 1,在html文件中 <style type="text/css"> @import url(http://www.dreamdu.com/st ...
- JAVA判断32位还是64位,调用不同的DLL(转)
源:JAVA判断32位还是64位,调用不同的DLL 通过获取sun.arch.data.model可判断是32还是64的JAVA 将32或者64位的DLL放不同的目录,实现自适应调用DLL Prope ...
- Delphi 内存与指针
源:Delphi 内存与指针 Delphi 的内存操作函数(1): 给字符指针分配内存 Delphi 的内存操作函数(2): 给数组指针分配内存 Delphi 的内存操作函数(3): 给结构体指针分配 ...
- OI队内测试二【数论概率期望】
版权声明:未经本人允许,擅自转载,一旦发现将严肃处理,情节严重者,将追究法律责任! 序:代码部分待更[因为在家写博客,代码保存在机房] T1: 题解:插头dp应该很好想吧,我们考虑当出现转折时我们对下 ...
- p1349星屑幻想
这道题的原题目我也不知道是什么. 大致题意是有一个图,有些点的权值已确定,要求你确定其他点的权值使所有边两个点的权值的xor和最小,输出所有点的最终权值,输出有spj: 解法是最小割,由于题目要求的使 ...
- java 服务
2014年2月27日 10:30:55 学习 Java Service Wrapper 2014年2月27日 14:14:14太复杂了,以后研究吧. ###### 2014年2月27日 14:14:2 ...