49962023-04-08 16:41:58TomaSajtKülönböző katicákcpp17Wrong answer 10/10087ms35888 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);
  cout << "IGEN\n";
  for (int i = 1; i <= n; i++)
    cout << spots[i] << ' ';
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1872 KiB
2Accepted3ms2068 KiB
3Accepted74ms13240 KiB
subtask210/10
4Accepted64ms12216 KiB
5Accepted70ms13468 KiB
6Accepted75ms14440 KiB
7Accepted82ms15696 KiB
subtask30/15
8Accepted74ms34992 KiB
9Wrong answer83ms34892 KiB
10Accepted87ms35888 KiB
11Accepted86ms35328 KiB
12Accepted85ms34628 KiB
13Accepted86ms35636 KiB
14Accepted79ms34324 KiB
subtask40/35
15Accepted3ms4268 KiB
16Accepted3ms4224 KiB
17Wrong answer3ms4476 KiB
18Accepted3ms4432 KiB
19Accepted3ms4432 KiB
20Accepted3ms4428 KiB
21Accepted3ms4428 KiB
22Accepted3ms4444 KiB
subtask50/40
23Accepted75ms15736 KiB
24Accepted67ms16256 KiB
25Wrong answer85ms16776 KiB
26Accepted75ms15736 KiB
27Accepted75ms15992 KiB
28Accepted83ms16880 KiB
29Accepted82ms16932 KiB
30Accepted79ms16432 KiB
31Accepted76ms16412 KiB
32Accepted79ms16412 KiB
33Accepted86ms17068 KiB