39152023-03-05 13:02:18zsomborKülönböző katicákcpp17Accepted 100/10087ms36760 KiB
#include <iostream>
#include <vector>
using namespace std;

int n;
bool lehet = true;
vector <vector <int>> g(1e5 + 1);
vector <int> f(1e5 + 1, 0);
vector <int> p(1e5 + 1, -1);
vector <int> mn(1e5 + 1, -1e9);
vector <int> mx(1e5 + 1, 1e9);
vector <int> par(1e5 + 1, -1);

void dfs1(int x) {
    if (p[x] > -1) {
        mn[x] = mx[x] = p[x];
        par[x] = p[x] % 2;
    }
    for (int i : g[x]) {
        dfs1(i);
        mn[x] = max(mn[x], mn[i] - 1);
        mx[x] = min(mx[x], mx[i] + 1);
        if (par[x] == -1 && par[i] > -1) par[x] = 1 - par[i];
        if (par[x] > -1 && par[i] > -1 && par[x] == par[i]) lehet = false;
    }
    if (mn[x] > mx[x]) lehet = false;
}

void dfs2(int x) {
    if (p[x] == -1) p[x] = (p[f[x]] + 1 <= mx[x] ? p[f[x]] + 1 : p[f[x]] - 1);
    for (int i : g[x]) dfs2(i);
}

int main()
{
    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];
    dfs1(1);
    if (!lehet) { cout << "NEM"; return 0; }
    p[1] = (par[1] == -1 ? 0 : mn[1]);
    dfs2(1);
    cout << "IGEN\n";
    for (int i = 1; i <= n; i++) cout << p[i] << " ";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted7ms10180 KiB
2Accepted6ms10452 KiB
3Accepted71ms14428 KiB
subtask210/10
4Accepted64ms14160 KiB
5Accepted68ms14848 KiB
6Accepted75ms15408 KiB
7Accepted81ms15748 KiB
subtask315/15
8Accepted71ms35800 KiB
9Accepted68ms35632 KiB
10Accepted87ms36760 KiB
11Accepted82ms36076 KiB
12Accepted82ms35296 KiB
13Accepted83ms36244 KiB
14Accepted78ms34840 KiB
subtask435/35
15Accepted7ms12488 KiB
16Accepted6ms12616 KiB
17Accepted6ms12700 KiB
18Accepted6ms12836 KiB
19Accepted6ms12780 KiB
20Accepted6ms12780 KiB
21Accepted6ms12912 KiB
22Accepted6ms12900 KiB
subtask540/40
23Accepted74ms16548 KiB
24Accepted65ms16680 KiB
25Accepted67ms16680 KiB
26Accepted75ms16552 KiB
27Accepted75ms16548 KiB
28Accepted82ms16680 KiB
29Accepted82ms16700 KiB
30Accepted79ms16836 KiB
31Accepted76ms16816 KiB
32Accepted81ms16924 KiB
33Accepted86ms16924 KiB