Longest Special Path Problem
Description
LeetCode Problem 3425.
You are given an undirected tree rooted at node 0 with n nodes numbered from 0 to n - 1, represented by a 2D array edges of length n - 1, where edges[i] = [u_i, v_i, length_i] indicates an edge between nodes u_i and v_i with length length_i. You are also given an integer array nums, where nums[i] represents the value at node i.
A special path is defined as a downward path from an ancestor node to a descendant node such that all the values of the nodes in that path are unique.
Note that a path may start and end at the same node.
Return an array result of size 2, where result[0] is the length of the longest special path, and result[1] is the minimum number of nodes in all possible longest special paths.
Example 1:
Input: edges = [[0,1,2],[1,2,3],[1,3,5],[1,4,4],[2,5,6]], nums = [2,1,2,1,3,1]
Output: [6,2]
Explanation:
In the image below, nodes are colored by their corresponding values in nums

The longest special paths are 2 -> 5 and 0 -> 1 -> 4, both having a length of 6. The minimum number of nodes across all longest special paths is 2.
Example 2:
Input: edges = [[1,0,8]], nums = [2,2]
Output: [0,1]
Explanation:

The longest special paths are 0 and 1, both having a length of 0. The minimum number of nodes across all longest special paths is 1.
Constraints:
2 <= n <= 5 * 10^4edges.length == n - 1edges[i].length == 30 <= u_i, v_i < n1 <= length_i <= 10^3nums.length == n0 <= nums[i] <= 5 * 10^4- The input is generated such that
edgesrepresents a valid tree.
Solution
Approach: DFS With a Sliding Start on the Root Path
A special path goes from a node up to one of its ancestors, so while walking down the tree from the root we can look at all paths that end at the current node. Keep the current root-to-node path as a stack:
dist[d]is the distance from the root to the node at depthd.last[v]is the depth where valuevlast appeared on the path (or-1).start[d]is the shallowest depth where a special path ending at depthdcan begin. It ismax(start[d - 1], last[nums[node]] + 1): the path must begin below the previous copy of the node's value.
The longest special path ending at this node has length dist[d] - dist[start[d]] and d - start[d] + 1 nodes. Compare it with the best answer (longer wins, fewer nodes break ties). When we leave a node, we restore last[nums[node]] to its earlier value. The DFS is written with an explicit stack because the tree can be a long chain.
Complexity: O(n) time and O(n + V) space, where V is the largest value in nums.
class Solution:
def longestSpecialPath(self, edges: List[List[int]], nums: List[int]) -> List[int]:
n = len(nums)
g = [[] for _ in range(n)]
for u, v, w in edges:
g[u].append((v, w))
g[v].append((u, w))
last = [-1] * (max(nums) + 1) # depth where each value last appeared on the current root path
dist = [0] * n # dist[d]: distance from the root to the node at depth d
start = [0] * n # start[d]: shallowest depth the special path ending at depth d may begin at
saved = [0] * n # value of last[] to restore when leaving depth d
parent = [-1] * n
pos = [0] * n # next neighbour to visit
saved[0] = last[nums[0]]
last[nums[0]] = 0
best, nodes = 0, 1
path = [0] # iterative DFS (the tree can be a 50000-long chain)
while path:
u = path[-1]
d = len(path) - 1
if pos[u] < len(g[u]):
v, w = g[u][pos[u]]
pos[u] += 1
if v == parent[u]:
continue
parent[v] = u
dist[d + 1] = dist[d] + w
saved[d + 1] = last[nums[v]]
start[d + 1] = max(start[d], last[nums[v]] + 1)
last[nums[v]] = d + 1
length = dist[d + 1] - dist[start[d + 1]]
count = d + 1 - start[d + 1] + 1
if length > best or (length == best and count < nodes):
best, nodes = length, count
path.append(v)
else:
last[nums[u]] = saved[d]
path.pop()
return [best, nodes]