#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
#define int long long
using namespace std;
const int mod=1e9+7,mxn=2e5+5,inf=1e18,minf=-1e18,lg=30;
//#undef int
int n,k,m,w,h,l;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
int ans[mxn+10];
vector<pii>adj[mxn+10];
vector<vector<int>>dp,dpu;
int sz[mxn+10];
void getsz(int cur,int p){sz[cur]=1;for(auto i:adj[cur])if(i.f!=p)getsz(i.f,cur),sz[cur]+=sz[i.f];}
void getdp(int cur,int p){
dp[cur][0]++;
for(auto i:adj[cur])if(i.f!=p){
getdp(i.f,cur);
for(int j=0;j<=k;j++){
if(j+i.s<=k)dp[cur][j+i.s]+=dp[i.f][j];
else{
ans[i.f]+=(dp[i.f][j]*(n-sz[i.f]));
dp[cur][i.s]+=dp[i.f][j];
}
}
}
}
void getdpu(int cur,int p){
vector<vector<int>>pref(adj[cur].size()+1,vector<int>(k+1,0)),suf(adj[cur].size()+1,vector<int>(k+1,0));
int c=0;
dpu[cur][0]++;
for(auto i:adj[cur])if(i.f!=p){
for(int j=0;j<=k;j++){
if(j+i.s<=k)pref[c][j+i.s]+=dp[i.f][j];
else pref[c][i.s]+=dp[i.f][j];
if(c)pref[c][j]+=pref[c-1][j];
}
c++;
}
c=adj[cur].size()-1;
if(p!=-1)c--;
for(int i=adj[cur].size()-1;i>=0;i--)if(adj[cur][i].f!=p){
for(int j=0;j<=k;j++){
if(j+adj[cur][i].s<=k)suf[c][j+adj[cur][i].s]+=dp[adj[cur][i].f][j];
else suf[c][adj[cur][i].s]+=dp[adj[cur][i].f][j];
suf[c][j]+=suf[c+1][j];
}
c--;
}
c=0;
for(auto i:adj[cur])if(i.f!=p){
for(int j=0;j<=k;j++){
if(j+i.s<=k){
dpu[i.f][j+i.s]+=dpu[cur][j];
if(c)dpu[i.f][j+i.s]+=pref[c-1][j];
dpu[i.f][j+i.s]+=suf[c+1][j];
}
else{
int sum=dpu[cur][j];
if(c)sum+=pref[c-1][j];
sum+=suf[c+1][j];
dpu[i.f][i.s]+=sum;
ans[cur]+=(sz[i.f]*sum);
}
}
c++;
}
for(auto i:adj[cur])if(i.f!=p)getdpu(i.f,cur);
}
int32_t main(){
fastio
cin>>n>>k;
dp.resize(n+1,vector<int>(k+1,0));
dpu.resize(n+1,vector<int>(k+1,0));
for(int i=0;i<n-1;i++){
int a,b,c;cin>>a>>b>>c;
adj[a].pb({b,c});
adj[b].pb({a,c});
}
getsz(0,-1);
getdp(0,-1);
getdpu(0,-1);
for(int i=0;i<n;i++)cout<<ans[i]<<'\n';
}
/*
dp(x,k) = cnt node A in subtree x with dist =k
dpu(x,k) =cnt node outside subtree x
*/
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
32 | #pragma GCC optimize ("03,unroll-lopps")
| ^
Main.cpp:38:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
38 | void setIO(string name){
| ^
Main.cpp:47:25: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
47 | void getsz(int cur,int p){sz[cur]=1;for(auto i:adj[cur])if(i.f!=p)getsz(i.f,cur),sz[cur]+=sz[i.f];}
| ^
Main.cpp:48:25: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
48 | void getdp(int cur,int p){
| ^
Main.cpp:61:26: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
61 | void getdpu(int cur,int p){
| ^
Main.cpp:103:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
103 | int32_t main(){
| ^
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |