C. Timofey and a tree codeforces 395 div2 C
来源:互联网 发布:遗传算法的过程 编辑:程序博客网 时间:2024/05/21 17:58
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.
Input
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.
Output
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.
Examples
input
4
1 2
2 3
3 4
1 2 1 1
output
YES
2
input
3
1 2
2 3
1 2 3
output
YES
2
input
4
1 2
2 3
3 4
1 2 1 2
output
NO
n个点,每个点有各自的颜色(可能相同可能不同),问一个棵树遮住一个点,令它的子树都只有一种颜色。
傻瓜题,找出不同颜色的两个点,判断就行,因为如果有两个颜色不一样的点,那么要遮住的点肯定就在这两个里面,还要特判一下,全部颜色相同的情况。贴代码。
#include <bits/stdc++.h>using namespace std;long long a[200100];vector<int> v[200100];int flag;int vis[200100];void dfs(int x,int f){ if(flag==1) return ; if(a[x]!=f) { flag=1; return ; } for(int i=0;i<v[x].size();i++) { if(vis[v[x][i]]) continue; vis[v[x][i]]=1; dfs(v[x][i],f); vis[v[x][i]]=0; if(flag==1) break; } return ;}pair<int,int> p[201000];int re[4];int main(){ int n; while(cin>>n){ memset(vis,0,sizeof(vis)); memset(v,0,sizeof(v)); for(int i=0;i<n-1;i++) { int x1,x2; cin>>x1>>x2; v[x1].push_back(x2); v[x2].push_back(x1); p[i]=make_pair(x1,x2); } for(int i=1;i<=n;i++) { cin>>a[i]; } int f=0; for(int i=0;i<n-1;i++) { int x1=p[i].first; int x2=p[i].second; if(a[x1]!=a[x2]) { re[0]=x1; re[1]=x2; f=1; // printf("%d %d\n",x1,x2 ); } } if(!f) { printf("YES\n"); printf("1\n" ); continue; } int g=0; for(int i=0;i<=1;i++) { flag=0; int h=re[i]; vis[h]=1; for(int j=0;j<v[h].size();j++) { dfs(v[h][j],a[v[h][j]]); } vis[h]=0; if(flag==0) { printf("YES\n"); printf("%d\n",h ); g=1; break; } } if(!g) printf("NO\n"); }}
- C. Timofey and a tree codeforces 395 div2 C
- 【codeforces 764C】Timofey and a tree
- CodeForces 764C Timofey and a tree
- Codeforces Round #395 (Div. 2) C. Timofey and a tree
- Codeforces Round #395 (Div. 2) C. Timofey and a tree
- Codeforces Round 395 C Timofey and a tree 树
- C. Timofey and a tree
- Codeforces 764C - Timofey and a tree(dfs)
- Codeforces 764C Timofey and a tree 树+思维
- 【codeforces 764C】Timofey and a tree 题解
- Codeforces 764C Timofey and a tree(思路)
- codeforces 764C Timofey and a tree(树+思维)
- codeforces 764C Timofey and a tree (思维题)
- 【Codeforces Round #395 (Div. 2)】Codeforces 764C Timofey and a tree
- Codeforces Round #395 (Div. 2) -- C. Timofey and a tree(并查集+缩点)
- Codeforces Round #395(Div. 2)C. Timofey and a tree【思维+并查集缩点+判定】
- Codeforces Round #395 (Div.2) C-Timofey and a tree 树型dp
- Codeforces Round #395 (Div. 2) C. Timofey and a tree (树的基础应用)
- sql单独获取时间的年月日部分
- 各版本Linux安装软件
- java:深入理解volatile关键字
- [Python标准库]decimal——定点数和浮点数的数学运算
- Java的类与对象
- C. Timofey and a tree codeforces 395 div2 C
- iOS10新特性
- 有趣的婚姻,要互相调戏
- Git的详细教程(附总结的常用代码)
- 大前端时代到来,你可以改变世界--系列公开课
- android中Resources的资源缓存机制
- wikioi1230 元素查找
- 关于使用ajaxSubmit来上传图片
- linux下调用openssl实现获得文件MD5值