CodeForces

来源:互联网 发布:淘宝导航条hot图标 编辑:程序博客网 时间:2024/06/16 03:38

点击打开链接

Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph.

There are n vertices in the tree, each of them is painted black or white. Anton doesn't like multicolored trees, so he wants to change the tree such that all vertices have the same color (black or white).

To change the colors Anton can use only operations of one type. We denote it as paint(v), where v is some vertex of the tree. This operation changes the color of all vertices u such that all vertices on the shortest path from v to u have the same color (including v and u). For example, consider the tree

and apply operation paint(3) to get the following:

Anton is interested in the minimum number of operation he needs to perform in order to make the colors of all vertices equal.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree.

The second line contains n integers colori (0 ≤ colori ≤ 1) — colors of the vertices. colori = 0 means that the i-th vertex is initially painted white, while colori = 1 means it's initially painted black.

Then follow n - 1 line, each of them contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — indices of vertices connected by the corresponding edge. It's guaranteed that all pairs (ui, vi) are distinct, i.e. there are no multiple edges.

Output

Print one integer — the minimum number of operations Anton has to apply in order to make all vertices of the tree black or all vertices of the tree white.

Example
Input
110 0 0 1 1 0 1 0 0 1 11 21 32 42 55 65 73 83 93 109 11
Output
2
Input
40 0 0 01 22 33 4
Output
0
Note

In the first sample, the tree is the same as on the picture. If we first apply operation paint(3) and then apply paint(6), the tree will become completely black, so the answer is 2.

In the second sample, the tree is already white, so there is no need to apply any operations and the answer is 0.

题意:一次操作能把树上联通的相同的颜色的节点变成另一个颜色,给定一颗n个节点的树,问至少多少次操作能将树变成一个颜色。

题解:典型的联通缩点,感觉这种类似的问题都是联通缩点,然后求出树的直径,最后的结果就是直径的一半。

具体为啥呢?借用大佬学弟的解释,就是每次把中间的点染色,然后缩点,然后再染色中间的点,最后就是直径的一半。。。感觉应该是这样理解的,事实证明这样写也是对的

求树的直径的方法:先从任意一个点开始dfs,找到离它最远的点,然后从那个点开始dfs,最后的结果即是树的直径。


#include<stdio.h>#include<iostream>#include<set>#include<queue>#include<algorithm>#include<math.h>#include<string.h>#include<string>#include<vector>using namespace std;const int inf = 0x3f3f3f3f ;const int mx = 2e5 + 5 ;int col[mx]  , col1[mx] , col2[mx];vector<int >G[mx] , edge[mx];bool vis[mx] ;void dfs1(int x , int cur) {//缩点    if(vis[x])        return ;    vis[x] = true ;    if(col[x] != col[cur]) {        edge[x].push_back(cur) ;        edge[cur].push_back(x) ;        cur = x;    }    for(int i = 0 ; i < G[x].size() ; i ++) {        dfs1(G[x][i] , cur) ;    }}int maxlen , s ;void dfs(int x , int dis) {//dfs求树的直径    if(vis[x])        return ;    vis[x] = true ;    if(dis > maxlen) {        maxlen = dis;        s = x ;    }    for(int i = 0 ; i < edge[x].size() ; i ++) {        dfs(edge[x][i] , dis + 1) ;    }}int main(){    //freopen ("in.txt", "r", stdin);    int n ;    while(~scanf("%d" , &n)) {        for(int i = 0 ; i <= n ; i ++) {            G[i].clear() ;            edge[i].clear() ;        }        memset(vis , false , sizeof(vis)) ;        maxlen = 0 ;        for(int i = 1 ; i <= n ; i ++)            scanf("%d" , &col[i]) ;        for(int i = 1 ; i < n ; i ++) {                int u , v ;            scanf("%d%d" , &u , &v) ;            G[u].push_back(v) ;            G[v].push_back(u) ;        }        dfs1(1 , 1) ;        memset(vis , false , sizeof(vis));        dfs(1 , maxlen) ;        memset(vis , false , sizeof(vis));        dfs(s , 0) ;        //cout<<maxlen<<endl;        cout<<(maxlen + 1 )/2<<endl;    }    return 0;}





原创粉丝点击