树上淀粉质点分治
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
const int N = 1e5 + 10;
const int M = 1e7 + 1000;
const int mod = 11092019;
const int inf = 0x7f7f7f7f;
const int eps = 1e-8;
ll read()
{
ll x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while ('0' <= c && c <= '9')
{
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
ll n, m, tot, root, sumsiz;
ll head[N], to[N << 1], nex[N << 1], val[N << 1], que[110], maxsiz[N], siz[N], dis[N], notedis[N], notejud[N];
bool jud[M], ans[110], vis[N];
void addedge(ll u, ll v, ll w)
{
to[++tot] = v;
val[tot] = w;
nex[tot] = head[u];
head[u] = tot;
}
void getroot(ll rt, ll fa)
{
siz[rt] = 1, maxsiz[rt] = 0;
for (int i = head[rt]; i; i = nex[i])
{
if (vis[to[i]] || to[i] == fa)
continue;
getroot(to[i], rt);
siz[rt] += siz[to[i]];
maxsiz[rt] = max(maxsiz[rt], siz[to[i]]);
}
maxsiz[rt] = max(maxsiz[rt], sumsiz - siz[rt]);
if (maxsiz[root] > maxsiz[rt])
root = rt;
}
void getdis(ll rt, ll fa)
{
notedis[++notedis[0]] = dis[rt];
for (int i = head[rt]; i; i = nex[i])
{
if (vis[to[i]] || to[i] == fa)
continue;
dis[to[i]] = dis[rt] + val[i];
getdis(to[i], rt);
}
}
void calc(ll rt)
{
ll cnt = 0;
for (int i = head[rt]; i; i = nex[i])
{
if (vis[to[i]])
continue;
dis[to[i]] = val[i];
notedis[0] = 0;
getdis(to[i], rt);
for (int j = 1; j <= notedis[0]; j++)
for (int k = 1; k <= m; k++)
if (que[k] >= notedis[j])
ans[k] |= jud[que[k] - notedis[j]];
for (int j = 1; j <= notedis[0]; j++)
if (notedis[j] <= 1e7 + 5)
{
notejud[++cnt] = notedis[j];
jud[notedis[j]] = 1;
}
}
for (int i = 1; i <= cnt; i++)
jud[notejud[i]] = 0;
}
void solve(ll rt)
{
vis[rt] = jud[0] = 1;
calc(rt);
for (int i = head[rt]; i; i = nex[i])
{
if (vis[to[i]])
continue;
sumsiz = siz[to[i]];
root = 0;
maxsiz[root] = inf;
getroot(to[i], 0);
solve(root);
}
}
int main()
{
cin >> n >> m;
for (int i = 1; i < n; i++)
{
int x, y, z;
cin >> x >> y >> z;
addedge(x, y, z);
addedge(y, x, z);
}
for (int i = 1; i <= m; i++)
cin >> que[i];
root = 0;
maxsiz[root] = inf;
getroot(1, 0);
solve(root);
for (int i = 1; i <= m; i++)
puts(ans[i] ? "AYE" : "NAY");
return 0;
}