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.
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.
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.
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
2
4 0 0 0 0 1 2 2 3 3 4
0
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.
思路:黑白染色,先DFS把黑白块缩成点,再BFS找树的直径len,有len-1黑白邻接面,len/2即答案。
#include <queue>
#include <functional>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <assert.h>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define mst(a,k) memset(a,k,sizeof(a));
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define mod 1000000007
#define N 200005
#define M 15
using namespace std;
bool color[N]; //点的颜色
struct edge
{
int v,next;
}e[N<<1]; //邻接表建树
int num,len; //num 缩点后块的总数 ,len 直径
int node[N]; //每一块的序号
vector<int>v[N];//每一个块连着其他块
int head[N<<1];
void dfs(int x , int fa) //DFS缩点
{
if(fa == -1) node[x] = num++;
else if(color[x] == color[fa]){
node[x] = node[fa];
}else {
v[node[fa]].push_back(num);
v[num].push_back(node[fa]);
node[x] = num++;
}
for(int i=head[x]; ~i; i=e[i].next){
int v = e[i].v;
if(v == fa) continue;
dfs(v,x);
}
}
bool has[N];
struct step
{
int num;
int stepp;
}now,nex;
int bfs(int x) //两次BFS求直径
{
queue<step>que;
has[x] = 1;
now.num = x;
now.stepp = 1;
que.push(now);
int ans = x;
while(!que.empty()){
now = que.front(); que.pop();
int vv = v[now.num].size();
for(int i =0; i<vv ;i++){
int w = v[now.num][i];
if(has[w]) continue;
has[w] = 1;
nex.num = w;
nex.stepp = now.stepp + 1;
que.push(nex);
}
}
len = now.stepp;
return now.num;
}
main()
{
int n;
scan(n);
REPP(i,1,n) scan(color[i]);
int cnt = 0;
mst(head,-1);
REP(i,1,n){
int a,b;
scann(a,b);
e[cnt].v = a;
e[cnt].next = head[b];
head[b] = cnt++;
e[cnt].v = b;
e[cnt].next = head[a];
head[a] = cnt++;
}
dfs(1,-1);
int s = bfs(0);
mst(has,0);
bfs(s);
printf("%d",len/2);
}