#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=array<int,2>;
using tii=array<int,3>;
using tll=array<ll,3>;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N=70005;
int n,k;
vector<pii> g[N];
int sz[N],vis[N],sub[N],p[16][N],val[N],up[N];
ll cttl,csub[N],cnt[N];
int m; ll D[N],P[N];
vector<int> V;
vector<pii> vals;
vector<tll> dists;
ll d[N],st[N],dp[N];
ll ans[N];
struct FEN{
ll F[N];
int n;
void init(int n_){
n=n_;
for(int i=0;i<=n;i++) F[i]=0;
}
void add(int i,ll v){
for(;i<=n;i+=i&-i) F[i]+=v;
}
ll qry(int i){
ll r=0;
for(;i;i-=i&-i) r+=F[i];
return r;
}
}F;
void dfsSize(int u,int p){
sz[u]=1;
for(auto [v,w]: g[u]) if(!vis[v]&&p!=v){
dfsSize(v,u);
sz[u]+=sz[v];
}
}
int findCentroid(int u,int p,int S){
for(auto [v,w]: g[u]) if(!vis[v]&&p!=v&&sz[v]>=S) return findCentroid(v,u,S);
return u;
}
void dfs(int u){
V.push_back(u);
dp[u]=1;
D[++m]=d[u]-1;
for(auto [v,w]: g[u]) if(!vis[v]&&p[0][u]!=v){
p[0][v]=u;
for(int i=1;i<=15;i++) p[i][v]=p[i-1][p[i-1][v]];
sub[v]=sub[u];
if(!p[0][u]) sub[v]=v;
d[v]=d[u]+w;
st[v]=k-w;
dfs(v);
}
if(d[u]>k){
int v=u;
for(int i=15;i>=0;i--) if(d[u]-d[p[i][v]]<=k) v=p[i][v];
up[u]=v;
} else{
up[u]=0;
val[u]=k-d[u];
}
if(p[0][u]){
dists.push_back({d[u]-1,u,1});
dists.push_back({d[p[0][u]]-1,u,-1});
}
csub[u]=cnt[u]=0;
}
void dfs2(int u){
P[u]=F.qry(lower_bound(D+1,D+m+1,d[u]-1)-D);
debug(u,cnt[u]);
cnt[u]+=P[u]-P[p[0][u]];
F.add(lower_bound(D+1,D+m+1,d[u]+st[u])-D,cnt[u]);
for(auto [v,w]: g[u]) if(!vis[v]&&p[0][u]!=v){
dfs2(v);
}
F.add(lower_bound(D+1,D+m+1,d[u]+st[u])-D,-cnt[u]);
}
void solve(int r){
dfsSize(r,0);
r=findCentroid(r,0,(sz[r]+1)/2);
for(int i=0;i<=15;i++) p[i][r]=0;
d[r]=0;
sub[r]=0;
vals.clear();
dists.clear();
V.clear();
m=0;
P[r]=0;
dfsSize(r,0);
dfs(r);
D[++m]=1e18;
sort(D+1,D+m+1);
m=unique(D+1,D+m+1)-D-1;
for(int u: V){
if(up[u]) val[u]=val[up[u]];
vals.push_back({val[u],u});
}
for(int i=(int)V.size()-1;i>=0;i--){
int u=V[i];
if(up[u]) dp[up[u]]+=dp[u];
}
sort(vals.begin(),vals.end());
sort(dists.begin(),dists.end());
cttl=0;
for(int i=0,j=0;i<(int)dists.size();i++){
while(j<(int)vals.size()&&vals[j][0]<=dists[i][0]){
cttl++;
csub[sub[vals[j][1]]]++;
debug(vals[j]);
j++;
}
debug(dists[i],cttl,csub[sub[dists[i][1]]]);
cnt[dists[i][1]]+=dists[i][2]*(cttl-csub[sub[dists[i][1]]]);
}
F.init(m);
for(auto [v,w]: g[r]) if(!vis[v]) dfs2(v);
debug(r);
for(int u: V){
ans[u]+=(dp[u]-1)*((ll)V.size()-sz[sub[u]]-(u==r));
debug(u,dp[u],V.size(),sz[sub[u]],cnt[u]);
if(u!=r) ans[p[0][u]]+=cnt[u]*sz[u];
}
debug(vector<int>(ans+1,ans+n+1));
vis[r]=1;
for(auto [v,w]: g[r]) if(!vis[v]) solve(v);
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>k;
for(int u,v,w,i=1;i<n;i++){
cin>>u>>v>>w;
u++; v++;
g[u].push_back({v,w});
g[v].push_back({u,w});
}
solve(1);
for(int i=1;i<=n;i++) cout<<ans[i]<<"\n";
return 0;
}
Compilation message
Main.cpp: In function 'void dfs2(int)':
Main.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 42
| ^~
Main.cpp:82:2: note: in expansion of macro 'debug'
82 | debug(u,cnt[u]);
| ^~~~~
Main.cpp: In function 'void solve(int)':
Main.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 42
| ^~
Main.cpp:121:4: note: in expansion of macro 'debug'
121 | debug(vals[j]);
| ^~~~~
Main.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 42
| ^~
Main.cpp:124:3: note: in expansion of macro 'debug'
124 | debug(dists[i],cttl,csub[sub[dists[i][1]]]);
| ^~~~~
Main.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 42
| ^~
Main.cpp:129:2: note: in expansion of macro 'debug'
129 | debug(r);
| ^~~~~
Main.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 42
| ^~
Main.cpp:132:3: note: in expansion of macro 'debug'
132 | debug(u,dp[u],V.size(),sz[sub[u]],cnt[u]);
| ^~~~~
Main.cpp:13:20: warning: statement has no effect [-Wunused-value]
13 | #define debug(...) 42
| ^~
Main.cpp:135:2: note: in expansion of macro 'debug'
135 | debug(vector<int>(ans+1,ans+n+1));
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10300 KB |
Output is correct |
3 |
Correct |
3 ms |
10332 KB |
Output is correct |
4 |
Correct |
4 ms |
10332 KB |
Output is correct |
5 |
Correct |
4 ms |
10332 KB |
Output is correct |
6 |
Correct |
6 ms |
10588 KB |
Output is correct |
7 |
Correct |
5 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10304 KB |
Output is correct |
9 |
Correct |
4 ms |
10332 KB |
Output is correct |
10 |
Correct |
4 ms |
10520 KB |
Output is correct |
11 |
Correct |
4 ms |
10312 KB |
Output is correct |
12 |
Correct |
6 ms |
10308 KB |
Output is correct |
13 |
Correct |
5 ms |
10332 KB |
Output is correct |
14 |
Correct |
3 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
779 ms |
26844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10300 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
779 ms |
26844 KB |
Output is correct |
5 |
Correct |
783 ms |
26428 KB |
Output is correct |
6 |
Correct |
542 ms |
26172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10300 KB |
Output is correct |
3 |
Correct |
433 ms |
23252 KB |
Output is correct |
4 |
Correct |
767 ms |
26940 KB |
Output is correct |
5 |
Correct |
417 ms |
26284 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
484 ms |
23652 KB |
Output is correct |
8 |
Correct |
455 ms |
23780 KB |
Output is correct |
9 |
Correct |
477 ms |
23520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10300 KB |
Output is correct |
3 |
Correct |
433 ms |
23252 KB |
Output is correct |
4 |
Correct |
767 ms |
26940 KB |
Output is correct |
5 |
Correct |
417 ms |
26284 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
484 ms |
23652 KB |
Output is correct |
8 |
Correct |
455 ms |
23780 KB |
Output is correct |
9 |
Correct |
477 ms |
23520 KB |
Output is correct |
10 |
Correct |
310 ms |
23252 KB |
Output is correct |
11 |
Correct |
305 ms |
22764 KB |
Output is correct |
12 |
Correct |
309 ms |
22768 KB |
Output is correct |
13 |
Correct |
351 ms |
23532 KB |
Output is correct |
14 |
Correct |
336 ms |
23676 KB |
Output is correct |
15 |
Correct |
357 ms |
24300 KB |
Output is correct |
16 |
Correct |
63 ms |
22968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10300 KB |
Output is correct |
3 |
Correct |
3 ms |
10332 KB |
Output is correct |
4 |
Correct |
4 ms |
10332 KB |
Output is correct |
5 |
Correct |
4 ms |
10332 KB |
Output is correct |
6 |
Correct |
6 ms |
10588 KB |
Output is correct |
7 |
Correct |
5 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10304 KB |
Output is correct |
9 |
Correct |
4 ms |
10332 KB |
Output is correct |
10 |
Correct |
4 ms |
10520 KB |
Output is correct |
11 |
Correct |
4 ms |
10312 KB |
Output is correct |
12 |
Correct |
6 ms |
10308 KB |
Output is correct |
13 |
Correct |
5 ms |
10332 KB |
Output is correct |
14 |
Correct |
3 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
779 ms |
26844 KB |
Output is correct |
17 |
Correct |
783 ms |
26428 KB |
Output is correct |
18 |
Correct |
542 ms |
26172 KB |
Output is correct |
19 |
Correct |
433 ms |
23252 KB |
Output is correct |
20 |
Correct |
767 ms |
26940 KB |
Output is correct |
21 |
Correct |
417 ms |
26284 KB |
Output is correct |
22 |
Correct |
2 ms |
10332 KB |
Output is correct |
23 |
Correct |
484 ms |
23652 KB |
Output is correct |
24 |
Correct |
455 ms |
23780 KB |
Output is correct |
25 |
Correct |
477 ms |
23520 KB |
Output is correct |
26 |
Correct |
310 ms |
23252 KB |
Output is correct |
27 |
Correct |
305 ms |
22764 KB |
Output is correct |
28 |
Correct |
309 ms |
22768 KB |
Output is correct |
29 |
Correct |
351 ms |
23532 KB |
Output is correct |
30 |
Correct |
336 ms |
23676 KB |
Output is correct |
31 |
Correct |
357 ms |
24300 KB |
Output is correct |
32 |
Correct |
63 ms |
22968 KB |
Output is correct |
33 |
Correct |
349 ms |
22500 KB |
Output is correct |
34 |
Correct |
302 ms |
23776 KB |
Output is correct |
35 |
Correct |
366 ms |
23740 KB |
Output is correct |
36 |
Correct |
387 ms |
23316 KB |
Output is correct |
37 |
Correct |
335 ms |
23132 KB |
Output is correct |
38 |
Correct |
353 ms |
22024 KB |
Output is correct |
39 |
Correct |
306 ms |
23904 KB |
Output is correct |
40 |
Correct |
77 ms |
23576 KB |
Output is correct |