1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
   | #include<bits/stdc++.h> #define rep(i, a, b)    for(int i = (a); i <= (b); ++i) #define per(i, a, b)    for(int i = (a); i >= (b); --i) #define debug(x)    cerr << #x << ' ' << x << endl #define size sizeeeeeee using namespace std;
  typedef long long ll; const int MAXN = 1e4 + 7; const int MAXM = 1e7 + 7; const int MOD = 1e9 + 7;
  vector<pair<int, int> > G[MAXN]; int son[MAXN], dep[MAXN], f[MAXN], q[MAXN], ans[MAXN], k; int root, size; int cnt[MAXM], n, m; bool vis[MAXN];
  void get_rt(int x, int fa) {     son[x] = 1; f[x] = 0;     for(auto p: G[x]) {         int u = p.first;         if(u == fa || vis[u])   continue;         get_rt(u, x);         son[x] += son[u];         f[x] = max(f[x], son[u]);     }     f[x] = max(f[x], size - son[x]);     if(f[x] < f[root])  root = x; } vector<int> v; void get_dep(int x, int fa) {     v.push_back(dep[x]);     for(auto p: G[x]) {         int u = p.first, w = p.second;         if(u == fa || vis[u])   continue;         dep[u] = dep[x] + w;         get_dep(u, x);     } } void calc(int x, int w, int op) {     v.clear(); dep[x] = w; get_dep(x, 0);     for(int p: v) {         rep(i, 1, m) if(p <= q[i]) ans[i] += op * cnt[q[i] - p];         cnt[p]++;     }     for(int p: v) cnt[p]--; } void solve(int x) {     calc(x, 0, 1); vis[x] = 1;     for(auto p: G[x]) {         int u = p.first, w = p.second;         if(vis[u])  continue;         calc(u, w, -1);         root = 0; size = son[u];         get_rt(u, 0);         solve(root);     } } int main() {     scanf("%d %d", &n, &m);     int u, v, w;     rep(i, 1, n-1) {         scanf("%d %d %d", &u, &v, &w);         G[u].push_back({v, w});         G[v].push_back({u, w});     }     rep(i, 1, m) scanf("%d", &q[i]);     f[0] = INT_MAX; size = n; root = 0;     get_rt(1, 0); solve(root);     rep(i, 1, m) {         if(ans[i])  printf("AYE\n");         else printf("NAY\n");     }     return 0; }
   |