Submission
# | When | Author | Problem | Language | CPU | Memory | |
---|---|---|---|---|---|---|---|
20851 | 2024-06-12 23:30:18 | AHAMMED_99 | Cheaters | C++ 14 | 1 ms | 3340 kb | Accepted |
Source Code
#include <iostream> #include <vector> using namespace std; void findCheaters(const vector<int>& a, int n) { vector<int> next(n + 1, -1); int start = -1; // Initialize the next array and find the starter for (int i = 0; i < n; ++i) { if (a[i] == i + 1) { start = i + 1; } else { next[a[i]] = i + 1; } } // Trace the order from the starter vector<int> order; int current = start; while (current != -1) { order.push_back(current); current = next[current]; } // Print the result for (int i = 0; i < order.size(); ++i) { cout << order[i]; if (i != order.size() - 1) { cout << " "; } } cout << endl; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } findCheaters(a, n); } return 0; }