#include "infection.h"
#include "bits/stdc++.h"

#define pii pair<int, int>
#define pb push_back
#define ar array
#define ll long long

const ll inf = 1e18 + 7;

using namespace std;

vector<int> solve(
    vector<pair<int, int>> R, vector<long long> D, vector<int> S, vector<int> T, vector<bool> I) {

    int n = R.size() + 1;

    vector<vector<int>> pon(n);

    for (auto [u, v]: R) {
        pon[u].pb(v);
        pon[v].pb(u);
    }

    int m = D.size();

    vector<vector<int>> paths(m);

    vector<ll> days;

    for (int i = 0; i < m; i++) {
        bool flag = 0;

        function<void(int, int, int)> dfs = [&](int u, int p, int f) {
            paths[i].pb(u);

            if (u == f) {
                flag = 1;

                return;
            }

            for (auto v: pon[u]) {
                if (v != p) {
                    dfs(v, u, f);
                }

                if (flag) {
                    return;
                }
            }

            if (!flag) {
                paths[i].pop_back();
            }
        };

        dfs(S[i], S[i], T[i]);

        for (ll j = D[i]; j < D[i] + paths[i].size(); j++) {
            days.pb(j);
        }
    }

    sort(days.begin(), days.end());
    days.erase(unique(days.begin(), days.end()), days.end());

    vector<ar<ll, 3>> verts;

    for (int i = 0; i < m; i++) {
        verts.pb({D[i], S[i], i});
    }

    vector<ll> a(m, inf);

    for (int i = 0; i < m; i++) {
        if (I[i]) {
            a[i] = D[i];
        }
    }

    sort(verts.rbegin(), verts.rend());

    for (auto d: days) {
        vector<ar<ll, 3>> ded;

        vector<int> used(n);

        while (verts.size() && verts.back()[0] <= d) {
            auto [curd, u, ind] = verts.back();

            verts.pop_back();

            if (a[ind] != inf) {
                used[u] = 1;
            }

            ded.pb({curd, u, ind});
        }

        for (auto [curd, u, ind]: ded) {
            if (used[u]) {
                a[ind] = min(a[ind], curd);
            }

            if (curd != d) {
                if (curd - D[ind] + 1 == paths[ind].size()) {
                    continue;
                }

                curd++, u = paths[ind][curd - D[ind]];
            }

            verts.pb({curd, u, ind});
        }
    }

    vector<int> ret;

    for (int i = 0; i < m; i++) {
        ret.pb(max(D[i] + (ll)paths[i].size() - a[i], 0ll));
    }

    return ret;
}

/*signed main() {
    int n, m;

    std::cin >> n;
    std::cin >> m;

    std::vector<std::pair<int, int>> edges = {};
    std::vector<long long> day;
    std::vector<int> start;
    std::vector<int> target;
    std::vector<bool> infected;

    for (int i = 0; i < n - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        edges.emplace_back(u, v);
    }

    for (int i = 0; i < m; i++) {
        long long _d, _s, _t, _i;
        std::cin >> _d >> _s >> _t >> _i;
        day.push_back(_d);
        start.push_back(_s);
        target.push_back(_t);
        infected.push_back(_i);
    }

    const auto ans = solve(edges, day, start, target, infected);
    for (const auto it : ans) {
        std::cout << it << std::endl;
    }

    return 0;
}*/


