Dsa
Trees
Medium
The Alternating Neon Path
You are given a tree representing a network of neon-lit tunnels. Each edge (tunnel) has a color. A 'vibrant path' is a simple path in the tree such that no two adjacent edges on the path have the same color. Your task is to find the length (number of edges) of the longest vibrant path in the tree.
Input Format:
- Line 1: An integer N, the number of nodes (1-indexed).
- Next N-1 lines: Three integers u, v, c representing an edge between u and v with color c.
Output Format:
- A single integer representing the length of the longest vibrant path.
Example:
Input:
5
1 2 1
2 3 2
3 4 2
3 5 1
Output:
3
(The path 1-2-3-5 has colors 1, 2, 1 which are alternating. Length 3.)
Key concepts
tree dpdepth first search
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now