Need a faster way to create an adjacency list in c++

  adjacency-list, arrays, c++

I’m trying to create an adjacency list from an input of vertices, edges and single connections. The input looks like this:

3 2 (vertices, edges)

1 2 (connection)

1 3

Right now, my code is

int vertices, edges;
scanf("%d %d", &vertices, &edges);

vector<int> storage[vertices+1];
for (int i = 0; i < edges; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    if (find(storage[b].begin(), storage[b].end(), a) != storage[b].end() == false) {
        storage[b].push_back(a);
    }
    if (find(storage[a].begin(), storage[a].end(), b) != storage[a].end() == false) {
        storage[a].push_back(b);
    }
}

Is there a faster/more efficient way to do this, or is this the best way?

Source: Windows Questions C++

LEAVE A COMMENT