#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
std::vector<int> solve(
std::vector<std::pair<int, int>> R,
std::vector<long long> D,
std::vector<int> S,
std::vector<int> T,
std::vector<bool> I
) {
    for (auto &[i, j] : R) i++, j++;
    for (auto &i : S) i++;
    for (auto &i : T) i++;
    int n = R.size() + 1;
    vector<int> g[n + 1];
    for (auto [i, j] : R) {
        g[i].push_back(j);
        g[j].push_back(i);
    }
    vector<int> tin(n + 1), tout(n + 1);
    int timer = 0;
    vector<vector<int>> up(20, vector<int>(n + 1));
    function<void(int, int)> dfs = [&](int v, int p) {
        tin[v] = timer++;
        up[0][v] = p;
        for (int lg = 1; lg < 20; ++lg) {
            up[lg][v] = up[lg - 1][up[lg - 1][v]];
        }
        for (auto to : g[v]) {
            if (to == p) continue;
            dfs(to, v);
        }
        tout[v] = timer++;
        return;
    };
    dfs(1, 0);
    auto parent = [&](int u, int v) {
        return tin[u] <= tin[v] && tout[v] <= tout[u];
    };
    auto lca = [&](int u, int v) {
        if (parent(u, v)) return u;
        if (parent(v, u)) return v;
        for (int lg = 19; lg >= 0; --lg) {
            if (up[lg][u] != 0 && parent(up[lg][u], v) == false) {
                u = up[lg][u];
            }
        }
        return up[0][u];
    };
    cc_hash_table<int, vector<pair<int, int>>> mp;
    int m = S.size();
    vector<int> ans(m);
    for (int i = 0; i < m; ++i) {
        int st = S[i], fn = T[i];
        //cout << "!" << st << ' '<< fn << '\n';
        int l = lca(st, fn);
        vector<int> way, tmp;
        while (true) {
            way.push_back(st);
           // cout << "!" << ' ' << st << ' '<< l<< endl;;
            if (st == l) break;
            st = up[0][st];
        }
        while (true) {
            if (fn == l) break;
            tmp.push_back(fn);
            fn = up[0][fn];
        }
        reverse(tmp.begin(), tmp.end());
        for (auto z : tmp) way.push_back(z);

        for (int j = 0; j < way.size(); ++j) {
            mp[j + D[i]].push_back({way[j], i});
        }
        /*for (auto i : way) cout << i << ' ';
        cout << '\n';*/
    }
    cc_hash_table<int, bool> mapa;
    for (auto [key, vec] : mp) {
        int sum = 0;
        mapa.clear();
        for (auto [ver, idx] : vec) {
            mapa[ver] = mapa[ver] | I[idx];
        }
        for (auto [ver, idx] : vec) {
            if (mapa[ver]) I[idx] = true;
            ans[idx] += I[idx];
        }
    }
    return ans;
}
