Shortest Path in an Unweighted Graph

EASY · C · 2000ms

Rakib also needs the shortest number of hops from the main server to every other machine in the network. Given an undirected unweighted graph with N nodes and M edges, and a source node S, print the shortest distance (number of edges) from S to every node 1..N. Print -1 for unreachable nodes.

Input

Line1: N M S. Next M lines: u v.

Output

N integers: distances from S to each node 1..N.

Constraints

1 ≤ N ≤ 10^5, 0 ≤ M ≤ 2×10^5

Sample Input

4 3 1
1 2
2 3
3 4

Sample Output

0 1 2 3
main.c
Loading editor…

Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.

Shortest Path in an Unweighted Graph · DIU ContestHub