Loading...
Submission
# When Author Problem Language CPU Memory
7101 2023-03-03 13:54:16 heisenberg_120 Cheaters C++ 17 6 ms 3528 kb Wrong Answer - 2
Test Cases
# CPU Memory Points
1 3 ms 3408 kb 1 Accepted
2 6 ms 3528 kb 0 Wrong Answer
Source Code
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. const int N = 55;
  5. vector<int> g[N];
  6. vector<bool> vis(N), used(N);
  7.  
  8. void DFS(int cur){
  9. vis[cur] = true;
  10. cout << cur << ' ';
  11. for(auto x : g[cur]){
  12. if(!vis[x]){
  13. DFS(x);
  14. }
  15. }
  16. }
  17.  
  18. signed main(){
  19. ios_base::sync_with_stdio(false);
  20. cin.tie(nullptr);
  21. int tt;
  22. cin >> tt;
  23. while (tt--){
  24. int n;
  25. cin >> n;
  26. for(int i = 0; i <= n; i++){
  27. g[i].clear();
  28. vis[i] = false;
  29. used[i] = false;
  30. }
  31. vector<int> a(n + 1); int boss;
  32. for(int i = 1; i <= n; i++){
  33. cin >> a[i];
  34. if(a[i] == i){
  35. boss = i;
  36. }
  37. }
  38. vector<int> c;
  39. for(int i = 1; i <= n; i++){
  40. if(a[i] == boss and i != boss){
  41. c.push_back(i);
  42. }
  43. }
  44. used[boss] = true;
  45. for(auto x : c){
  46. if(x != boss){
  47. g[boss].push_back(x);
  48. used[x] = true;
  49. }
  50. }
  51. for(auto x : c){
  52. int yy = x - 1;
  53. while (yy > 0 and !used[yy]){
  54. used[yy] = true;
  55. g[x].push_back(yy);
  56. yy--;
  57. }
  58. }
  59. for(auto x : c){
  60. int yy = x + 1;
  61. while (yy <= n and !used[yy]){
  62. used[yy] = true;
  63. g[x].push_back(yy);
  64. yy++;
  65. }
  66. }
  67. int x = boss - 1, y = boss + 1;
  68. while (x > 0 and !used[x]){
  69. used[x] = true;
  70. g[boss].push_back(x);
  71. x--;
  72. }
  73. while (y <= n and !used[y]){
  74. used[y] = true;
  75. g[boss].push_back(x);
  76. y++;
  77. }
  78. DFS(boss);
  79. cout << '\n';
  80. }
  81. return 0;
  82. }