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 [Image] 

and apply operation paint(3) to get the following: [Image] 

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



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

Source: codeforces | Difficulty: HARD
Tags: Tree algorithms

Dynamic programming

Graph traversal


Solutions:
def main():
	n = int(input())
	colors = input()[::2]
	dsu = list(range(n))
	edges = [[] for _ in range(n)]
	for _ in range(n - 1):
		(u, v) = list(map(int, input().split()))
		u -= 1
		v -= 1
		if colors[u] == colors[v]:
			(a, b) = (dsu[u], dsu[v])
			while a != dsu[a]:
				a = dsu[a]
			while b != dsu[b]:
				b = dsu[b]
			if a < b:
				dsu[b] = dsu[v] = a
			else:
				dsu[a] = dsu[u] = b
		else:
			edges[u].append(v)
	for (u, v) in enumerate(dsu):
		dsu[u] = dsu[v]
	d = {u: [] for (u, v) in enumerate(dsu) if u == v}
	for (u, e) in enumerate(edges):
		for v in e:
			d[dsu[u]].append(dsu[v])
			d[dsu[v]].append(dsu[u])

	def bfs(x):
		(nxt, visited, t) = ([x], set(), 0)
		while nxt:
			t += 1
			(cur, nxt) = (nxt, [])
			for x in cur:
				visited.add(x)
				for y in d[x]:
					if y not in visited:
						nxt.append(y)
		return (t, cur[0])
	print(bfs(bfs(0)[1])[0] // 2)

def __starting_point():
	main()
__starting_point()


from collections import defaultdict, deque

class DSU:

	def __init__(self, n):
		self.parents = [i for i in range(n)]
		self.ranks = [0 for i in range(n)]

	def find_parent(self, v):
		if self.parents[v] == v:
			return v
		self.parents[v] = self.find_parent(self.parents[v])
		return self.parents[v]

	def join_sets(self, u, v):
		u = self.find_parent(u)
		v = self.find_parent(v)
		if u != v:
			if self.ranks[u] < self.ranks[v]:
				(u, v) = (v, u)
			self.parents[v] = u
			if self.ranks[v] == self.ranks[u]:
				self.ranks[u] += 1
n = int(input())
dsu = DSU(n)
colors = list(map(int, input().split(' ')))
vertices = []
for i in range(n - 1):
	(u, v) = [int(x) - 1 for x in input().split(' ')]
	if colors[u] == colors[v]:
		dsu.join_sets(u, v)
	vertices.append((u, v))
graph = defaultdict(list)
for (u, v) in vertices:
	if colors[u] != colors[v]:
		u = dsu.find_parent(u)
		v = dsu.find_parent(v)
		graph[u].append(v)
		graph[v].append(u)

def bfs(u):
	d = dict()
	d[u] = 0
	q = deque()
	q.append(u)
	while q:
		u = q.pop()
		for v in graph[u]:
			if v not in d:
				d[v] = d[u] + 1
				q.append(v)
	return d
if graph:
	v = list(graph.keys())[0]
	d = bfs(v)
	u = v
	for i in d:
		if d[i] > d[u]:
			u = i
	d = bfs(u)
	w = u
	for i in d:
		if d[i] > d[w]:
			w = i
	print((d[w] + 1) // 2)
else:
	print(0)


f = lambda : map(int, input().split())
(s, n) = (0, int(input()))
g = [set() for i in range(n + 1)]
c = [0] + list(f())
d = [0] * (n + 1)
for j in range(n - 1):
	(a, b) = f()
	g[a].add(b)
	g[b].add(a)
p = [q for (q, t) in enumerate(g) if len(t) == 1]
while p:
	a = p.pop()
	if not g[a]:
		break
	b = g[a].pop()
	g[b].remove(a)
	if c[a] - c[b]:
		d[a] += 1
	s = max(s, d[b] + d[a])
	d[b] = max(d[b], d[a])
	if len(g[b]) == 1:
		p.append(b)
print(s + 1 >> 1)


def main():
	from collections import defaultdict
	(n, colors) = (int(input()), input()[::2])
	(dsu, edges, d) = (list(range(n)), [], defaultdict(list))
	for _ in range(n - 1):
		(u, v) = list(map(int, input().split()))
		u -= 1
		v -= 1
		if colors[u] == colors[v]:
			(a, b) = (dsu[u], dsu[v])
			while a != dsu[a]:
				a = dsu[a]
			while b != dsu[b]:
				b = dsu[b]
			if a < b:
				dsu[b] = dsu[v] = a
			else:
				dsu[a] = dsu[u] = b
		else:
			edges.append(u)
			edges.append(v)
	for (u, v) in enumerate(dsu):
		dsu[u] = dsu[v]
	while edges:
		(u, v) = (dsu[edges.pop()], dsu[edges.pop()])
		d[u].append(v)
		d[v].append(u)

	def bfs(x):
		(nxt, avail, t) = ([x], [True] * n, 0)
		while nxt:
			t += 1
			(cur, nxt) = (nxt, [])
			for y in cur:
				avail[y] = False
				for y in d[y]:
					if avail[y]:
						nxt.append(y)
		return t if x else cur[0]
	print(bfs(bfs(0)) // 2)

def __starting_point():
	main()
__starting_point()


import sys
from collections import defaultdict, deque

def buildTree(root):
	q = deque()
	q.append((root, root))
	vis = [0 for _ in range(n + 1)]
	vis[root] = 1
	while q:
		(v, fa) = q.pop()
		for x in preedge[v]:
			if vis[x] != 1:
				vis[x] = 1
				if col[x] == col[fa]:
					q.append((x, fa))
				else:
					edge[fa].append(x)
					edge[x].append(fa)
					q.append((x, x))

def getDeep(root):
	vis = [0 for _ in range(n + 1)]
	d = {}
	q = deque()
	q.append(root)
	vis[root] = 1
	d[root] = 0
	while q:
		u = q.pop()
		for x in edge[u]:
			if vis[x] == 0:
				vis[x] = 1
				d[x] = d[u] + 1
				q.append(x)
	return d
n = int(input())
precol = [int(_) for _ in input().split()]
preedge = [[] for _ in range(n + 1)]
for _ in range(n - 1):
	(x, y) = [int(_) for _ in input().split()]
	preedge[x].append(y)
	preedge[y].append(x)
root = (n + 1) // 2
edge = [[] for _ in range(n + 1)]
col = [0 for _ in range(n + 1)]
for x in range(1, n + 1):
	col[x] = precol[x - 1]
buildTree(root)
d = getDeep(root)
u = max(d.keys(), key=lambda k: d[k])
print((max(getDeep(u).values()) + 1) // 2)