As we know graph can be represented using either adjacency matrix or adjacency list.
Below is how we can save graph using adjacenct list using STL vectors.
N is the number of nodes in the graph.
I is the number of edges in the graph.
a,b are the nodes connected.
Also note that the graph is un-directed graph with edge weight "1".
vector<vector<int> > pairs(N);
for (int i = 0; i < I; ++i) {
int a, b;
cin >> a >> b;
pairs[a].push_back(b);
pairs[b].push_back(a);
}
No comments:
Post a Comment