5289 | 2023-04-25 12:59:22 | ZsofiaKeresztely | Különböző katicák | cpp14 | Accepted 100/100 | 86ms | 33796 KiB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define fi first
#define se second
vector<pii> iv;
vector<int> p, c, d;
vector<int> odd;
vector<vector<int> > g;
bool op = true;
void dfs1(int v){
for (int x : g[v]){
d[x] = d[v] + 1;
dfs1(x);
if (!op) return;
iv[v].fi = max(iv[v].fi, iv[x].fi - 1);
iv[v].se = min(iv[v].se, iv[x].se + 1);
if (odd[v] == odd[x] && odd[v] >= 0 || iv[v].se < iv[v].fi){
op = false;
return;
}
if (odd[x] >=0) odd[v] = 1 - odd[x];
}
}
void dfs2(int v){
iv[v].fi = max(iv[v].fi, iv[p[v]].fi - 1);
iv[v].se = min(iv[v].se, iv[p[v]].se + 1);
if (odd[v] == odd[p[v]] && odd[v] >= 0 || iv[v].se < iv[v].fi){
op = false;
return;
}
if (odd[p[v]] >=0) odd[v] = 1 - odd[p[v]];
for (int x : g[v]){
dfs2(x);
}
}
int main()
{
//freopen("be3.txt", "r", stdin);
int n;
cin >> n;
g.resize(n+1);
p.resize(n+1);
d.resize(n+1);
c.resize(n+1);
odd.assign(n+1, -1);
iv.assign(n+1, {0, 1e9});
for (int i=1; i<=n; i++){
cin >> p[i];
g[p[i]].push_back(i);
}
for (int i=1; i<=n; i++){
cin >> c[i];
if (c[i] >= 0){
iv[i] = {c[i], c[i]};
odd[i] = c[i] % 2;
}
}
d[1] = 0;
dfs1(1);
if (!op){
cout << "NEM";
return 0;
}
dfs2(1);
if (!op){
cout << "NEM";
return 0;
}
cout << "IGEN\n";
if (iv[1].se == 1e9){
for (int i=1; i<=n; i++){
cout << d[i] << " ";
}
}
else{
for (int i=1; i<=n; i++){
if (!iv[i].fi && odd[i]) cout << 1 << " ";
else cout << iv[i].fi << " ";
}
}
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1876 KiB | ||||
2 | Accepted | 3ms | 2120 KiB | ||||
3 | Accepted | 74ms | 14156 KiB | ||||
subtask2 | 10/10 | ||||||
4 | Accepted | 63ms | 12924 KiB | ||||
5 | Accepted | 70ms | 13800 KiB | ||||
6 | Accepted | 75ms | 15100 KiB | ||||
7 | Accepted | 82ms | 16356 KiB | ||||
subtask3 | 15/15 | ||||||
8 | Accepted | 70ms | 32944 KiB | ||||
9 | Accepted | 71ms | 32548 KiB | ||||
10 | Accepted | 86ms | 33796 KiB | ||||
11 | Accepted | 83ms | 32960 KiB | ||||
12 | Accepted | 82ms | 32312 KiB | ||||
13 | Accepted | 85ms | 33256 KiB | ||||
14 | Accepted | 78ms | 32048 KiB | ||||
subtask4 | 35/35 | ||||||
15 | Accepted | 3ms | 4000 KiB | ||||
16 | Accepted | 3ms | 4048 KiB | ||||
17 | Accepted | 3ms | 3916 KiB | ||||
18 | Accepted | 3ms | 3916 KiB | ||||
19 | Accepted | 3ms | 3912 KiB | ||||
20 | Accepted | 3ms | 4168 KiB | ||||
21 | Accepted | 3ms | 4376 KiB | ||||
22 | Accepted | 3ms | 4336 KiB | ||||
subtask5 | 40/40 | ||||||
23 | Accepted | 75ms | 16324 KiB | ||||
24 | Accepted | 65ms | 17388 KiB | ||||
25 | Accepted | 65ms | 17460 KiB | ||||
26 | Accepted | 75ms | 16688 KiB | ||||
27 | Accepted | 75ms | 16420 KiB | ||||
28 | Accepted | 82ms | 17460 KiB | ||||
29 | Accepted | 81ms | 17700 KiB | ||||
30 | Accepted | 78ms | 17128 KiB | ||||
31 | Accepted | 79ms | 17128 KiB | ||||
32 | Accepted | 78ms | 17384 KiB | ||||
33 | Accepted | 86ms | 18376 KiB |