Skip to content

Commit

Permalink
Add count 3 cycle and 4 cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
OmeletWithoutEgg committed Sep 29, 2023
1 parent de1e58c commit 4499e28
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions codes/Graph/CountCycles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sort(ord.begin(), ord.end(), [&](int i, int j) {
return pair(deg[i], i) > pair(deg[j], j); });
for (int i = 0; i < n; i++) rnk[ord[i]] = i;
if (rnk[u] < rnk[v]) D[u].push_back(v); // D: new DAG
for (int x = 0; x < n; x++) { // c3
for (int y : D[x]) vis[y] = 1;
for (int y : D[x]) for (int z : D[y]) ans += vis[z];
for (int y : D[x]) vis[y] = 0;
}
for (int x = 0; x < n; x++) { // c4
for (int y : D[x]) for (int z : adj[y])
if (rnk[z] > rnk[x]) ans += vis[z]++;
for (int y : D[x]) for (int z : adj[y])
if (rnk[z] > rnk[x]) vis[z]--;
} // both are O(M * sqrt(M))

0 comments on commit 4499e28

Please sign in to comment.