7652022-01-09 18:35:06Szin AttilaTitkos sorozatcpp14Wrong answer 0/4041ms26104 KiB
#include <bits/stdc++.h>
using namespace std;
#define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using ll = long long;

const int maxN = 2e5 + 5;
const int MOD = 1e9 + 7;

vector<vector<int> > g(maxN);
vector<bool> volt(maxN), elhagy(maxN);
vector<int> sorrend, mo;

void dfs(int x)
{
	volt[x] = true;
	for (int sz : g[x]) {
		if (!volt[sz]) {
			dfs(sz);
		}
		else if (!elhagy[sz]) {
			cout << "IMPOSSIBLE" << endl;
			exit(0);
		}
	}
	if(mo[x] == -1)sorrend.push_back(x);
	elhagy[x] = true;
}

int main() {

#pragma region
#ifndef ONLINE_JUDGE
   freopen("../input.txt", "r", stdin);
   freopen("../output.txt", "w", stdout);
#endif

   InTheNameOfGod;
#pragma endregion
    
    int n;
    cin >> n;
    vector<int> v(n);
    mo.resize(n, -1);
    for(int i = 0; i < n; i++) {
        cin >> v[i];
    }
    int curr = n;

    for(int i = 0; i < n; i++) {
        if(v[i] == -1) mo[i] = curr--;
        else {
            g[v[i]].push_back(i);
        }
    }

    for (int i = 1; i <= n; i++) {
		if (!volt[i])
			dfs(i);
	}

    for(int i = 0; i < sorrend.size(); i++) {
        mo[sorrend[i]] = i+1;
    }

    for(int a : mo) cout << a << ' ';

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base0/40
1Accepted0/06ms10916 KiB
2Wrong answer0/019ms14716 KiB
3Wrong answer0/16ms11316 KiB
4Wrong answer0/14ms11320 KiB
5Wrong answer0/24ms11320 KiB
6Wrong answer0/16ms11328 KiB
7Wrong answer0/16ms11332 KiB
8Wrong answer0/26ms11332 KiB
9Wrong answer0/234ms18144 KiB
10Wrong answer0/232ms18744 KiB
11Wrong answer0/237ms19292 KiB
12Wrong answer0/232ms19892 KiB
13Wrong answer0/232ms20492 KiB
14Wrong answer0/237ms21212 KiB
15Wrong answer0/237ms21872 KiB
16Wrong answer0/337ms22528 KiB
17Wrong answer0/335ms23212 KiB
18Wrong answer0/339ms23872 KiB
19Wrong answer0/335ms24648 KiB
20Wrong answer0/335ms26104 KiB
21Wrong answer0/135ms24608 KiB
22Wrong answer0/132ms25072 KiB
23Wrong answer0/141ms25436 KiB