屑题。。。。
调的比较久、、、、、
直接每次取重心,暴力检测答案就行了。。。
#include<bits/stdc++.h>
#define MAXN 80005
typedef long long ll;
using namespace std;
int n,m,k,h[MAXN],tot,vis[MAXN],sz[MAXN],zhong[MAXN],maxl,ask[MAXN],ans[MAXN];
int dis[MAXN],js,dui[MAXN],in[40000005],jjs;
struct node{
int from,to,nex,cost;
}e[MAXN << 1];
void add(int x , int y , int z){
tot++;
e[tot].from = x;
e[tot].to = y;
e[tot].cost = z;
e[tot].nex = h[x];
h[x] = tot;
return;
}
void getsz(int now , int fa){
sz[now] = 1;
for(int i = h[now] ; i != (-1) ; i = e[i].nex){
if(e[i].to == fa || vis[e[i].to])continue;
getsz(e[i].to , now);
sz[now] += sz[e[i].to];
}
return;
}
void getzx(int now , int fa , int yl){
zhong[now] = 0;
for(int i = h[now] ; i != (-1) ; i = e[i].nex){
if(e[i].to == fa || vis[e[i].to])continue;
getzx(e[i].to , now , yl);
if(sz[zhong[now]] < sz[e[i].to])zhong[now] = e[i].to;
}
zhong[now] = max(sz[zhong[now]] , sz[yl] - sz[zhong[now]]);
if(zhong[now] < zhong[maxl])maxl = now;
return;
}
void getdis(int now , int fa){
for(int i = h[now] ; i != (-1) ; i = e[i].nex){
if(e[i].to == fa || vis[e[i].to])continue;
dis[e[i].to] = dis[now] + e[i].cost;
getdis(e[i].to , now);
}
dui[++js] = dis[now];
return;
}
void calc(int now){
js = 0;
for(int i = h[now] ; i != (-1) ; i = e[i].nex){
if(vis[e[i].to])continue;
dis[e[i].to] = e[i].cost , jjs = js;
dui[++js] = dis[e[i].to];
getdis(e[i].to , now);
//ÏȶÔÓÚÿ¸öѯÎÊ
for(int i = 1 ; i <= m ; i++){
for(int j = jjs + 1 ; j <= js ; j++){
if(ask[i] < dui[j])continue;
if(in[ask[i] - dui[j]])ans[i] = 1;
}
}
for(int i = jjs ; i <= js ; i++)if(dui[i] <= 10000005)in[dui[i]] = 1;
}
for(int i = 1 ; i <= js ; i++)in[dui[i]] = 0;
return;
}
void solve(int now){
vis[now] = 1;
calc(now);getsz(now , now);
for(int i = h[maxl] ; i != (-1) ; i = e[i].nex){
if(vis[e[i].to])continue;
maxl = 0;getzx(e[i].to , now , e[i].to);
solve(maxl);
}
return;
}
void init(){
memset(ans , -1 , sizeof(ans));
memset(in, 0 , sizeof(in));
memset(h , -1 , sizeof(h));
cin>>n>>m;
int x,y,z;tot = 0 , sz[n + 1] = n;
zhong[0] = 9999999;in[0] = 1;
for(int i = 1 ; i < n ; i++){
cin>>x>>y>>z;
add(x , y , z);
add(y , x , z);
}
for(int i = 1 ; i <= m ; i++)cin>>ask[i];
maxl = 0;getzx(1 , 1 , n + 1);
solve(maxl);
for(int i = 1 ; i <= m ; i++){
if(ans[i] == 1)cout<<"AYE"<<endl;
else cout<<"NAY"<<endl;
}
}
int main(){
init();
}