-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion1.cpp
47 lines (44 loc) · 1.17 KB
/
Question1.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
46
47
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to return the adjacency list for each vertex.
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;
}
};
int main(){
Solution test;
vector<pair<int, int>>edges = {
{0, 1},
{0, 3},
{2, 4},
{2, 1},
{3, 2},
{3, 4},
{1, 3}
};
vector<vector<int>> adjList = test.printGraph(5, edges);
//print out the adjacency list
cout << "[";
for (int i = 0; i < adjList.size(); i++) {
cout << "[";
for (int j = 0; j < adjList[i].size(); j++) {
cout << adjList[i][j];
if (j != adjList[i].size() - 1) // No comma after the last element
cout << ",";
}
cout << "]";
if (i != adjList.size() - 1) // No comma after the last list
cout << ", ";
}
cout << "]" << endl;
return 0;
}