#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=500005;
struct edge {
ll next,to;
double qz;
} e[maxn<<1];
ll n,m,s=1,tot=0,p,f[maxn][25],head[maxn];
queue<ll>q;
double dis[maxn],yc[maxn],d[maxn];
void add(ll x,ll y,double z) {
e[++tot].to=y;
e[tot].next=head[x];
head[x]=tot;
e[tot].qz=z;
}
void bfs() {
q.push(s);
d[s]=1;
dis[s]=1;
while(!q.empty()) {
ll top=q.front();
q.pop();
for(ll i=head[top]; i; i=e[i].next) {
ll t=e[i].to;
if(!d[t]) {
d[t]=d[top]+1;
dis[t]=dis[top]*e[i].qz;
f[t][0]=top;
for(ll j=1; j<=p; j++) f[t][j]=f[f[t][j-1]][j-1];
q.push(t);
}
}
}
}
ll lca(ll x,ll y) {
if(d[x]>d[y]) swap(x,y);//x<y
for(int i=p; i>=0; i--)
if(d[f[y][i]]>=d[x]) y=f[y][i];
if(x==y) return x;
for(ll i=p; i>=0; i--)
if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
return f[x][0];
}
int main() {
ll x,y;
double z;
scanf("%lld %lld",&n,&m);
p=(int)(log(n)/log(2))+1;
for(int i=1; i<=n; i++) cin>>yc[i];
for(ll i=1; i<n; i++) {
cin>>x>>y>>z;
add(x,y,z);
add(y,x,z);
}
bfs();
f[1][0]=1;
for(int i=1; i<=n; i++) cout<<d[i]<<" ";
cout<<endl;
for(int i=1; i<=n; i++) cout<<dis[i]<<" ";
cout<<endl;
for(ll i=1; i<=m; i++) {
cin>>x>>y;
double r=(1.0*yc[x]*dis[x]*dis[y]/dis[lca(x,y)])/dis[lca(x,y)];
if(r-(int)r==0) cout<<"Yes\n";
else cout<<"No";
}
return 0;
}/*
5 10
1 2 3 4 5
1 2 1
2 3 20
3 4 5
2 5 99
*/
写了写觉得思路没问题,但是过不去,求助!