77922024-01-11 10:17:31gortomiKülönböző katicákcpp17Wrong answer 65/10052ms40888 KiB
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g;
vector<int> l, r, p, val, a;
void dfs(int v)
{
    for(auto x : g[v])
    {
        dfs(x);
        if(p[x] != -1) p[v] = 1 - p[x];
        l[v] = max(l[v], l[x] - 1);
        r[v] = min(r[v], r[x] + 1);
    }
    if(p[v] == -1 && a[v] != -1) p[v] = a[v] % 2;
    if(a[v] != -1 && p[v] != (a[v] % 2))
    {
        cout << "NEM\n";
        exit(0);
    }
}
void calc(int v)
{
    for(auto x : g[v])
    {
        if(val[v] - 1 < l[x]) val[x] = val[v] + 1;
        else val[x] = val[v] - 1;
        calc(x);
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    g.resize(n + 1);
    l.resize(n + 1, 0);
    r.resize(n + 1, 1e7);
    a.resize(n + 1);
    p.resize(n + 1, -1);
    val.resize(n + 1);
    for(int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        g[x].push_back(i);
    }
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if(a[i] != -1) l[i] = r[i] = a[i];
    }
    dfs(1);
    for(int i = 1; i <= n; i++)
    {
        if(l[i] > r[i])
        {
            cout << "NEM\n";
            return 0;
        }
    }
    val[1] = l[1];
    calc(1);
    cout << "IGEN\n";
    for(int i = 1; i <= n; i++) cout << val[i] << " ";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1892 KiB
2Accepted3ms2120 KiB
3Accepted41ms14248 KiB
subtask210/10
4Accepted35ms13688 KiB
5Accepted39ms15688 KiB
6Accepted43ms17228 KiB
7Accepted46ms19152 KiB
subtask315/15
8Accepted39ms36548 KiB
9Accepted39ms37184 KiB
10Accepted52ms39064 KiB
11Accepted50ms39012 KiB
12Accepted48ms39204 KiB
13Accepted50ms40888 KiB
14Accepted46ms40488 KiB
subtask40/35
15Accepted3ms13016 KiB
16Wrong answer3ms12972 KiB
17Accepted3ms13128 KiB
18Accepted3ms13084 KiB
19Accepted3ms13352 KiB
20Accepted3ms13308 KiB
21Accepted3ms13316 KiB
22Accepted3ms13576 KiB
subtask540/40
23Accepted43ms25996 KiB
24Accepted34ms27716 KiB
25Accepted35ms28928 KiB
26Accepted43ms29016 KiB
27Accepted45ms29688 KiB
28Accepted46ms31440 KiB
29Accepted48ms32580 KiB
30Accepted45ms32904 KiB
31Accepted43ms33652 KiB
32Accepted45ms34528 KiB
33Accepted48ms36160 KiB