fork download
  1. /// Codeforces E - Round Dace
  2. /// https://c...content-available-to-author-only...s.com/contest/1833/problem/E
  3. /// Author: Qwerty
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. const int MAXN = 2e5 + 100;
  7. int g[MAXN];
  8. int vis[MAXN];
  9. int main(){
  10. ios_base::sync_with_stdio(0);
  11. cin.tie(0);
  12. int t;
  13. cin >> t;
  14. while (t--){
  15. int n;
  16. cin >> n;
  17. memset(vis, 0, sizeof(vis));
  18. for (int i = 1; i <= n; i++){
  19. cin >> g[i];
  20. }
  21. int b = 0;
  22. int c = 0;
  23. for (int i = 1; i <= n; i++){
  24. if (!vis[i]){
  25. int x = i;
  26. int p = x;
  27. int pp = p;
  28. while (!vis[x]){
  29. vis[x] = 1;
  30. int v = g[x];
  31. pp = p;
  32. p = x;
  33. x = v;
  34. }
  35. if (x == pp) b++;
  36. else if (x == i) c++;
  37. }
  38. }
  39. cout << c + min(b, 1) << " " << c + b << '\n';
  40. }
  41. }
  42.  
Success #stdin #stdout 0.01s 5292KB
stdin
10
6
2 1 4 3 6 5
6
2 3 1 5 6 4
9
2 3 2 5 6 5 8 9 8
2
2 1
4
4 3 2 1
5
2 3 4 5 1
6
5 3 4 1 1 2
5
3 5 4 1 2
6
6 3 2 5 4 3
6
5 1 4 3 4 2
stdout
1 3
2 2
1 3
1 1
1 2
1 1
1 1
2 2
1 2
1 1