#include <bits/stdc++.h> using namespace std;
const int N = 10010; const int M = 110;
int n, m; int rt, sum; int siz[N], weight[N], q[M]; bool vis[N], ans[N], tf[10000010]; vector<pair<int, int>> adj[N];
void getRoot(int u, int fa) { siz[u] = 1, weight[u] = 0; for (auto &[v, w] : adj[u]) { if (v == fa || vis[v]) continue; getRoot(v, u); weight[u] = max(weight[u], siz[v]); siz[u] += siz[v]; } weight[u] = max(weight[u], sum - siz[u]); if (weight[u] < weight[rt]) rt = u; }
int d[N], dis[N], cnt; void getDis(int u, int fa) { dis[++cnt] = d[u]; for (auto &[v, w] : adj[u]) { if (v == fa || vis[v]) continue; d[v] = d[u] + w, getDis(v, u); } }
queue<int> del; void dfs(int u, int fa) { tf[0] = 1; del.push(0); vis[u] = 1; for (auto &[v, w] : adj[u]) { if (v == fa || vis[v]) continue; d[v] = w; getDis(v, u); for (int k = 1; k <= cnt; ++k) for (int i = 1; i <= m; ++i) if (q[i] >= dis[k]) ans[i] |= tf[q[i] - dis[k]];
for (int k = 1; k <= cnt; ++k) if (dis[k] <= 1e7) del.push(dis[k]), tf[dis[k]] = 1;
cnt = 0; } while (!del.empty()) tf[del.front()] = 0, del.pop(); for (auto &[v, w] : adj[u]) { if (v == fa || vis[v]) continue; sum = siz[v]; rt = 0; weight[rt] = INT_MAX; getRoot(v, u); getRoot(rt, -1); dfs(rt, u); } }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for (int i = 1; i < n; ++i) { int u, v, w; cin >> u >> v >> w; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } for (int i = 1; i <= m; ++i) cin >> q[i]; rt = 0, sum = n, weight[rt] = INT_MAX; getRoot(1, -1); getRoot(rt, -1); dfs(rt, -1); for (int i = 1; i <= m; ++i) if (ans[i] == 1) cout << "AYE\n"; else cout << "NAY\n"; return 0; }
|