This is from a blog post on Codeforces. I couldn't really understand why the editorialist goes on to claim that this code works in O(n m)
This is a graph problem, where we are supposed to find the number of ways to traverse from a to c. Note that only the paths like a-->b-->c and a-->d-->c need to be considered. That is, there should be a difference of only one node, in between them.
n is the number of vertices, m is the number of edges and nxt is the adjacency list.
Let's iterate through all combinations of a and c just two simple nested loops in O(n^2) and find all candidates for b and d inside. To find candidates you can go through all neighbors of a and check that they are neighbors of c. Among all the candidates you should choose two junctions as b and d. So just use https://en.wikipedia.org/wiki/Combination All you need is to add to the answer , where r is the number of candidates (common neighbors of a and c). The code is:
for (int a = 0; a < n; a++) for (int c = 0; c < n; c++) if (a != c) { int r = 0; for (int b = 0; b < nxt[a].size(); b++) if (nxt[a][b] != a && nxt[a][b] != c && g[nxt[a][b]][c]) r++; result += r * (r - 1) / 2; }
It is easy to see that the total complexity is O(nm), because of sum of number of neighbors over all junctions is exactly m.
If there are 3 loops involved, then how can the worst case complexity be O(n m)
nxt
array is adjacency list of the graph.