4997 | 2023-04-08 16:43:44 | TomaSajt | Különböző katicák | cpp17 | Elfogadva 100/100 | 89ms | 36176 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] << ' ';
}
Részfeladat | Összpont | Teszt | Verdikt | Idő | Memória | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Elfogadva | 3ms | 1876 KiB | ||||
2 | Elfogadva | 3ms | 2120 KiB | ||||
3 | Elfogadva | 74ms | 13296 KiB | ||||
subtask2 | 10/10 | ||||||
4 | Elfogadva | 64ms | 12352 KiB | ||||
5 | Elfogadva | 70ms | 13660 KiB | ||||
6 | Elfogadva | 75ms | 14512 KiB | ||||
7 | Elfogadva | 82ms | 15760 KiB | ||||
subtask3 | 15/15 | ||||||
8 | Elfogadva | 71ms | 35360 KiB | ||||
9 | Elfogadva | 71ms | 35004 KiB | ||||
10 | Elfogadva | 89ms | 36176 KiB | ||||
11 | Elfogadva | 86ms | 35524 KiB | ||||
12 | Elfogadva | 82ms | 34716 KiB | ||||
13 | Elfogadva | 83ms | 35632 KiB | ||||
14 | Elfogadva | 79ms | 34428 KiB | ||||
subtask4 | 35/35 | ||||||
15 | Elfogadva | 3ms | 4092 KiB | ||||
16 | Elfogadva | 3ms | 4168 KiB | ||||
17 | Elfogadva | 3ms | 4352 KiB | ||||
18 | Elfogadva | 3ms | 4284 KiB | ||||
19 | Elfogadva | 3ms | 4540 KiB | ||||
20 | Elfogadva | 3ms | 4500 KiB | ||||
21 | Elfogadva | 3ms | 4492 KiB | ||||
22 | Elfogadva | 3ms | 4568 KiB | ||||
subtask5 | 40/40 | ||||||
23 | Elfogadva | 74ms | 15792 KiB | ||||
24 | Elfogadva | 65ms | 16312 KiB | ||||
25 | Elfogadva | 68ms | 16836 KiB | ||||
26 | Elfogadva | 75ms | 15952 KiB | ||||
27 | Elfogadva | 75ms | 15960 KiB | ||||
28 | Elfogadva | 81ms | 16928 KiB | ||||
29 | Elfogadva | 79ms | 16872 KiB | ||||
30 | Elfogadva | 78ms | 16624 KiB | ||||
31 | Elfogadva | 76ms | 16816 KiB | ||||
32 | Elfogadva | 79ms | 16748 KiB | ||||
33 | Elfogadva | 83ms | 17516 KiB |