You are given an undirected graph with n nodes numbered 1..n and m edges. Starting a breadth-first search from node 1, every node reachable from node 1 sits at some distance d (the minimum number of edges on a path from node 1), where node 1 itself is at distance 0.
Group the reachable nodes by their distance and report, for each distance d = 0, 1, 2, ... up to the largest reachable distance, how many nodes lie at that distance. Nodes that are not reachable from node 1 are ignored.
The graph may contain repeated edges and self-loops; treat them as an ordinary undirected graph (they never change shortest distances).
Line 1: two integers n and m separated by a space.
Next m lines: two integers u v (1-indexed) describing an undirected edge between u and v.
A single line of space-separated counts. The value at position d (0-indexed) is the number of nodes whose shortest distance from node 1 equals d. The line always begins with 1 (node 1 itself at distance 0) and stops at the deepest reachable layer.
Example 1
Input
6 6 1 2 1 3 2 4 3 4 4 5 5 6
Expected
1 2 1 1 1
Explanation
Distances from node 1: node 1 at 0; nodes 2,3 at 1; node 4 at 2; node 5 at 3; node 6 at 4. Layer sizes are 1 2 1 1 1.
Example 2
Input
1 0
Expected
1
Explanation
There is only node 1 and no edges, so the single layer (distance 0) contains just node 1, giving 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →