Longest Path

4.2

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

You are given a tree with nodes and edges and an integer . Each node has a label denoted by an integer, . Your task is to find the length of the longest path such that label of all the nodes in the path are divisible by .

Length of a path is the number of edges in that path.

 

Input Format:

First line contains two space separated integers,  and . Next line contains space separated integers, integer denotes the label of node, . Next lines contains two space separated integers each, and denoting that there is an edge between node and node .

 

Output Format:

Print a single integer denoting the length of the longest path such that label of all the nodes in the path are divisible by .

 

Constraints:

Sample Input
6 2
4 2 3 2 3 5
1 2
1 3
2 4
2 5
3 6
Sample Output
2
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Longest path will be 1->2->4.

Editor Image

?