Source Code
#include <bits/stdc++.h>
using namespace std;

/********************* ordered set imp.********************************/
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
/********************* macros and typedefs *****************************/
#define all(v) (v.begin()), (v.end())
#define sor(x) sort(all(x))
#define fl(v, d) (memset(v, d, sizeof(v)))
typedef long long ll;
typedef vector<int> vii;
typedef pair<int, int> pii;
void take(vii &v, int n)
{
    v.resize(n);
    for (int i = 0; i < n; i++)
        cin >> v[i];
}
/********************* debuging template *******************************/
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T &x)
{
    int f = 0;
    cerr << '{';
    for (auto &i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define de(x...)                  \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define de(x...)
#endif
/********************* actual sol.***********************************/

#define MOD ll(1e9 + 7)
#define N (const ll)1e5 + 1

inline void solve()
{
    int n;
    cin >> n;
    vii m(n + 1), cost(n + 1);
    vector<pair<int, int>> ans(n + 1);
    for (int i = 2; i <= n; i++)
    {
        int x;
        cin >> x;
        m[i] = x;
    }
    for (int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        cost[i] = x;
        ans[i] = {INT_MAX, 0};
    }
    function<int(int)> getpar = [&](int child) -> int
    {
        int cur = child;
        for (int i = 1; i <= 4; i++)
        {
            cur = m[cur];
            if (i == 1 || i == 2 || i == 3)
            {
                if (cur == 1)
                    return -1;
            }
        }
        return cur;
    };

    for (int i = n; i >= 2; i--)
    {
        int to = getpar(i);
        if (to == -1)
            continue;

        int cur = ans[to].first;
        int nw = abs(cost[i] - cost[to]);
        if (cur > nw)
            ans[to] = {nw, i};
        else if (cur == nw)
            ans[to] = {nw, min(ans[to].second, i)};
    }

    for (int i = 1; i <= n; i++)
        cout << ans[i].second << " ";
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    // cin >> t;
    while (t--)
        solve();
}
Copy
Find a Friend Yazn_yousef
GNU G++17
47 ms
5.3 MB
Accepted