namespace ST { int mx[N][20]; voidinit(); intquery(int l, int r); } namespace HLD { int fa[N], dep[N], siz[N]; int big[N], top[N]; ll dis[N]; int dfn[N], rk[N], dft; voiddfs1(int x, int dad); voiddfs2(int x, int tp); intlca(int x, int y); ll dist(int x, int y); intmaxc(int x, int y); boolcheck(int x, ll w); pair<int,int> path(int a, int b, int c, int d); } usingnamespace HLD;
structedge {int to, cost; };
int n, q; vector<edge> ver[N];
signedmain() { cin >> n >> q; for(int i = 1; i < n; i++) { int a, b, w; cin >> a >> b >> w; ver[a].push_back({b, w}); ver[b].push_back({a, w}); } HLD::dfs1(1, 0); HLD::dfs2(1, 1); ST::init(); dis[0] = -1e18; for(int i = 1; i <= q; i++) { int u1, v1, t1, u2, v2, t2; cin >> u1 >> v1 >> t1 >> u2 >> v2 >> t2; auto p = HLD::path(u1, v1, u2, v2); int f = lca(p.first, p.second); if(p.first == -1) { cout << "NO\n"; continue; } ll d1 = t1+HLD::dist(u1, p.first); ll d2 = t1+HLD::dist(u1, p.second); ll d3 = t2+HLD::dist(u2, p.first); ll d4 = t2+HLD::dist(u2, p.second); if((d1 <= d2) != (d3 <= d4)) { if((d1 <= d3 and d2 >= d4) or (d1 >= d3 and d2 <= d4)) { ll len = dist(p.first,p.second)+min(d3,d4)-min(d1,d2); if(len%2 == 1) cout << "YES\n"; else { len /= 2; int st; if(d1 <= d2) { if(len <= dist(p.first, f)) st = p.first; else st = p.second, len = dist(p.first, p.second)-len; } else { if(len <= dist(p.second, f)) st = p.second; else st = p.first, len = dist(p.first, p.second)-len; } cout << (HLD::check(st, len)?"NO\n":"YES\n"); } } else cout << "NO\n"; } else { if(abs(min(d1,d2)-min(d3,d4)) < HLD::maxc(p.first, p.second)) cout << "YES\n"; else cout << "NO\n"; } } }
namespace HLD { voiddfs1(int x, int dad) { siz[x] = 1; fa[x] = dad; dep[x] = dep[dad]+1; for(auto i : ver[x]) { if(i.to == dad) continue; dis[i.to] = dis[x]+i.cost; dfs1(i.to, x); siz[x] += siz[i.to]; if(siz[i.to] > siz[big[x]]) big[x] = i.to; } } voiddfs2(int x, int tp) { top[x] = tp; dfn[x] = ++dft; rk[dft] = x; if(big[x]) dfs2(big[x], tp); for(auto i : ver[x]) if(i.to != big[x] and i.to != fa[x]) dfs2(i.to, i.to); for(auto i : ver[x]) ST::mx[dfn[i.to]][0] = i.cost; } intlca(int x, int y) { while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x, y); x = fa[top[x]]; } return dep[x] < dep[y] ? x : y; } ll dist(int x, int y) { return dis[x]+dis[y]-2*dis[lca(x,y)]; } intmaxc(int x, int y) { int ans = 0; while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x, y); ans = max(ans, ST::query(dfn[top[x]], dfn[x])); x = fa[top[x]]; } if(dep[x] > dep[y]) swap(x, y); if(x != y) ans = max(ans, ST::query(dfn[x]+1, dfn[y])); return ans; } pair<int,int> path(int a, int b, int c, int d) { int p[] = {lca(a,c), lca(a,d), lca(b,c), lca(b,d)}; sort(p, p+4, [&](int a, int b){ return dep[a] > dep[b]; }); if(p[0] != p[1]) return {p[0], p[1]}; else return {-1, -1}; } boolcheck(int x, ll w) { while(dis[x]-dis[fa[top[x]]] <= w) { w -= dis[x]-dis[fa[top[x]]]; x = fa[top[x]]; } int l = dfn[top[x]], r = dfn[x]; while(l < r) { int mid = (l+r)>>1; if(dis[x]-dis[rk[mid]] < w) l = mid+1; else r = mid; } return dis[x]-dis[rk[l]] == w; }
}
namespace ST { voidinit() { for(int k = 1; k < 20; k++) for(int i = 1; i+(1<<k)-1 <= n; i++) mx[i][k] = max(mx[i][k-1], mx[i+(1<<(k-1))][k-1]); } intquery(int l, int r) { int k = log2(r-l+1); returnmax(mx[l][k], mx[r-(1<<k)+1][k]); } }