#K33347. Amusement Park Adventure
Amusement Park Adventure
Amusement Park Adventure
You are given an undirected graph representing an amusement park. The park consists of n rooms (numbered from 1 to n) and m bidirectional passages connecting them. One of these rooms is designated as the central room, labeled with index c. Your task is to determine:
- The total number of rooms accessible from the central room.
- The minimum number of passages needed to reach each room from the central room. For rooms that are not accessible, output
-1
.
You may use the Breadth-First Search (BFS) algorithm. In mathematical terms, if the distance from room c to some room i is d(i), then for every passage \( (u,v) \) used in the shortest path we have \( d(v) = d(u) + 1 \). Print the results as described below.
inputFormat
The input is given via standard input (stdin) in the following format:
n m c u1 v1 u2 v2 ... um vm
Here, n
is the number of rooms, m
is the number of passages, and c
is the index of the central room. Each of the next m
lines contains two integers u
and v
indicating that there is a passage between room u
and room v
.
outputFormat
The output should be printed to standard output (stdout) in two lines:
- The first line contains a single integer: the number of rooms accessible from the central room.
- The second line contains
n
space-separated integers, where the i-th integer represents the minimum number of passages needed to reach room i from the central room. If a room is not accessible, output-1
for that room.
5 4 1
1 2
2 3
3 4
4 5
5
0 1 2 3 4
</p>