「JLOI2012」树
「JLOI2012」树
传送门
不得不说这题的数据是真的水。。。
我们可以想到很明确的一条思路:枚举每一个点向根节点跳,知道路径和不小于 \(s\),恰好等于 \(s\) 就直接加答案。
跳的过程可以用倍增搞,但是暴力跳也可以过(这棵树的高度比较友好啊)
我只给了暴力的代码,倍增的懒得去写了。。。
参考代码:
/*--------------------------------
Code name: B.cpp
Author: The Ace Bee
This code is made by The Ace Bee
--------------------------------*/
#include <cstdio>
#define rg register
#define file(x) \
freopen(x".in", "r", stdin); \
freopen(x".out", "w", stdout);
const int $ = 100010;
inline int read() {
int s = 0; bool f = false; char c = getchar();
while (c < '0' || c > '9') f |= (c == '-'), c = getchar();
while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
return f ? -s : s;
}
int n, s, val[$], fa[$];
inline int jump(int u) {
int tmp = 0;
for (; u && tmp < s; u = fa[u]) tmp += val[u];
return tmp == s;
}
int main() {
// file("B");
n = read(), s = read();
for (rg int i = 1; i <= n; ++i) val[i] = read();
for (rg int u, v, i = 1; i <= n - 1; ++i)
u = read(), v = read(), fa[v] = u;
int ans = 0;
for (rg int i = 1; i <= n; ++i) ans += jump(i);
printf("%d\n", ans);
return 0;
}