49972023-04-08 16:43:44TomaSajtKülönböző katicákcpp17Accepted 100/10089ms36176 KiB
#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> subordinates;
vector<int> boss, spots, parity, range_start, range_end;
bool impossible = false;

void range_dfs(int u) {
  for (int v : subordinates[u]) {
    range_dfs(v);
    range_start[u] = max(range_start[u], range_start[v] - 1);
    range_end[u] = min(range_end[u], range_end[v] + 1);
    if (parity[u] == -1 && parity[v] != -1)
      parity[u] = !parity[v];
    if (parity[u] != -1 && parity[u] == parity[v])
      impossible = true;
  }
  if (range_start[u] > range_end[u])
    impossible = true;
}

void fill_dfs(int u) {
  if (spots[u] == -1) {
    int bs = spots[boss[u]];
    spots[u] = bs + 1 <= range_end[u] ? bs + 1 : bs - 1;
  }
  for (int v : subordinates[u])
    fill_dfs(v);
}

int main() {
  int n;
  cin >> n;
  subordinates.resize(n + 1);
  boss.resize(n + 1);
  spots.resize(n + 1);
  parity.resize(n + 1, -1);
  range_start.resize(n + 1, INT_MIN / 2);
  range_end.resize(n + 1, INT_MAX / 2);
  for (int i = 1; i <= n; i++) {
    cin >> boss[i];
    subordinates[boss[i]].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    cin >> spots[i];
    if (spots[i] != -1) {
      parity[i] = spots[i] % 2;
      range_start[i] = range_end[i] = spots[i];
    }
  }
  range_dfs(1);
  if (impossible) {
    cout << "NEM";
    return 0;
  }
  fill_dfs(1);
  cout << "IGEN\n";
  for (int i = 1; i <= n; i++)
    cout << spots[i] << ' ';
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1876 KiB
2Accepted3ms2120 KiB
3Accepted74ms13296 KiB
subtask210/10
4Accepted64ms12352 KiB
5Accepted70ms13660 KiB
6Accepted75ms14512 KiB
7Accepted82ms15760 KiB
subtask315/15
8Accepted71ms35360 KiB
9Accepted71ms35004 KiB
10Accepted89ms36176 KiB
11Accepted86ms35524 KiB
12Accepted82ms34716 KiB
13Accepted83ms35632 KiB
14Accepted79ms34428 KiB
subtask435/35
15Accepted3ms4092 KiB
16Accepted3ms4168 KiB
17Accepted3ms4352 KiB
18Accepted3ms4284 KiB
19Accepted3ms4540 KiB
20Accepted3ms4500 KiB
21Accepted3ms4492 KiB
22Accepted3ms4568 KiB
subtask540/40
23Accepted74ms15792 KiB
24Accepted65ms16312 KiB
25Accepted68ms16836 KiB
26Accepted75ms15952 KiB
27Accepted75ms15960 KiB
28Accepted81ms16928 KiB
29Accepted79ms16872 KiB
30Accepted78ms16624 KiB
31Accepted76ms16816 KiB
32Accepted79ms16748 KiB
33Accepted83ms17516 KiB