You are given a directed graph with n nodes labeled from 0 to n-1, where each node has exactly one outgoing edge. This creates a functional graph where every node points to exactly one other node (possibly itself).
The graph is represented by a 0-indexed integer array edges of length n, where edges[i] indicates that there is a directed edge from node i to node edges[i].
The edge score of a node i is defined as the sum of the labels of all the nodes that have an edge pointing to i. In other words, it's the sum of all indices j where edges[j] == i.
Goal: Return the node with the highest edge score. If multiple nodes have the same edge score, return the node with the smallest index.
Example: If edges = [1,0,0,0,0,7,7,5], then:
- Node 0 receives edges from nodes 1,2,3,4 → score = 1+2+3+4 = 10
- Node 1 receives edge from node 0 → score = 0
- Node 7 receives edges from nodes 5,6 → score = 5+6 = 11
Node 7 has the highest score (11), so we return 7.
Input & Output
Constraints
- n == edges.length
- 2 ≤ n ≤ 105
- 0 ≤ edges[i] ≤ n - 1
- edges[i] ≠ i
- Edge scores can be very large - use appropriate data types to avoid overflow