1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路

Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路

时间:2021-02-11 06:43:45

相关推荐

Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路

题目链接:/contest/734/problem/E

E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output

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

There arenvertices 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 aspaint(v), wherevis some vertex of the tree. This operation changes the color of all verticesusuch that all vertices on the shortest path fromvtouhave the same color (includingvandu). For example, consider the tree

and apply operationpaint(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 integern(1 ≤ n ≤ 200 000)— the number of vertices in the tree.

The second line containsnintegerscolori(0 ≤ colori ≤ 1)— colors of the vertices.colori = 0means that thei-th vertex is initially painted white, whilecolori = 1means it's initially painted black.

Then follown - 1line, each of them contains a pair of integersuiandvi(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.

Examples 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 operationpaint(3)and then applypaint(6), the tree will become completely black, so the answer is2.

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

题解:

1.缩点:将颜色相同的连通块缩成一个点。当然并不是实际将其缩成一点,而是当前点与上一个点的颜色相同,则“不作为”, 即无视这个点。经过缩点之后,这棵树的结点颜色为黑白交替,如下图。

2.无环无向图的最长路:首先取随便一个点作为起点,然后dfs跑一遍。取得路径最长的那条路的终点,并以此作为起点,再跑一遍dfs,取最长的路径len,最终答案即为len/2,向下取整。

代码如下:

1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const double eps = 1e-6; 5 const int INF = 2e9; 6 const LL LNF = 9e18; 7 const int mod = 1e9+7; 8 const int maxn = 2e5+10; 9 10 int n, c[maxn];11 vector<int>son[maxn];12 13 pair<int,int> dfs(int u, int fa, int cnt)14 {15pair<int,int> tmp = make_pair(cnt, u);16for(int i = 0; i<son[u].size(); i++)17{18 int v = son[u][i];19 if(v==fa) continue; //由于无环,所以只要避免其走“回头路”即可,无需vis数组20 tmp = max( tmp, dfs(v,u,cnt+(c[u]!=c[v])) ); //如果颜色相同,则cnt+0,就跳过了当前点21}22return tmp;23 }24 25 int main()26 {27scanf("%d",&n);28for(int i = 1; i<=n; i++)29 scanf("%d",&c[i]);30 31for(int i = 1; i<n; i++)32{33 int u, v;34 scanf("%d%d",&u,&v);35 son[u].push_back(v);36 son[v].push_back(u);37}38 39pair<int,int> tmp;40tmp = dfs(1,-1,1);41tmp = dfs(tmp.second, -1, 1);42cout<< tmp.first/2 <<endl;43 }

View Code

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。