#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
typedef long long ll;
using namespace std;
const int N = 70070;
ll cnt[N];
int subtree[N];
ll pref[N];
ll people[N];
vector <vector<pair<int, int>>> G;
int n, k;
void dfs_0(int v, int p) {
subtree[v] = 1;
for (auto it : G[v]) {
if (it.first == p) continue;
dfs_0(it.first, v);
subtree[v] += subtree[it.first];
}
}
void dfs(int v, int p, int depth) {
for (auto it : G[v]) {
if (it.first == p) continue;
int c = depth + it.second;
if (c <= k) dfs(it.first, v, c);
else {
cnt[v] += subtree[it.first];
dfs(it.first, v, it.second);
}
}
}
void f(vector <int> indd, vector <int> len) {
indd.insert(indd.begin(), 0);
len.insert(len.begin(), 0);
for (int i = 1; i < n; i++) {
pref[i] = pref[i - 1] + len[i];
}
for (int i = 1; i <= n; i++) {
people[i] = 1;
}
for (int i = 1; i <= n; i++) {
if ((pref[n - 1] - pref[i - 1]) <= k) continue;
int ind = upper_bound(pref + 1, pref + n, pref[i - 1] + k) - pref;
people[ind] += people[i];
cnt[indd[ind]] += (people[i] * 1ll * (n - ind));
}
}
int main() {
cin >> n >> k;
G.resize(n + 1);
for (int i = 1; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
a++; b++;
G[a].push_back({ b, c });
G[b].push_back({ a, c });
}
if (n <= 1000) {
for (int i = 1; i <= n; i++) {
dfs_0(i, i);
dfs(i, i, 0);
}
for (int i = 1; i <= n; i++) {
cout << cnt[i] << "\n";
}
return 0;
}
vector <int> ind;
vector <int> len;
for (int i = 1; i <= n; i++) {
if ((int)G[i].size() == 1) {
ind.push_back(i);
break;
}
}
while ((int)ind.size() != n) {
int v = ind.back(), p = -1;
if ((int)ind.size() != 1) p = ind[(int)ind.size() - 2];
for (auto it : G[v]) {
if (it.first == p) continue;
ind.push_back(it.first);
len.push_back(it.second);
break;
}
}
f(ind, len);
reverse(ind.begin(), ind.end()); reverse(len.begin(), len.end());
f(ind, len);
for (int i = 1; i <= n; i++) {
cout << cnt[i] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
8 ms |
348 KB |
Output is correct |
4 |
Correct |
17 ms |
496 KB |
Output is correct |
5 |
Correct |
9 ms |
528 KB |
Output is correct |
6 |
Correct |
22 ms |
344 KB |
Output is correct |
7 |
Correct |
18 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
10 ms |
516 KB |
Output is correct |
10 |
Correct |
11 ms |
344 KB |
Output is correct |
11 |
Correct |
10 ms |
520 KB |
Output is correct |
12 |
Correct |
17 ms |
348 KB |
Output is correct |
13 |
Correct |
10 ms |
344 KB |
Output is correct |
14 |
Correct |
7 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
57 ms |
7684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
57 ms |
7684 KB |
Output is correct |
5 |
Correct |
82 ms |
8824 KB |
Output is correct |
6 |
Correct |
47 ms |
8564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
3577 ms |
4272 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
3577 ms |
4272 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
8 ms |
348 KB |
Output is correct |
4 |
Correct |
17 ms |
496 KB |
Output is correct |
5 |
Correct |
9 ms |
528 KB |
Output is correct |
6 |
Correct |
22 ms |
344 KB |
Output is correct |
7 |
Correct |
18 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
10 ms |
516 KB |
Output is correct |
10 |
Correct |
11 ms |
344 KB |
Output is correct |
11 |
Correct |
10 ms |
520 KB |
Output is correct |
12 |
Correct |
17 ms |
348 KB |
Output is correct |
13 |
Correct |
10 ms |
344 KB |
Output is correct |
14 |
Correct |
7 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
57 ms |
7684 KB |
Output is correct |
17 |
Correct |
82 ms |
8824 KB |
Output is correct |
18 |
Correct |
47 ms |
8564 KB |
Output is correct |
19 |
Execution timed out |
3577 ms |
4272 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |