Skip to content

Instantly share code, notes, and snippets.

@30mb1
Created November 19, 2015 07:37
Show Gist options
  • Save 30mb1/7f02cbd6f6c0490ab866 to your computer and use it in GitHub Desktop.
Save 30mb1/7f02cbd6f6c0490ab866 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
void cycle(int& numbvec, const int cyclebegin, int& summ, std::vector<int>& MainVec) {
if (MainVec[numbvec] == cyclebegin) {
summ = summ + 1;
MainVec[numbvec] = 0;
} else {
cycle(MainVec[numbvec], cyclebegin, summ, MainVec);
MainVec[numbvec] = 0;
}
}
int main() {
int n, summ = 0;
std::cin >> n;
std::vector<int> Vec;
for (int i = 0; i < n; i++) {
int figure;
std::cin >> figure;
Vec.push_back(figure - 1);
}
for (int ii = 0; ii < n; ii++) {
if ((Vec[ii] == ii) || (Vec[ii] == 0)) {
continue;
} else {
cycle(Vec[ii], ii, summ, Vec);
}
}
std::cout << summ << "\n" << std::endl << "\n" << std::endl << "\n";
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment