You are given an unweighted graph representing a network of wires.
-
The graph has
N nodes
labeled
0..N-1
and
M edges
(each edge is a bidirectional wire).
-
A subset of nodes are
torches
.
-
Each torch provides power level
16 at its own node
.
-
Power propagates through wires and
decays by 1 per wire traversed
(i.e., by graph distance in number of edges).
-
If multiple torches can reach a node, the node receives the
maximum
power among them.
-
Power cannot be negative.
For every node v, define:
-
dist(v) =
the length (in edges) of the shortest path from
v
to any torch (∞ if unreachable)
-
power(v) = max(0, 16 - dist(v))
(and
power(v)=0
if unreachable)
Task
Given N, the edge list, and the list of torch nodes, compute power(v) for all nodes.
Input (one possible format)
-
N
(number of nodes)
-
edges
: list of pairs
(u, v)
-
torches
: list of torch node indices
Output
Notes / constraints
-
Assume
N
can be large (e.g., up to 10^5), so the solution should be near-linear in
N + M
.
-
Graph may be disconnected.