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;
vector<vector<pair<int,int>>>adjL(MAX_N);
vector<int>vec;
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) {
int cur;
for(pair<int,int>p: adjL[pos]) {
if(p.first == prev) continue;
vec.push_back(vec.back() + p.second);
cur = vec.back();
calc(p.first, pos);
}
int brk = lower_bound(vec.begin(), vec.end(), cur + k) - vec.begin();
if(brk == vec.size()) return;
else {
--brk;
clones[brk] += clones[pos];
ans[brk] += clones[pos];
}
}
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) clones[i] = 1;
vec.push_back(0);
for(int i=0; i<n; ++i) {
if(adjL[i].size() == 1) {
calc(i, i);
break;
}
}
}
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 calc(long long int, long long int)':
Main.cpp:36: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]
36 | if(brk == vec.size()) return;
| ~~~~^~~~~~~~~~~~~
Main.cpp:35:55: warning: 'cur' may be used uninitialized in this function [-Wmaybe-uninitialized]
35 | int brk = lower_bound(vec.begin(), vec.end(), cur + k) - vec.begin();
| ~~~~^~~
# | 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... |