-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOTD-Print adjacency list.cpp
45 lines (43 loc) · 1.02 KB
/
POTD-Print adjacency list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
vector<vector<int>> printGraph(int V, vector<pair<int,int>>edges) {
vector<vector<int>> adj(V);
for(const pair<int,int> edge:edges) {
int u=edge.first;
int v=edge.second;
adj[u].push_back(v);
adj[v].push_back(u);
}
return adj;
}
};
//{ Driver Code Starts.
int main() {
int tc;
cin >> tc;
while (tc--) {
int V, E;
cin >> V >> E;
vector<pair<int,int>>edges;
for (int i = 0; i < E; i++) {
int u, v;
cin >> u >> v;
edges.push_back({u,v});
}
Solution obj;
vector<vector<int>> adj = obj.printGraph(V, edges);
for(int i=0;i<V;i++)
{
sort(adj[i].begin(),adj[i].end());
for(auto it:adj[i])
cout<<it<<" ";
cout<<endl;
}
}
return 0;
}
// } Driver Code Ends