ProgMe

Codeforces_forever Round #1 (Div3) B1

Apr 25th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. //решение
  2. В этой задаче вам просто нужно написать то, что сказано в условии. Для ребенка i следующий псевдокод сможет посчитать ответ (индексы массива p и его значения 0-индексированы):
  3.  
  4.  
  5. pos = p[i]
  6. ans = 1
  7. while pos != i:
  8. ans += 1
  9. pos = p[pos]
  10. //реализация
  11. #include <bits/stdc++.h>
  12.  
  13. using namespace std;
  14.  
  15. int main() {
  16. #ifdef _DEBUG
  17. freopen("input.txt", "r", stdin);
  18. // freopen("output.txt", "w", stdout);
  19. #endif
  20.  
  21. int q;
  22. cin >> q;
  23. for (int i = 0; i < q; ++i) {
  24. int n;
  25. cin >> n;
  26. vector<int> p(n);
  27. for (int j = 0; j < n; ++j) {
  28. cin >> p[j];
  29. --p[j];
  30. }
  31. for (int j = 0; j < n; ++j) {
  32. int cnt = 0;
  33. int k = j;
  34. do {
  35. ++cnt;
  36. k = p[k];
  37. } while (k != j);
  38. cout << cnt << " ";
  39. }
  40. cout << endl;
  41. }
  42.  
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment