Longest Path
Practice
4.2 (10 votes)
Algorithms
Depth first search
Graphs
Medium
Problem
95% Success 7136 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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

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

 

Input Format:

First line contains two space separated integers, \(N\) and \(K\). Next line contains \(N\) space separated integers, \(i^{th}\) integer denotes the label of \(i^{th}\) node, \(A_i\). Next \(N-1\) lines contains two space separated integers each, \(U\) and \(V\) denoting that there is an edge between node \(U\) and node \(V\).

 

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 \(K\).

 

Constraints:

\(1 \le N \le 10^5\)

\(1 \le K \le 10^5\)

\(1 \le A_i \le 10^5\)

\(1 \le U, V \le N\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
10 votes
Tags:
AlgorithmsApprovedDepth First SearchGraphsMediumOpen
Points:30
8 votes
Tags:
GraphsAlgorithmsDepth First SearchC++
Points:30
2 votes
Tags:
AlgorithmsDepth First SearchGraphsMedium