Loading...
Submission
# When Author Problem Language CPU Memory
20851 2024-06-12 23:30:18 AHAMMED_99 Cheaters C++ 14 1 ms 3340 kb Accepted
Test Cases
# CPU Memory Points
1 1 ms 3308 kb 1 Accepted
2 1 ms 3340 kb 1 Accepted
Source Code
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. void findCheaters(const vector<int>& a, int n) {
  7. vector<int> next(n + 1, -1);
  8. int start = -1;
  9.  
  10. // Initialize the next array and find the starter
  11. for (int i = 0; i < n; ++i) {
  12. if (a[i] == i + 1) {
  13. start = i + 1;
  14. } else {
  15. next[a[i]] = i + 1;
  16. }
  17. }
  18.  
  19. // Trace the order from the starter
  20. vector<int> order;
  21. int current = start;
  22. while (current != -1) {
  23. order.push_back(current);
  24. current = next[current];
  25. }
  26.  
  27. // Print the result
  28. for (int i = 0; i < order.size(); ++i) {
  29. cout << order[i];
  30. if (i != order.size() - 1) {
  31. cout << " ";
  32. }
  33. }
  34. cout << endl;
  35. }
  36.  
  37. int main() {
  38. int t;
  39. cin >> t;
  40. while (t--) {
  41. int n;
  42. cin >> n;
  43. vector<int> a(n);
  44. for (int i = 0; i < n; ++i) {
  45. cin >> a[i];
  46. }
  47. findCheaters(a, n);
  48. }
  49. return 0;
  50. }
  51.  
  52.