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 题意:给出一个树,然后各个节点有对应的颜色,问是否存在以一个点为根节点子树的颜色都一样. 这里的子树颜 ...
随机推荐
- 负载均衡 LVS+Keepalived
系统 CentOS 5.6 X64 物理机 IP 10.10.10.104 Xen : 三台 CentOS 5.8 ip为: 10.10.10.106 10.10.10.107 ...
- 从url下载图片--java与python实现方式比较
从url下载图片--java与python实现方式比较 博客分类: 技术笔记小点滴 javapython图片下载 一.java的实现方式 首先读取图片 //方式一:直接根据url读取图片 priva ...
- 一道关于 precision、recall 和 threshold关系的机器学习题
Suppose you have trained a logistic regression classifier which is outputing hθ(x). Currently, you p ...
- php中使用mysql_fetch_object向页面输出结果,总结!
public function selectResultByThird() { if ((!empty ($_REQUEST["bigname"])) && (!e ...
- Mac搭建Hadoop源码阅读环境
1.本次Hadoop源码阅读环境使用的阅读工具是idea,Hadoop版本是2.7.3.需要安装的工具包括idea.jdk.maven.protobuf等 2.jdk,使用的版本是1.8版,在jdk官 ...
- Git for Windows 工具下载及配置
前言,关于git工具的帖子:http://cn.v2ex.com/t/225027 最终选择了git for windows这个工具,路径为:https://git-for-windows.githu ...
- linear-gradient线性渐变
作者:zccst CSS3 Gradient 分为 linear-gradient(线性渐变)和 radial-gradient(径向渐变). 1,在mozila background: -moz-l ...
- Android相关修改教程
ICS: 自己动手实现接通震动功能 原生来电归属地修改教程
- db2 odbc连接设置
参考:http://www.withdata.com/blog/tips/how-to-install-and-configure-db2-odbc-driver.html 驱动下载地址:https: ...
- Python3基础 用list()查看filter()返回的对象
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...