81632024-01-12 15:04:04UnluckYTitkos sorozatcpp17Accepted 40/4068ms23672 KiB
#include <bits/stdc++.h>

using namespace std;


vector<int> tops;
vector<bool> seen;
vector<vector<int>> v;

void dfs(int x){
    if (seen[x]) return;
    seen[x] = true;
    for (int i : v[x]) dfs(i);
    tops.push_back(x);
}


int main() {
	
	int n; cin >> n;

    v.assign(n+1, {});
    seen.assign(n+1, false);
    vector<int> s;
	int el = -1;
    s.push_back(0);

    for (int i = 1; i <= n; i++){
        int a; cin >> a;
		s.push_back(a);
        if (a == -1){
            if (el != -1){
				v[i].push_back(el);
			}
			el = i;
            continue;
        }
        v[i].push_back(a);
    }

	for (int i = 1; i <= n; i++){
		if (s[i] != -1){
			v[i].push_back(el);
		}
	}

    for (int i = 1; i <= n; i++) dfs(i);

    reverse(tops.begin(), tops.end());

    vector<int> asd(n+1);

    for (int i = 1; i <= n; i++) asd[tops[i]] = i;

    for (int i = 1; i <= n; i++) cout << asd[i]+1 << " "; // 2 3 5 4 1 

    return 0;


}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1812 KiB
2Accepted0/034ms8940 KiB
3Accepted1/13ms2224 KiB
4Accepted1/13ms2436 KiB
5Accepted2/23ms2544 KiB
6Accepted1/13ms2932 KiB
7Accepted1/13ms3144 KiB
8Accepted2/23ms3360 KiB
9Accepted2/265ms16520 KiB
10Accepted2/264ms16636 KiB
11Accepted2/264ms16628 KiB
12Accepted2/265ms17108 KiB
13Accepted2/265ms17232 KiB
14Accepted2/264ms17884 KiB
15Accepted2/267ms18788 KiB
16Accepted3/365ms18892 KiB
17Accepted3/364ms19180 KiB
18Accepted3/367ms19928 KiB
19Accepted3/365ms20900 KiB
20Accepted3/368ms23672 KiB
21Accepted1/164ms19256 KiB
22Accepted1/164ms19656 KiB
23Accepted1/164ms20096 KiB