Codeforces 734E Anton and Tree【并查集缩点+树的直径】好题~

224 篇文章 2 订阅
87 篇文章 0 订阅

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 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.

Examples
Input
11
0 0 0 1 1 0 1 0 0 1 1
1 2
1 3
2 4
2 5
5 6
5 7
3 8
3 9
3 10
9 11
Output
2
Input
4
0 0 0 0
1 2
2 3
3 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个点的一棵树,每个点都有一个颜色(0/1),每一次操作可以使得一种颜色的某一个连通块反色(0->1/1->0);

问最少操作多少次使得整颗树变成一种颜色。


思路:


1、首先我们用并查集将所有联通块进行缩点。


2、那么接下来我们将缩点之后再建图,很显然它还是一棵树。现在这棵树是一颗黑白相间的树,那么我们每选择一个点之后,其改变的只有其周围几个点的颜色。

那么我们最少需要进行操作的步数,就要在树的最长链上操作,对于最长链上的所有点都统一了颜色,那么边岔路也一定统一了颜色。

所以我们求出此时这棵树的直径长度Len,一共这条链子上有Len+1个点,那么可知结果就是(Len+1)/2.


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<map>
using namespace std;
vector<int >mp[205000];
int f[300050];
int p[300050][2];
int color[300050];
int root,maxn;
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
void Dfs(int u,int from,int depth)
{
    if(depth>maxn)
    {
        maxn=depth;
        root=u;
    }
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from)continue;
        else
        {
            Dfs(v,u,depth+1);
        }
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)scanf("%d",&color[i]),f[i]=i,mp[i].clear();
        for(int i=0;i<n-1;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            p[i][0]=x;
            p[i][1]=y;
            if(color[x]==color[y])
            {
                merge(x,y);
            }
        }
        map<int ,int >s;
        int cnt=0;
        maxn=0;
        for(int i=0;i<n-1;i++)
        {
            if(find(p[i][0])!=find(p[i][1]))
            {
                p[i][0]=find(p[i][0]);
                p[i][1]=find(p[i][1]);
                if(s[p[i][0]]==0)s[p[i][0]]=++cnt;
                if(s[p[i][1]]==0)s[p[i][1]]=++cnt;
                int u=s[p[i][0]];
                int v=s[p[i][1]];
                root=u;
                mp[u].push_back(v);
                mp[v].push_back(u);
            }
        }
        Dfs(root,-1,0);
        maxn=0;
        Dfs(root,-1,0);
        printf("%d\n",(maxn+1)/2);
    }
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值