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 |
Source Code
#include <iostream> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; void findCheaters(vector<int>& a, int n) { unordered_map<int, int> parentMap; int start; // Initialize the parent map and find the starter for (int i = 0; i < n; ++i) { if (a[i] == i + 1) { start = i + 1; } parentMap[a[i]] = i + 1; } // Trace the order from the starter vector<int> order; int current = start; while (parentMap.find(current) != parentMap.end()) { order.push_back(current); current = parentMap[current]; } order.push_back(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; }