Graph Representation Using Adjacency List
Represent an undirected graph using an adjacency list and implement basic traversal setup.
Input format
The first line contains N (vertices) and M (edges). The next M lines contain pairs of vertices (0-indexed, undirected).
Output format
Print N lines; line i contains sorted neighbors of vertex i, or -1 if isolated. Process neighbors in ascending order.
Constraints
- Values fit in a 64-bit signed integer
- Trailing whitespace and a trailing newline are ignored by the judge
Read from stdin, write to stdout. Sample cases below show the exact format.
Sample cases
Example 1
Input
2 0Expected output
-1
-1Example 2
Input
16 6
0 5
4 14
9 15
2 3
2 7
8 14Expected output
5
-1
3 7
2
14
0
-1
2
14
15
-1
-1
-1
-1
4 8
9