4995 | 2023-04-08 16:41:19 | TomaSajt | Különböző katicák | cpp17 | Wrong answer 0/100 | 89ms | 35920 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] << ' ';
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Wrong answer | 3ms | 1872 KiB | ||||
2 | Accepted | 3ms | 2120 KiB | ||||
3 | Wrong answer | 72ms | 13336 KiB | ||||
subtask2 | 0/10 | ||||||
4 | Wrong answer | 63ms | 12264 KiB | ||||
5 | Wrong answer | 68ms | 13536 KiB | ||||
6 | Wrong answer | 75ms | 14520 KiB | ||||
7 | Wrong answer | 81ms | 15636 KiB | ||||
subtask3 | 0/15 | ||||||
8 | Accepted | 68ms | 34892 KiB | ||||
9 | Wrong answer | 81ms | 34792 KiB | ||||
10 | Wrong answer | 89ms | 35920 KiB | ||||
11 | Wrong answer | 82ms | 35268 KiB | ||||
12 | Wrong answer | 79ms | 34400 KiB | ||||
13 | Wrong answer | 82ms | 35312 KiB | ||||
14 | Wrong answer | 81ms | 34012 KiB | ||||
subtask4 | 0/35 | ||||||
15 | Wrong answer | 3ms | 3780 KiB | ||||
16 | Accepted | 3ms | 4092 KiB | ||||
17 | Wrong answer | 3ms | 4308 KiB | ||||
18 | Wrong answer | 3ms | 4432 KiB | ||||
19 | Wrong answer | 3ms | 4432 KiB | ||||
20 | Wrong answer | 3ms | 4688 KiB | ||||
21 | Wrong answer | 3ms | 4644 KiB | ||||
22 | Wrong answer | 3ms | 4644 KiB | ||||
subtask5 | 0/40 | ||||||
23 | Wrong answer | 75ms | 16248 KiB | ||||
24 | Accepted | 64ms | 16628 KiB | ||||
25 | Wrong answer | 81ms | 17092 KiB | ||||
26 | Wrong answer | 74ms | 16048 KiB | ||||
27 | Wrong answer | 72ms | 16060 KiB | ||||
28 | Wrong answer | 79ms | 16900 KiB | ||||
29 | Wrong answer | 78ms | 17248 KiB | ||||
30 | Wrong answer | 79ms | 16856 KiB | ||||
31 | Wrong answer | 75ms | 16764 KiB | ||||
32 | Wrong answer | 75ms | 16832 KiB | ||||
33 | Wrong answer | 83ms | 17664 KiB |