This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX_N = 70000;
int ans[MAX_N], clones[MAX_N], k, n, num[MAX_N];
vector<vector<pair<int,int>>>adjL(MAX_N);
vector<int>vec, vec2;
vector<bool>visited;
int dfs(int pos, int fuel) {
visited[pos] = 1;
int tmp = 1;
for(pair<int,int>p: adjL[pos]) {
if(visited[p.first]) continue;
if(fuel < p.second) {
int X = dfs(p.first, k - p.second);
ans[pos] += X;
tmp += X;
} else tmp += dfs(p.first, fuel - p.second);
}
return tmp;
}
void calc(int pos, int prev) {
for(pair<int,int>p: adjL[pos]) {
if(p.first == prev) continue;
vec.push_back(vec.back() + p.second);
vec2.push_back(p.first);
calc(p.first, pos);
num[pos] += num[p.first];
}
}
void calc2(int pos, int prev, int cnt) {
int brk = upper_bound(vec.begin(), vec.end(), vec[cnt] + k) - vec.begin();
if(brk == vec.size()) return;
else {
--brk;
clones[vec2[brk]] += clones[pos];
ans[vec2[brk]] += (num[vec2[brk]] - 1) * clones[pos];
}
for(pair<int,int>p: adjL[pos]) {
if(p.first == prev) continue;
calc2(p.first, pos, cnt + 1);
}
}
void solve() {
cin >> n >> k;
for(int i=0; i<n-1; ++i) {
int u, v, w;
cin >> u >> v >> w;
adjL[u].push_back({v, w});
adjL[v].push_back({u, w});
}
if(n <= 1000) {
for(int i=0; i<n; ++i) {
visited.assign(n, 0);
dfs(i, k);
}
} else {
for(int i=0; i<n; ++i) {
if(adjL[i].size() == 1) {
for(int i=0; i<n; ++i) clones[i] = num[i] = 1;
vec.push_back(0);
vec2.push_back(i);
calc(i, i);
calc2(i, i, 0);
vec.clear();
vec2.clear();
}
}
}
for(int i=0; i<n; ++i) cout << ans[i] << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
while(t--) solve();
}
Compilation message (stderr)
Main.cpp: In function 'void calc2(long long int, long long int, long long int)':
Main.cpp:39:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | if(brk == vec.size()) return;
| ~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |