File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
23 - Graph Data Structure Problems/04 - Print Adjacency List Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments