#include <iostream>
#include <cstring>
using namespace std;
const int N = 2e5 + 5, M = 4e5 + 5;
int n, m, a, b, h[N], e[M], ne[M], idx, root = 1, dep[N], fa[N][19], die;
double d[N], dist[N], w[M], c, ans;
bool st[N];
void add(int a, int b, double c)
{
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
void dfs(int u, double val)
{
dist[u] = val, st[u] = true;
for (int i = h[u]; ~i; i = ne[i])
if (!st[e[i]])
dfs(e[i], val * w[i]);
}
void dfs_lca(int u, int father)
{
dep[u] = dep[father] + 1, fa[u][0] = father;
for (int i = 1; i <= 18; i++)
fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int i = h[u]; ~i; i = ne[i])
if (e[i] != father)
dfs_lca(e[i], u);
}
int lca(int u, int v)
{
if (dep[u] < dep[v])
swap(u, v);
for (int i = 18; i >= 0; i--)
if (dep[fa[u][i]] >= dep[v])
u = fa[u][i];
if (u == v)
return v;
for (int i = 18; i >= 0; i--)
if (fa[u][i] != fa[v][i])
u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m, memset(h, -1, sizeof(h));
for (int i = 1; i <= n; i++)
cin >> d[i];
for (int i = 1; i < n; i++)
cin >> a >> b >> c, add(a, b, c), add(b, a, c), st[b] = true;
while (st[root])
root++;
memset(st, false, sizeof(st)), dfs(root, 1), dfs_lca(root, 0);
while (m--)
{
cin >> a >> b, die = lca(a, b);
ans = dist[a] * dist[b] * d[a] / dist[die] / dist[die];
if (ans - (int)ans < 1e-8)
cout << "Yes\n";
else
cout << "No\n";
}
}
输出了一些信息,看上去样例是没问题的...