39142023-03-05 12:59:41zsomborKülönböző katicákcpp17Wrong answer 0/10090ms36468 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 >= mn[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
2Accepted6ms10388 KiB
3Wrong answer71ms14356 KiB
subtask20/10
4Wrong answer64ms14032 KiB
5Wrong answer71ms14508 KiB
6Wrong answer75ms15208 KiB
7Wrong answer82ms15544 KiB
subtask30/15
8Accepted72ms35636 KiB
9Accepted71ms35324 KiB
10Wrong answer90ms36468 KiB
11Wrong answer85ms35928 KiB
12Accepted86ms35072 KiB
13Accepted86ms35900 KiB
14Wrong answer81ms34620 KiB
subtask40/35
15Wrong answer7ms12416 KiB
16Accepted7ms12248 KiB
17Accepted6ms12380 KiB
18Wrong answer7ms12364 KiB
19Accepted6ms12508 KiB
20Wrong answer7ms12632 KiB
21Wrong answer6ms12756 KiB
22Accepted6ms12800 KiB
subtask50/40
23Wrong answer74ms16716 KiB
24Accepted65ms16992 KiB
25Accepted67ms16984 KiB
26Wrong answer75ms16804 KiB
27Wrong answer75ms16800 KiB
28Accepted82ms16912 KiB
29Wrong answer79ms16912 KiB
30Wrong answer75ms16952 KiB
31Wrong answer78ms16716 KiB
32Wrong answer79ms16808 KiB
33Wrong answer82ms17068 KiB