81542024-01-12 14:49:17szilTitkos sorozatcpp17Accepted 40/4043ms27788 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 200'001;

int a[MAXN], ans[MAXN], timer = 1;
bool vis[MAXN];
vector<int> g[MAXN];

void dfs(int u) {
	vis[u]=1;
	for (int v : g[u]) {
		if (!vis[v]) dfs(v);
	}
	ans[u] = timer++;
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int n; cin >> n;
	int x = n, last = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		if (a[i] == -1) {
			if (last) {
				g[i].emplace_back(last);
			}
			last = i;
		} else {
			g[i].emplace_back(a[i]);
		}
	}
	for (int i = 1; i <= n; i++) {
		if (a[i] != -1) g[i].emplace_back(last);
	}
	for (int i = 1; i <= n; i++) {
		if (!vis[i]) dfs(i);
	}
	for (int i = 1; i <= n; i++) {
		cout << n-ans[i]+1 << " ";
	}
	cout << "\n";
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/06ms11332 KiB
2Accepted0/024ms16092 KiB
3Accepted1/16ms11932 KiB
4Accepted1/16ms12104 KiB
5Accepted2/26ms12132 KiB
6Accepted1/16ms12172 KiB
7Accepted1/16ms12208 KiB
8Accepted2/26ms12352 KiB
9Accepted2/239ms20448 KiB
10Accepted2/241ms20620 KiB
11Accepted2/239ms20664 KiB
12Accepted2/241ms21080 KiB
13Accepted2/239ms21276 KiB
14Accepted2/239ms21840 KiB
15Accepted2/241ms22652 KiB
16Accepted3/341ms22856 KiB
17Accepted3/343ms23372 KiB
18Accepted3/343ms24036 KiB
19Accepted3/343ms24812 KiB
20Accepted3/343ms27788 KiB
21Accepted1/141ms23296 KiB
22Accepted1/141ms23608 KiB
23Accepted1/141ms24108 KiB