52882023-04-25 12:47:46ZsofiaKeresztelyKülönböző katicákcpp14Wrong answer 10/10087ms36752 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()
{
    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++){
            cout << iv[i].fi << " ";
        }
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1876 KiB
2Accepted3ms2068 KiB
3Wrong answer75ms14928 KiB
subtask210/10
4Accepted64ms13952 KiB
5Accepted70ms16316 KiB
6Accepted76ms18256 KiB
7Accepted82ms19664 KiB
subtask30/15
8Accepted71ms35852 KiB
9Accepted71ms35716 KiB
10Wrong answer87ms36752 KiB
11Wrong answer85ms35912 KiB
12Accepted82ms35520 KiB
13Accepted86ms36248 KiB
14Accepted81ms35056 KiB
subtask40/35
15Accepted3ms6872 KiB
16Accepted3ms6872 KiB
17Accepted3ms6996 KiB
18Wrong answer3ms7008 KiB
19Accepted3ms7260 KiB
20Wrong answer3ms7228 KiB
21Wrong answer3ms7212 KiB
22Accepted3ms7224 KiB
subtask50/40
23Accepted75ms19204 KiB
24Accepted64ms20244 KiB
25Accepted65ms20460 KiB
26Wrong answer75ms19748 KiB
27Wrong answer75ms19436 KiB
28Accepted81ms20348 KiB
29Wrong answer81ms20624 KiB
30Wrong answer79ms19840 KiB
31Wrong answer76ms19836 KiB
32Wrong answer78ms19840 KiB
33Wrong answer83ms20796 KiB