52892023-04-25 12:59:22ZsofiaKeresztelyKülönböző katicákcpp14Accepted 100/10086ms33796 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define fi first
#define se second
vector<pii> iv;
vector<int> p, c, d;
vector<int> odd;
vector<vector<int> > g;
bool op = true;

void dfs1(int v){
    for (int x : g[v]){
        d[x] = d[v] + 1;
        dfs1(x);
        if (!op) return;
        iv[v].fi = max(iv[v].fi, iv[x].fi - 1);
        iv[v].se = min(iv[v].se, iv[x].se + 1);
        if (odd[v] == odd[x] && odd[v] >= 0 || iv[v].se < iv[v].fi){
            op = false;
            return;
        }
        if (odd[x] >=0) odd[v] = 1 - odd[x];
    }
}

void dfs2(int v){
    iv[v].fi = max(iv[v].fi, iv[p[v]].fi - 1);
    iv[v].se = min(iv[v].se, iv[p[v]].se + 1);
    if (odd[v] == odd[p[v]] && odd[v] >= 0 || iv[v].se < iv[v].fi){
        op = false;
        return;
    }
    if (odd[p[v]] >=0) odd[v] = 1 - odd[p[v]];
    for (int x : g[v]){
        dfs2(x);
    }
}

int main()
{
    //freopen("be3.txt", "r", stdin);
    int n;
    cin >> n;
    g.resize(n+1);
    p.resize(n+1);
    d.resize(n+1);
    c.resize(n+1);
    odd.assign(n+1, -1);
    iv.assign(n+1, {0, 1e9});
    for (int i=1; i<=n; i++){
        cin >> p[i];
        g[p[i]].push_back(i);
    }
    for (int i=1; i<=n; i++){
        cin >> c[i];
        if (c[i] >= 0){
            iv[i] = {c[i], c[i]};
            odd[i] = c[i] % 2;
        }
    }
    d[1] = 0;
    dfs1(1);
    if (!op){
        cout << "NEM";
        return 0;
    }
    dfs2(1);
    if (!op){
        cout << "NEM";
        return 0;
    }
    cout << "IGEN\n";
    if (iv[1].se == 1e9){
        for (int i=1; i<=n; i++){
            cout << d[i] << " ";
        }
    }
    else{
        for (int i=1; i<=n; i++){
            if (!iv[i].fi && odd[i]) cout << 1 << " ";
            else cout << iv[i].fi << " ";
        }
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1876 KiB
2Accepted3ms2120 KiB
3Accepted74ms14156 KiB
subtask210/10
4Accepted63ms12924 KiB
5Accepted70ms13800 KiB
6Accepted75ms15100 KiB
7Accepted82ms16356 KiB
subtask315/15
8Accepted70ms32944 KiB
9Accepted71ms32548 KiB
10Accepted86ms33796 KiB
11Accepted83ms32960 KiB
12Accepted82ms32312 KiB
13Accepted85ms33256 KiB
14Accepted78ms32048 KiB
subtask435/35
15Accepted3ms4000 KiB
16Accepted3ms4048 KiB
17Accepted3ms3916 KiB
18Accepted3ms3916 KiB
19Accepted3ms3912 KiB
20Accepted3ms4168 KiB
21Accepted3ms4376 KiB
22Accepted3ms4336 KiB
subtask540/40
23Accepted75ms16324 KiB
24Accepted65ms17388 KiB
25Accepted65ms17460 KiB
26Accepted75ms16688 KiB
27Accepted75ms16420 KiB
28Accepted82ms17460 KiB
29Accepted81ms17700 KiB
30Accepted78ms17128 KiB
31Accepted79ms17128 KiB
32Accepted78ms17384 KiB
33Accepted86ms18376 KiB