Skip to content

Commit

Permalink
introduce piggybanks_dfs
Browse files Browse the repository at this point in the history
  • Loading branch information
witek-formanski committed Dec 7, 2023
1 parent 996afe6 commit d1b7834
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions src/cw9/zad1/piggybanks_dfs.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
// // inny pomysł: find-union
/*
// 0 1 2 3 4 5 6 7
// 2 0 4 6 0 7 3 5
// 0 1 0 3 0 5 3 5
// +1 +1 +1
T(n) = O(n)
M(n) = O(n)
0 1 2 3 4 5 6 7
2 0 4 6 0 7 3 5
0 1 0 3 0 5 3 5
+1 +1 +1
*/

#include <vector>

int piggybanks_dfs(std::vector<int> piggybanks) {
std::vector<int> cycles(piggybanks.size(), -1);
int closed_cycles_count = 0;
int next_pig;
for (int i = 0; i < piggybanks.size(); i++) {
if (cycles[i] == -1) {
cycles[i] = i;
next_pig = piggybanks[i];
while (cycles[next_pig] == -1) {
cycles[next_pig] = i;
next_pig = piggybanks[next_pig];
}
if (next_pig == i) closed_cycles_count++;
}
}

return closed_cycles_count;
}

#include <iostream>

int main() {
std::cout << piggybanks_dfs({2, 0, 4, 6, 0, 7, 3, 5});
}

// inny pomysł: find-union

0 comments on commit d1b7834

Please sign in to comment.