Submission #73300439
Source Code Expand
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN = 500010;
int A[MAXN];
int ans[MAXN];
int vis[MAXN];
void solve(int start, int N) {
vector<int> path;
int u = start;
while (vis[u] == 0) {
vis[u] = 1;
path.push_back(u);
u = A[u];
if (u < 1 || u > N) break;
}
int final_pos;
if (vis[u] == 2) {
final_pos = ans[u];
} else {
int cycle_start_idx = 0;
while (cycle_start_idx < (int)path.size() && path[cycle_start_idx] != u) {
cycle_start_idx++;
}
final_pos = u;
for (int i = cycle_start_idx; i < (int)path.size(); ++i) {
ans[path[i]] = final_pos;
vis[path[i]] = 2;
}
}
for (int i = 0; i < (int)path.size(); ++i) {
if (vis[path[i]] == 2) break;
ans[path[i]] = final_pos;
vis[path[i]] = 2;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
for (int i = 1; i <= N; ++i) {
cin >> A[i];
if (A[i] < 1 || A[i] > N) {
A[i] = i;
}
}
memset(ans, 0, sizeof(ans));
memset(vis, 0, sizeof(vis));
for (int s = 1; s <= N; ++s) {
if (vis[s] == 0) {
solve(s, N);
}
}
for (int s = 1; s <= N; ++s) {
if (s > 1) cout << " ";
cout << ans[s];
}
cout << "\n";
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | C - Sugoroku Destination |
| User | stone_shadow |
| Language | C++23 (GCC 15.2.0) |
| Score | 300 |
| Code Size | 1563 Byte |
| Status | AC |
| Exec Time | 58 ms |
| Memory | 11356 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 300 / 300 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
| All | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_00.txt | AC | 3 ms | 7500 KiB |
| 00_sample_01.txt | AC | 3 ms | 7632 KiB |
| 00_sample_02.txt | AC | 3 ms | 7540 KiB |
| 01_random_03.txt | AC | 58 ms | 9960 KiB |
| 01_random_04.txt | AC | 58 ms | 9980 KiB |
| 01_random_05.txt | AC | 58 ms | 9948 KiB |
| 01_random_06.txt | AC | 58 ms | 9920 KiB |
| 01_random_07.txt | AC | 57 ms | 9880 KiB |
| 01_random_08.txt | AC | 58 ms | 9892 KiB |
| 01_random_09.txt | AC | 57 ms | 9976 KiB |
| 01_random_10.txt | AC | 57 ms | 9776 KiB |
| 01_random_11.txt | AC | 22 ms | 8272 KiB |
| 01_random_12.txt | AC | 19 ms | 8272 KiB |
| 01_random_13.txt | AC | 51 ms | 9424 KiB |
| 01_random_14.txt | AC | 6 ms | 7700 KiB |
| 01_random_15.txt | AC | 57 ms | 9880 KiB |
| 01_random_16.txt | AC | 3 ms | 7516 KiB |
| 01_random_17.txt | AC | 42 ms | 11356 KiB |