Submission
# | When | Author | Problem | Language | CPU | Memory | |
---|---|---|---|---|---|---|---|
7100 | 2023-03-03 13:47:22 | heisenberg_120 | Cheaters | C++ 17 | 3 ms | 3496 kb | Wrong Answer - 2 |
Source Code
#include<bits/stdc++.h> using namespace std; #define int long long int const int N = 55; vector<int> g[N]; vector<bool> vis(N), used(N); void DFS(int cur){ vis[cur] = true; cout << cur << ' '; for(auto x : g[cur]){ if(!vis[x]){ DFS(x); } } } signed main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int tt; cin >> tt; while (tt--){ int n; cin >> n; for(int i = 0; i <= n; i++){ g[i].clear(); vis[i] = false; used[i] = false; } vector<int> a(n + 1); int boss; for(int i = 1; i <= n; i++){ cin >> a[i]; if(a[i] == i){ boss = i; } } vector<int> c; c.push_back(boss); for(int i = 1; i <= n; i++){ if(a[i] == boss and i != boss){ c.push_back(i); } } used[boss] = true; for(auto x : c){ if(x != boss){ g[boss].push_back(x); used[x] = true; } } for(auto x : c){ int yy = x - 1; while (yy > 0 and !used[yy]){ used[yy] = true; g[x].push_back(yy); yy--; } } for(auto x : c){ int yy = x + 1; while (yy <= n and !used[yy]){ used[yy] = true; g[x].push_back(yy); yy++; } } DFS(boss); cout << '\n'; } return 0; }