#include "infection.h"
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define sc second
#define ll long long
#define pb push_back
#define all(x) (x).begin(),(x).end()

const int maxN = 2 * 1e5;

vector<ll> d;
vector<bool> f;
vector<int> g[maxN], s, t;
int n, m, par[maxN][20], dep[maxN], in[maxN], out[maxN], tim;
void dfs(int v, int p) {
    in[v] = ++tim;
    par[v][0] = p; dep[v] = dep[p] + 1;
    for (int u : g[v]) {
        if (u == p) continue;
        dfs(u, v);
    }

    out[v] = ++tim;
}

bool is_anc(int x, int y) {
    return (in[x] <= in[y] && out[x] >= out[y]);
}
int lca(int x, int y) {
    if (is_anc(x, y)) return x;
    if (is_anc(y, x)) return y;
    for (int j = 19; j >= 0; j--) {
        if (!is_anc(par[x][j], y)) x = par[x][j];
    } return par[x][0];
}
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> _f
) {

    for (auto i : r) {
        g[i.ff].pb(i.sc);
        g[i.sc].pb(i.ff);
    }

    d = _d; s = _s; t = _t; f = _f;
    n = (int)r.size() + 1; m = (int)d.size();

    dfs(0, 0);
    for (int j = 1; j < 20; j++) {
        for (int i = 0; i < n; i++) {
            par[i][j] = par[par[i][j - 1]][j - 1];
        }
    }

    vector<int> ans(m, 0);
    for (int i = 0; i < m; i++) {
        if (f[i] == 0) continue;
        int lc = lca(s[i], t[i]);
        ans[i] = dep[s[i]] + dep[t[i]] - 2 * dep[lc] + 1;
    }

    return ans;
}


// 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;
// }
