49952023-04-08 16:41:19TomaSajtKülönböző katicákcpp17Wrong answer 0/10089ms35920 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] = max(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);
  for (int i = 1; i <= n; i++)
    cout << spots[i] << ' ';
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Wrong answer3ms1872 KiB
2Accepted3ms2120 KiB
3Wrong answer72ms13336 KiB
subtask20/10
4Wrong answer63ms12264 KiB
5Wrong answer68ms13536 KiB
6Wrong answer75ms14520 KiB
7Wrong answer81ms15636 KiB
subtask30/15
8Accepted68ms34892 KiB
9Wrong answer81ms34792 KiB
10Wrong answer89ms35920 KiB
11Wrong answer82ms35268 KiB
12Wrong answer79ms34400 KiB
13Wrong answer82ms35312 KiB
14Wrong answer81ms34012 KiB
subtask40/35
15Wrong answer3ms3780 KiB
16Accepted3ms4092 KiB
17Wrong answer3ms4308 KiB
18Wrong answer3ms4432 KiB
19Wrong answer3ms4432 KiB
20Wrong answer3ms4688 KiB
21Wrong answer3ms4644 KiB
22Wrong answer3ms4644 KiB
subtask50/40
23Wrong answer75ms16248 KiB
24Accepted64ms16628 KiB
25Wrong answer81ms17092 KiB
26Wrong answer74ms16048 KiB
27Wrong answer72ms16060 KiB
28Wrong answer79ms16900 KiB
29Wrong answer78ms17248 KiB
30Wrong answer79ms16856 KiB
31Wrong answer75ms16764 KiB
32Wrong answer75ms16832 KiB
33Wrong answer83ms17664 KiB