51112023-04-17 22:46:26nmarciKülönböző katicákcpp11Accepted 100/10087ms32352 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long int;

int p[100100];

vector<int> g[100100];
int f[100100];

pair<int,int> s[100100];

void dfs(int node){
    s[node] = {p[node], p[node]};
    for(auto i : g[node]){
        dfs(i);
        int l = s[i].first, r = s[i].second;
        if(l != -1 && r != -1){
            if(l == 0){
                ++l;
            }
            else{
                --l;
            }
            ++r;
            if(s[node].first == -1){
                s[node] = {l, r};
            }
            else if(s[node].first % 2 != l % 2 || r < s[node].first || l > s[node].second){
                cout << "NEM" << endl;
                exit(0);
            }
            if(p[node] == -1){
                s[node].first = max(s[node].first, l);
                s[node].second = min(s[node].second, r);
            }
        }
    }
}

void c(int node){
    if(p[node] == -1){
        int l = s[node].first, r = s[node].second;
        if(l <= p[f[node]] - 1 && r >= p[f[node]] - 1 && p[f[node]] - 1 >= 0){
            p[node] = p[f[node]] - 1;
        }
        else{
            p[node] = p[f[node]] + 1;
        }
    }
    for(auto i : g[node]){
        c(i);
    }
}

int main(){
    int n;
    cin >> n;
    for(int i = 1;  i <= n; ++i){
        cin >> f[i];
        g[f[i]].push_back(i);
    }
    for(int i = 1;  i <= n; ++i){
        cin >> p[i];
    }
    dfs(1);
    if(p[1] == -1){
        p[1] = s[1].first;
    }
    c(1);
    cout << "IGEN" << endl;
    for(int i = 1; i <= n; ++i){
        cout << p[i] << " ";
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms6664 KiB
2Accepted4ms6856 KiB
3Accepted71ms13400 KiB
subtask210/10
4Accepted61ms12828 KiB
5Accepted68ms13632 KiB
6Accepted75ms14444 KiB
7Accepted79ms14848 KiB
subtask315/15
8Accepted68ms31644 KiB
9Accepted67ms31680 KiB
10Accepted87ms32352 KiB
11Accepted81ms31788 KiB
12Accepted82ms31344 KiB
13Accepted83ms32244 KiB
14Accepted79ms31232 KiB
subtask435/35
15Accepted4ms8444 KiB
16Accepted4ms8564 KiB
17Accepted4ms8840 KiB
18Accepted4ms9076 KiB
19Accepted4ms9060 KiB
20Accepted4ms8960 KiB
21Accepted4ms9096 KiB
22Accepted4ms9192 KiB
subtask540/40
23Accepted74ms15604 KiB
24Accepted64ms15144 KiB
25Accepted67ms14756 KiB
26Accepted74ms15716 KiB
27Accepted74ms15736 KiB
28Accepted79ms16052 KiB
29Accepted79ms16276 KiB
30Accepted75ms16156 KiB
31Accepted75ms15972 KiB
32Accepted79ms16140 KiB
33Accepted82ms16436 KiB