mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-08 13:58:48 +00:00
2591. [Usaco 2012 Feb]Nearby Cows
https://hydro.ac/d/bzoj/record/6322eb9032ae6c3198cc9ae8
This commit is contained in:
parent
0c692c1357
commit
244f91eef3
79
BZOJ/2591/2591.cpp
Normal file
79
BZOJ/2591/2591.cpp
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <array>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using std::cin;
|
||||||
|
using std::cout;
|
||||||
|
const char endl = '\n';
|
||||||
|
|
||||||
|
const int N = 100005;
|
||||||
|
|
||||||
|
int n, k, fa[N];
|
||||||
|
std::vector<int> g[N];
|
||||||
|
std::array<int[25], N> f1, f2;
|
||||||
|
|
||||||
|
void dfs1(int u) {
|
||||||
|
for (int v : g[u]) {
|
||||||
|
if (v == fa[u]) continue;
|
||||||
|
|
||||||
|
fa[v] = u;
|
||||||
|
dfs1(v);
|
||||||
|
|
||||||
|
for (int i = 1; i <= k; i++) {
|
||||||
|
f1[u][i] += f1[v][i - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dfs2(int u) {
|
||||||
|
for (int v : g[u]) {
|
||||||
|
if (v != fa[u]) dfs2(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i <= k; i++) {
|
||||||
|
int cnt = 0, t = u;
|
||||||
|
|
||||||
|
while (++cnt < i && fa[t]) {
|
||||||
|
f1[u][i] += (f2[fa[t]][i - cnt] - f2[t][i - cnt - 1]);
|
||||||
|
t = fa[t];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fa[t]) f1[u][i] += f2[fa[t]][0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
cin >> n >> k;
|
||||||
|
|
||||||
|
for (int i = 1, u, v; i < n; i++) {
|
||||||
|
cin >> u >> v;
|
||||||
|
|
||||||
|
g[u].push_back(v);
|
||||||
|
g[v].push_back(u);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1, x; i <= n; i++) {
|
||||||
|
cin >> x;
|
||||||
|
|
||||||
|
f1[i][0] = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
dfs1(1);
|
||||||
|
f2 = f1;
|
||||||
|
dfs2(1);
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
for (int j = 0; j <= k; j++) {
|
||||||
|
sum += f1[i][j];
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << sum << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
BIN
BZOJ/2591/data/1.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/1.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/10.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/10.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/10.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/10.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/2.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/2.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/3.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/3.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/4.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/4.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/5.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/5.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/5.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/6.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/6.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/6.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/6.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/7.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/7.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/7.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/7.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/8.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/8.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/8.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/8.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/9.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/9.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/2591/data/9.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/2591/data/9.out
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user