Codeforces Round #395 (Div. 2) C. Timofey and a tree
地址:http://codeforces.com/contest/764/problem/C
题目:
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
NO
题意:给你一棵n个节点的树,每个节点都染了色,现在让你找出存不存在一个节点作为根,使不包含根节点的子树的所有节点颜色都一样。
思路:直接枚举出哪些边的两端节点颜色不一样,然后判断这些边是否有相同一个端点,有的话则这个相同点是根,否则不存在。注意点所有边的端点颜色都一样时要特判。
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int n,num,cl[K],u[K],v[K],ex,ey,fx,fy,ans;
PII ed[K];
int main(void)
{
cin>>n;
for(int i=;i<n;i++)
scanf("%d%d",&u[i],&v[i]);
for(int i=;i<=n;i++)
scanf("%d",&cl[i]);
for(int i=;i<n;i++)
if(cl[u[i]]!=cl[v[i]])
ed[++num]=MP(u[i],v[i]);
if(num==)
{
printf("YES\n1\n");
return ;
}
ex=ed[].first,ey=ed[].second;
fx=fy=ans=;
for(int i=;i<=num;i++)
{
if(fx && !(ex==ed[i].first||ex==ed[i].second)) fx=;
if(fy && !(ey==ed[i].first||ey==ed[i].second)) fy=;
if(!(fx||fy))
{
ans=;break;
}
}
if(ans)
{
printf("YES\n");
if(fx)
printf("%d\n",ex);
else
printf("%d\n",ey);
}
else
printf("NO\n");
return ;
}
Codeforces Round #395 (Div. 2) C. Timofey and a tree的更多相关文章
- 【树形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 #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...... ...
 - Codeforces Round #395 (Div. 2)(A.思维,B,水)
		
A. Taymyr is calling you time limit per test:1 second memory limit per test:256 megabytes input:stan ...
 - Codeforces Round #395 (Div. 2) D
		
Description One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On t ...
 - Codeforces Round #395 (Div. 2) B
		
Description Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his ...
 
随机推荐
- 03 Java图形化界面设计——布局管理器之FlowLayout(流式布局)
			
前文讲解了JFrame.JPanel,其中已经涉及到了空布局的使用.Java 虽然可以以像素为单位对组件进行精确的定位,但是其在不同的系统中将会有一定的显示差异,使得显示效果不尽相同,为此java提供 ...
 - Visual Studio 2010自动添加头部注释信息
			
在日常的开发中我们经常需要为我们的类库添加注释和版权等信息,这样我们就需要每次去拷贝粘贴同样的文字,为了减少这种重复性的工作,我们可以把这些信息保存在Visual Studio 2010类库模版文件里 ...
 - jQuery学习笔记1——操作属性
			
一.获得和设置内容 三个简单实用的用于 DOM 操作的 jQuery 方法: text() - 设置或返回所选元素的文本内容, 得到匹配元素集合中每个元素的文本内容结合,包括他们的后代, 即由所有匹配 ...
 - Github基本操作的学习与温习
			
GitHub是最先进的分布式版本控制工具,下面是我学习中总结的操作流程,仅供参考 ----------------------------------------------------------- ...
 - Java序列化(转载)
			
引用自:http://developer.51cto.com/art/201506/479979_all.htm 关于 Java 对象序列化您不知道的 5 件事 数年前,当和一个软件团队一起用 Jav ...
 - 【IOS】 readonly IOS下实战入门
			
当非常多XXX.h 文件属性YY设计中,对外的设计是 YY(readonly) 时.外界便不能改动该属性, 同一时候问题也来了,该XXX类的内部也不能改动改YY属性了.你瞬间凌乱过么. ..Y_Y 然 ...
 - 【VBA】制作散点图及打标签VBA
			
1.散点图的制作必须只选择xy轴对应的数据,不能选中标签. 2.调整xy轴交叉点,改为四个象限. 3.通过菜单移动散点图到新表. 4.运行宏,打上文本标签. VBA如下 Sub AttachLabel ...
 - Access 是/否 字段
			
Microsoft Access 数据库引擎 SQL 数据类型包含由 Microsoft® Jet 数据库引擎定义的 13 种主要数据类型,以及若干可识别为这些数据类型的有效同义词. 其中,在工作中遇 ...
 - 转载:Faster-RCNN详解
			
原文:http://blog.csdn.net/zy1034092330/article/details/62044941 原文大神有很多经典之作,并且讲解得很透彻,建议前往,这里仅当学习使用. Fa ...
 - Maven使用deploy上传jar包到远程库 以Oracle驱动为例
			
一.首先要得到Oracle JDBC Driver 1.通过Oracle官方网站下载相应版本:http://www.oracle.com/technetwork/database/features/j ...