码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 902b 根树的性质与染色

时间:2019-07-03 00:38:54      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:ber   rom   word   possible   mes   fat   https   ini   for   

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 根树的性质与染色

标签:ber   rom   word   possible   mes   fat   https   ini   for   

原文地址:https://www.cnblogs.com/coolwx/p/11123747.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!