树剖WA 0pts
自己搓了好几组数据,能过,但交上去还是wa
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
int x=0,f=0;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) f|=(ch=='-');
for(;isdigit(ch);ch=getchar()) x=(x<<1)+(x<<3)+(ch^48);
return f?-x:x;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(x>9) print(x/10);
putchar(x%10+48);
}
int tot,dfn[10233111],pre[10231311],top[10213131],fa[10231311],size[10231311],son[10231311],Dep[10231311];
int n,m,x,y,head[10231231],cnt;
double z,a[10231121],A[1023133],ans;
struct node{
int next,to;
double w;
}e[10231311];
void add(int u,int v,double w) {
e[++cnt].next=head[u];
e[cnt].to=v;
e[cnt].w=w;
head[u]=cnt;
}
namespace ss{
#define lson pos<<1
#define rson pos<<1|1
struct tt{
double sum;
}tree[10233121];
void build(int pos,int l,int r) {
if (l==r) {
tree[pos].sum=a[pre[l]];
return ;
}
int mid=l+r>>1;
build(lson,l,mid); build(rson,mid+1,r);
tree[pos].sum=tree[lson].sum*tree[rson].sum;
}
double query(int pos,int l,int r,int L,int R) {
if (l>=L && r<=R) return tree[pos].sum;
int mid=l+r>>1;
double res=1;
if (L<=mid) res*=query(lson,l,mid,L,R);
if (R>mid) res*=query(rson,mid+1,r,L,R);
return res;
}
}
namespace sp{
void dfs1(int now,int Fa) {
Dep[now]=Dep[Fa]+1;fa[now]=Fa;size[now]=1;
for (int i=head[now];i;i=e[i].next) {
if (e[i].to==Fa) continue;
a[e[i].to]=e[i].w;
dfs1(e[i].to,now);
size[now]+=size[e[i].to];
if (size[son[now]]<size[e[i].to]) son[now]=e[i].to;
}
}
void dfs2(int now,int Top) {
top[now]=Top; dfn[now]=++tot; pre[tot]=now;
if (son[now]) dfs2(son[now],Top);
for (int i=head[now];i;i=e[i].next) {
if (e[i].to==fa[now]||e[i].to==son[now]) continue;
dfs2(e[i].to,e[i].to);
}
}
double query(int x,int y) {
double res=1;
while(top[x]^top[y]) {
if (Dep[top[x]]<Dep[top[y]]) swap(x,y);
res*=ss::query(1,1,n,dfn[top[x]],dfn[x]);
x=fa[top[x]];
}
if (Dep[x]>Dep[y]) swap(x,y);
res*=ss::query(1,1,n,dfn[x]+1,dfn[y]);
return res;
}
}
signed main() {
cin>>n>>m;
for (int i=1;i<=n;++i) {
cin>>A[i];
}
for (int i=1;i<n;++i){
cin>>x>>y>>z;
add(x,y,z); add(y,x,z);
}
sp::dfs1(1,0); sp::dfs2(1,1);
ss::build(1,1,n);
for (int i=1;i<=m;++i) {
cin>>x>>y;
ans=sp::query(x,y)*A[x];
if (ans-(int)ans==0) cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}