https://vjudge.net/contest/292999#problem/J
https://blog.csdn.net/V5ZSQ/article/details/64919651?locationNum=5&fps=1
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxm = 5e5 + 5; typedef long long ll; ll w[maxm], sum[maxm]; int pa[maxm], id[maxm], cnt[maxm]; int t; int n; struct edge { int v; ll w; edge(int v = 0, ll w = 0) : v(v), w(w) {}; }; vector<edge> ve[maxm]; int res; void dfs(int u, int p) { for(int i = 0; i < ve[u].size(); i++) { int v = ve[u][i].v; ll val = ve[u][i].w; if(v == p) continue; pa[v] = u; id[res] = v; sum[res] = sum[res - 1] + val; int pos = lower_bound(sum, sum + res + 1, sum[res] - w[v]) - sum; if(pos < res) cnt[u]++, cnt[pa[id[pos] ] ]--; res++; dfs(v, u); res--; cnt[u] += cnt[v]; } } int main() { freopen("car.in", "r", stdin); scanf("%d", &t); while(t--) { scanf("%d", &n); memset(pa, 0, sizeof(pa)); memset(sum, 0, sizeof(sum)); memset(cnt, 0, sizeof(cnt)); for(int i = 1; i <= n; i++) ve[i].clear(); int u, v; ll val; for(int i = 1; i <= n; i++) scanf("%lld", &w[i]); for(int i = 1; i < n; i++) { scanf("%d%d%lld", &u, &v, &val); ve[u].push_back(edge(v, val)); ve[v].push_back(edge(u, val)); } pa[1] = 0, id[0] = 1, sum[0] = 0; res = 1; dfs(1, 1); for(int i = 1; i <= n; i++) { printf("%d%c", cnt[i], i == n ? ‘\n‘ : ‘ ‘); } } return 0; }
原文:https://www.cnblogs.com/downrainsun/p/10660377.html