-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadj_list.cpp
More file actions
31 lines (26 loc) · 748 Bytes
/
adj_list.cpp
File metadata and controls
31 lines (26 loc) · 748 Bytes
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
#include <iostream>
#include <list>
using namespace std;
//make an adjacency list from a list of edges
//number of vertices is given and the graph is simple
int main(){
int num_vertices;
cin >> num_vertices;
list<int> *adjl = new list<int>[num_vertices];
int edge1,edge2;
//two because the graph is undirected
while(cin >> edge1 >> edge2){
adjl[edge1-1].push_back(edge2-1);
adjl[edge2-1].push_back(edge1-1);
}
int index = 0;
while(index < num_vertices){
list<int>::iterator it;
cout << index+1 << ": ";
for(it = adjl[index].begin(); it != adjl[index].end(); ++it)
cout << (*it) + 1 << " ";
cout << endl;
index++;
}
return 0;
}