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