Codeforces 734E. Anton and Tree (联通块 + 树的直径)

Description
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 color i (0 ≤ color i ≤ 1) — colors of the vertices. color i = 0 means that the i-th vertex is initially painted white, while color i = 1 means it’s initially painted black.

Then follow n - 1 line, each of them contains a pair of integers u i and v i (1 ≤ u i, v i ≤ n, u i ≠ v i) — indices of vertices connected by the corresponding edge. It’s guaranteed that all pairs (u i, v i) 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

Solution
因为一次操作会同时操作该点所在颜色的联通块内所有点,所以我们考虑先按照不同颜色的联通块建新树。
不难发现,在新树的基础上,所需最少操作次数为新树的 [直径/2] (向上取整)

Code

#define pb push_back
const int maxn = 3e6 + 7;
vector<int>Edge[maxn];
int c[maxn];
bool vis[maxn];
vector<int>group[maxn];
int cnt;
int belong[maxn];
vector<int>G[maxn];
void build(int st){
	cnt++;
 	group[cnt].pb(st);
	queue<int>q; q.push(st);
	int nowc = c[st];
	vis[st] = true;
	while(!q.empty()){
		int u = q.front();q.pop();
		belong[u] = cnt;
		for(auto v :Edge[u]){
			if(vis[v] || c[v] != nowc) continue;
			vis[v] = true; group[cnt].pb(v);
			q.push(v);
		}
	}
}

int dis[maxn];
int bfs(int st){
	queue<int>q;q.push(st);
	clr(dis,0);
	clr(vis,false);
	dis[st] = 0; vis[st] = true;
	int ed = 0;
	while(!q.empty()){
		int u = q.front();q.pop();
		for(auto v :G[u]){
			if(vis[v]) continue;
			vis[v] = true;
			dis[v] = dis[u] + 1;
			q.push(v);
			ed = v;
		}
	}
	return ed;
}

int main() {
	int n;scanf("%d",&n);
	for(int i = 1;i <= n;++i) scanf("%d",&c[i]);
	for(int i = 1,u,v;i < n;++i){
		scanf("%d%d",&u,&v);
		Edge[u].pb(v), Edge[v].pb(u);
	}
	for(int i = 1;i <= n;++i){
		if(!vis[i]) build(i);
	}
	for(int i = 1;i <= cnt;++i){
		for(auto u : group[i]){
			for(auto v : Edge[u]){
				if(belong[u] != belong[v]){
					G[belong[u]].pb(belong[v]);
					G[belong[v]].pb(belong[u]);
				}
			}
		}
	}
	int x = bfs(1), y = bfs(x);
	printf("%d\n", (dis[y] + 1) / 2);
 	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值