Graph Using Adjacency List
Easyjavapythonccppjavascript
Given the number of vertices V (labelled 0 .. V-1) and a list of undirected edges, build the adjacency list of the graph and print it.
Input Format
The first line contains two integers V and E. Each of the next E lines contains two integers u and v, an undirected edge.
Output Format
Print V lines. Line i is i -> [n1,n2,...] where the neighbours of vertex i are listed in increasing order, comma-separated, inside square brackets (i -> [] if it has none).
Example 1
Input
4 3 0 1 0 2 1 3
Output
0 -> [1,2] 1 -> [0,3] 2 -> [0] 3 -> [1]
Explanation: Each edge connects its two endpoints.
- 1 <= V <= 100000
- 0 <= E <= 200000
- 0 <= u, v < V