81632024-01-12 15:04:04UnluckYTitkos sorozatcpp17Elfogadva 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;


}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base40/40
1Elfogadva0/03ms1812 KiB
2Elfogadva0/034ms8940 KiB
3Elfogadva1/13ms2224 KiB
4Elfogadva1/13ms2436 KiB
5Elfogadva2/23ms2544 KiB
6Elfogadva1/13ms2932 KiB
7Elfogadva1/13ms3144 KiB
8Elfogadva2/23ms3360 KiB
9Elfogadva2/265ms16520 KiB
10Elfogadva2/264ms16636 KiB
11Elfogadva2/264ms16628 KiB
12Elfogadva2/265ms17108 KiB
13Elfogadva2/265ms17232 KiB
14Elfogadva2/264ms17884 KiB
15Elfogadva2/267ms18788 KiB
16Elfogadva3/365ms18892 KiB
17Elfogadva3/364ms19180 KiB
18Elfogadva3/367ms19928 KiB
19Elfogadva3/365ms20900 KiB
20Elfogadva3/368ms23672 KiB
21Elfogadva1/164ms19256 KiB
22Elfogadva1/164ms19656 KiB
23Elfogadva1/164ms20096 KiB