Skip to content

Commit 978f2d1

Browse files
authored
Create main.cpp
1 parent 720072f commit 978f2d1

File tree

1 file changed

+39
-0
lines changed
  • 23 - Graph Data Structure Problems/04 - Print Adjacency List

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
template <typename T>
2+
class graph{
3+
public:
4+
vector<vector<int>> adj;
5+
6+
graph(int V){
7+
adj.resize(V);
8+
}
9+
10+
void addEdge(T u, T v, bool direction){
11+
adj[u].push_back(v);
12+
13+
if(direction == 0) adj[v].push_back(u);
14+
}
15+
16+
void printAdj(vector<vector<int>>& ans){
17+
for(int i = 0; i < adj.size(); i++){
18+
for(auto j : adj[i]){
19+
ans[i].push_back(j);
20+
}
21+
}
22+
}
23+
};
24+
25+
class Solution {
26+
public:
27+
vector<vector<int>> printGraph(int V, vector<pair<int, int>>& edges) {
28+
graph<int> g(V);
29+
30+
for(int i = 0; i < edges.size(); i++){
31+
g.addEdge(edges[i].first, edges[i].second, 0);
32+
}
33+
34+
vector<vector<int>> ans(V);
35+
g.printAdj(ans);
36+
37+
return ans;
38+
}
39+
};

0 commit comments

Comments
 (0)