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 }

 

posted @ 2019-07-02 23:15  coolwx  阅读(209)  评论(0编辑  收藏  举报