67182023-12-17 19:40:25111Titkos sorozatcpp17Accepted 40/4035ms16696 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double

#define pii pair<int, int>

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef CB
	freopen("be2.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif
	int N;
	cin >> N;
	vector<vector<int>> g(N + 1);
	vector<int> w(N + 1);
	for (int a = 1; a <= N; a++) {
		int b;
		cin >> b;
		w[a]=b;
		if (b == -1) {
			g[0].push_back(a);
		}
		else {
			g[b].push_back(a);
		}
	}
	vector<int> a(N + 1, -1);
	int x = N;
	deque<int> q;
	a[0] = 0;
	q.push_back(0);
	while (!q.empty()) {
		int i = q.front();
		q.pop_front();
		for (int j : g[i]) {
			if (a[j] == -1) {
				a[j] = x--;
				q.push_back(j);
			}
		}
	}
	for (int i = 1; i <= N; i++) {
		cout << a[i] << ' ';
	}
	cout << '\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1828 KiB
2Accepted0/018ms7796 KiB
3Accepted1/13ms2280 KiB
4Accepted1/13ms2452 KiB
5Accepted2/23ms2668 KiB
6Accepted1/13ms2816 KiB
7Accepted1/13ms2964 KiB
8Accepted2/23ms2848 KiB
9Accepted2/235ms14572 KiB
10Accepted2/235ms14848 KiB
11Accepted2/234ms14432 KiB
12Accepted2/235ms14644 KiB
13Accepted2/234ms14860 KiB
14Accepted2/234ms14884 KiB
15Accepted2/234ms15404 KiB
16Accepted3/334ms15492 KiB
17Accepted3/334ms15716 KiB
18Accepted3/334ms15700 KiB
19Accepted3/335ms15792 KiB
20Accepted3/335ms16696 KiB
21Accepted1/132ms15932 KiB
22Accepted1/132ms15864 KiB
23Accepted1/132ms15988 KiB