Skip to main content

Practice · 2 of 5

Shortest Steps

CHALLENGE
Difficulty: intermediate+25 XP

**Description:** Given an undirected unweighted graph and a start vertex s, print the shortest distance (edge count) from s to every vertex, -1 if unreachable. **Input:** Line 1: n m s (1 <= n <= 100000, 0 <= m <= 200000, 1 <= s <= n). Next m lines: u v. **Output:** One line: n integers — dist(s, 1) ... dist(s, n), space-separated. **Example:** 4 3 1 / 1 2 / 2 3 / 1 4 -> 0 1 2 1.

Back to lesson: Practice: Graph Theory Problem Set