P16222 [GeeksforGeeks] Print adjacency list 1587115620
普及-
题目描述

Given the adjacency list representation of an undirected graph
your task is to complete the function printGraph() that takes the number of vertices (V) and the adjacency list as input parameters.
The function should return a new adjacency list, where each adjacency list contains the vertex itself at first position then all its adjacent nodes, in the same order of the given adjacency list of that vertex.
Example 1:
Input:
Output:
0-> 1-> 4
1-> 0-> 2-> 3-> 4
2-> 1-> 3
3-> 1-> 2-> 4
4-> 0-> 1-> 3
Explanation:
As 0,1 and 3 is connected to 4 so 4th row
of the list containing 4 and its connected
nodes 0,1 and 3 and we have to add those in
sorted order and same for every row.
Example 2:
Input:
Output:
0-> 2-> 3
1-> 2
2-> 0-> 1
3-> 0
Explanation:
As 2 and 3 is connected to 0 so 0th row
of the list containing 0 and its connected
nodes 2 and 3 and we have to add those in
sorted order and same for every row.
Your task:
You dont need to read input or print anything. Your task is to complete the function printGraph() which takes the integer V denoting the number of vertices and adjacency list as input parameters and returns the list of list contains the node itself at first position with its connected nodes(as same as it is given in input adjacency list).
Expected Time Complexity: O(V + E)
Expected Auxiliary Space: O(V + E)
Constraints:
1 ≤ V, E ≤ 10^{4}

输入格式

见题面

输出格式

见题面

样例输出
0-> 1-> 4 1-> 0-> 2-> 3-> 4 2-> 1-> 3 3-> 1-> 2-> 4 4-> 0-> 1-> 3