Codeforces Round #379 (Div. 2)E. Anton and Tree(dfs缩点,想法题)

35 篇文章 1 订阅
8 篇文章 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.


题意

给你一棵树,每个树上的节点要么是1,要么是0

每次操作你可以指定任何一个节点,然后使得这个节点周围的同色连通块变色(这个节点也会变色)

问你最少花费多少次,使得整个树都是一个颜色。

题解:

首先可以将同色连通块进行缩点,对于缩点后的树,假设直径为d,那么答案就是(d+1)/2;

证明:

将这棵树变为同色至少需要(d+1)/2,假设是一条链,每次最多减少2个节点;

当不是链时,找到树直径的中点,然后死抓着这一点一直染这一点,画一画图就行了,经过(d+1)/2次就成为全同色了。



说明:缩点可以dfs过程直接缩点了,我开始想着用并查集,后来看了qsc的博客,学会了一个姿势更好的方法。详见代码。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
const int MAXN=2E5+100;
struct node
{
	int to,next;
}e[2*MAXN];
int tol=0;
int head[MAXN];
void add(int u,int v)
{
	e[++tol].to=v,e[tol].next=head[u],head[u]=tol;
	e[++tol].to=u,e[tol].next=head[v],head[v]=tol;
}
int c[MAXN];
PII dfs(int u,int fa,int d)
{
	PII tmp=make_pair(d,u),t;
	for(int i=head[u];i;i=e[i].next)
	{
		int v=e[i].to;
		if(v==fa) continue;
		if(c[u]==c[v]) t=dfs(v,u,d);
		else t=dfs(v,u,d+1);
		tmp=max(tmp,t);
	}
	return tmp;
}
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		tol=0;
		rep(i,1,n+1) scanf("%d",&c[i]);
		rep(i,1,n) 
		{
			int u,v;
			scanf("%d%d",&u,&v);
			add(u,v);
		}
		PII t=dfs(1,-1,0);
		PII tt=dfs(t.second,-1,0);
		printf("%d\n",(tt.first+1)/2);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值