codeforces 902b 根树的性质与染色
Posted coolwx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces 902b 根树的性质与染色相关的知识,希望对你有一定的参考价值。
You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1.
Each vertex has a color, let‘s denote the color of vertex v by cv. Initially cv = 0.
You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x.
It is guaranteed that you have to color each vertex in a color different from 0.
You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory).
居然只要比较父子节点的颜色,厉害
思维题,
不要被bfs迷惑了
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int father[10016],color[10016]; 5 int main(void) 6 7 int n; 8 scanf("%d",&n); 9 for(int i=2;i<=n;i++) 10 scanf("%d",&father[i]); 11 for(int i=1;i<=n;i++) 12 scanf("%d",&color[i]); 13 int ans=1; 14 for(int i=2;i<=n;i++) 15 if(color[i]!=color[father[i]]) 16 ans++; 17 printf("%d\n",ans ); 18 return 0; 19
以上是关于codeforces 902b 根树的性质与染色的主要内容,如果未能解决你的问题,请参考以下文章
D. Lost Tree树的二分图性质——Codeforces LATOKEN Round 1 (Div. 1 + Div. 2)
CodeForces 600E. Lomsat gelral树上启发式合并
Codeforces 348E 树的中心点的性质 / 树形DP / 点分治
CodeForces 1141G Privatization of Roads in Treeland (贪心+DFS染色)