Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //решение
- В этой задаче вам просто нужно написать то, что сказано в условии. Для ребенка i следующий псевдокод сможет посчитать ответ (индексы массива p и его значения 0-индексированы):
- pos = p[i]
- ans = 1
- while pos != i:
- ans += 1
- pos = p[pos]
- //реализация
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- #endif
- int q;
- cin >> q;
- for (int i = 0; i < q; ++i) {
- int n;
- cin >> n;
- vector<int> p(n);
- for (int j = 0; j < n; ++j) {
- cin >> p[j];
- --p[j];
- }
- for (int j = 0; j < n; ++j) {
- int cnt = 0;
- int k = j;
- do {
- ++cnt;
- k = p[k];
- } while (k != j);
- cout << cnt << " ";
- }
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment