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 ...
随机推荐
- IntelliJ 如何找到项目中 Deprecated 的方法
在一个项目中,如果我们标记了某些元素为 Deprecated 的话,如何让我们能够快速找到? 简单来说,你可以对项目进行 Code Inspection. 选择 Analyze > Inspec ...
- linux中vim使用技巧
一.导入文件内容 :r 解释 导入文件 示例 编辑模式下 # 将/tmp/test.txt内容导入到光标所在的位置的下一行 :r /tmp/test.txt :! 解释 在编辑文件时,执行系统命令 示 ...
- 最短路问题 Dijkstra算法- 路径还原
// 路径还原 // 求最短路,并输出最短路径 // 在单源最短路问题中我们很容易想到,既然有许多条最短路径,那将之都存储下来即可 // 但再想一下,我们是否要把所有的最短路径都求出来呢? // 实际 ...
- 5.Python安装依赖(包)模块方法介绍
1.前提条件 1). 确保已经安装需要的Python版本 2). 确保已经将Python的目录加入到环境变量中 2. Python安装包的几种常用方式 1). pip安装方式(正常在线安装) 2). ...
- P2214 [USACO14MAR]哞哞哞Mooo Moo
链接:Miku ---------------------- 这道题还是个背包 --------------------- 首先看一下声音的组成,对于每一个农场的声音,它是由两部分组成的 :上一个农场 ...
- 企业应用开发的大趋势,65%的应用开发将通过低代码完成 ZT
全球知名的咨询公司Gartner于近日发表了最新版的<低代码开发平台魔力象限>,并在报告中指出,到2024年65%的应用开发工作都将通过低代码的方式完成.Gartner长期关注软件开发领域 ...
- 在阿里云服务器上安装mysql及重置密码
参考链接:https://www.cnblogs.com/ljysy/p/10324854.html 下载与安装MySQL: 这里采用Yum管理好了各种rpm包的依赖,能够从指定的服务器自动下载RPM ...
- 逆战:微信小程序开发(二)
一.常用组件 在上一个章节中讲解了封装请求数据的模块,在此处请求轮播图的数据 1.首页轮播图数据的请求以及渲染 1.1 轮播图数据的请求 pages/home/home.js import { req ...
- java 开发中 dom4j的简单用法
Java中处理XML的方式有很多种,个人任务dom4j还是比较好用的.下面介绍以下简单的使用方法 先把import补充上 import org.dom4j.Document; import org.d ...
- 初始socket编程
服务端语法 import socket # 导入套接字模块# 生成一个socket对象进行网络编程操作server = socket.socket(family=socket.AF_INET, typ ...