Struktur data kelas grpah


#include <bits/stdc++.h>
using namespace std;

class Graph {
	int V;
	list<int> *l;

public:
	Graph(int v){
		V = v;
		l = new list<int>[V];
	}

	void addEdge(int u, int v, bool bidir = true){
		l[u].push_back(v);
		if(bidir){
			l[v].push_back(u);
		}
	}

	void printEdge(){
		for(int i=1;i<V;i++){
			cout<<i<<"-->";
			for(auto node: l[i]){
				cout<<node<<" ";
			}
			cout<<endl;
		}
	}

};


int main() {

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	
	Graph g(5);
	g.addEdge(1, 2);
	g.addEdge(2, 3);
	g.addEdge(3, 4);
	g.addEdge(1, 3);

	g.printEdge();	

	return 0;
}
Viper