diff --git a/src/cw9/zad1/piggybanks_dfs.cpp b/src/cw9/zad1/piggybanks_dfs.cpp index a06f1dc..4d17d85 100644 --- a/src/cw9/zad1/piggybanks_dfs.cpp +++ b/src/cw9/zad1/piggybanks_dfs.cpp @@ -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 + +int piggybanks_dfs(std::vector piggybanks) { + std::vector 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 + +int main() { + std::cout << piggybanks_dfs({2, 0, 4, 6, 0, 7, 3, 5}); +} + +// inny pomysł: find-union \ No newline at end of file