SIXIANG32 @ 2021-03-19 20:38:30
#include <iostream>
#include <vector>
#define MAXN 100000
#define QWQ printf("qwq\n");
using namespace std;
int max(int x, int y) {return ((x > y) ? (x) : (y));}
struct node {
int to, val;
node(int T, int V) {
to = T, val = V;
}
};
vector <node> gra[MAXN + 10];
int siz[MAXN + 10], rt, mp = 0, dis[MAXN + 10], qwq, res[MAXN + 10], ans[MAXN + 10];
int pikachu[MAXN + 10];
bool have[MAXN + 10], pikapika[MAXN + 10], vis[MAXN + 10];
int n, m, ask[MAXN + 10];
void get_root(int u, int size) {
siz[u] = 1, vis[u] = 1;
int now = 0;
for(int p = 0; p < gra[u].size(); p++) {
int v = gra[u][p].to;
if(!vis[v]) {
get_root(v, size);
siz[u] += siz[v];
now = max(now, siz[v]);
}
}
now = max(now, size - siz[u]);
if(now < mp) mp = now, rt = u;
}
void get_dis(int u, int fa) {
res[++qwq] = dis[u];
for(int p = 0; p < gra[u].size(); p++) {
int v = gra[u][p].to;
if(v != fa) {
dis[v] = dis[u] + gra[u][p].val;
get_dis(v, u);
}
}
}
void calc(int u) {
int C = 0;
for(int p = 0; p < gra[u].size(); p++) {
int v = gra[u][p].to;
if(!vis[v]) {
qwq = 0, dis[v] = gra[u][p].val;
get_dis(v, u);
for(int i = qwq; i >= 1; i--)
for(int j = 1; p <= m; p++)
if(ask[j] >= res[i])
have[j] |= pikapika[ask[j] - res[i]];
for(int i = qwq; i >= 1; i--)
pikapika[res[i]] = 1, pikachu[++C] = res[i];
}
}
for(int p = 1; p <= C; p++)
pikapika[pikachu[p]] = 0;
}
void solve(int u) {
cout << u << endl;
vis[u] = pikapika[0] = 1, calc(u);
for(int p = 0; p <= gra[u].size(); p++) {
int v = gra[u][p].to;
if(!vis[v]) {
rt = 0, mp = 0;
get_root(v, siz[v]);
solve(rt);
}
}
}
int main() {
cin >> n >> m;
for(int p = 1, x, y, z; p < n; p++) {
cin >> x >> y >> z;
gra[x].push_back(node(y, z));
gra[y].push_back(node(x, z));
}
for(int p = 1; p <= m; p++)
cin >> ask[p];
get_root(1, n);
solve(rt);
for(int p = 1; p <= m; p++)
if(have[ask[p]]) cout << "AYE" << endl;
else cout << "NAY" << endl;
}
代码写到最后都不知道自己在写什么了/dk
求助惹 qwq