Longest Special Path

4

1 votes
Graphs, Medium, Algorithms, Depth First Search
Problem

Given an undirected tree of N nodes in which every node has a value. A path is called a special path if adjacent nodes in the path are of opposite parity (even then odd then even and so on). You have to find the length of the longest special path in the tree.
You can know more about a path in a tree from here.

Input
The first line contains a single integer N denoting the number of nodes in the tree.
The second line contains N space separated integers denoting the value of the ith node.
Next N1 lines contain two space-separated integers U and V denoting there is an edge between node  U and V.

Output
Print a single integer denoting the length of longest special path.

Constraints
1N1051Valueofnodes1051U,VN

Author :Monu Kumar

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

 

The path from node 6 to 4 is special and of length 3.

Editor Image

?