#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 100010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
int n,k;
vii ke[maxn];
int res[maxn];
namespace sub1
{
int sz[maxn];
void dfs1(int u,int par = 0)
{
sz[u] = 1;
for(auto [v,_] : ke[u]) if(v != par)
{
dfs1(v,u);
sz[u] += sz[v];
}
}
void dfs(int u,int par,int now)
{
for(auto [v,w] : ke[u]) if(v != par)
{
if(now < w) res[u] += sz[v],dfs(v,u,k - w);
else dfs(v,u,now - w);
}
}
void solve()
{
fo(i,1,n)
{
dfs1(i);
dfs(i,i,k);
fo(i,1,n) sz[i] = 0;
}
fo(i,1,n) cout << res[i] << "\n";
}
}
namespace sub3
{
deque<array<int,4>> q;
int tot = 0;
int del;
void dfs(int u,int par,int id)
{
for(auto [v,w] : ke[u]) if(v != par)
{
int cnt = 0;
tot += w;
int sum = 1;
while(ss(q))
{
auto [dist,val,pos,sl] = q.front();
if(dist >= tot) break;
q.pop_front();
sum += sl;
cnt += val - (id - pos) * sl;
}
res[u] += cnt;
q.push_back({k + tot - w,n - id + cnt,id,sum});
dfs(v,u,id + 1);
}
}
void solve()
{
fo(i,1,n) if(ss(ke[i]) == 1)
{
tot = 0;
q.clear();
del = 0;
dfs(i,i,1);
}
fo(i,1,n) cout << res[i] << "\n";
}
}
namespace sub5
{
int up[maxn][15],down[maxn][15];
int sz[maxn];
void dfs(int u,int par = 0)
{
sz[u] = 1;
for(auto [v,w] : ke[u]) if(v != par)
{
dfs(v,u);
sz[u] += sz[v];
}
}
void dfsup(int u,int par = 0)
{
up[u][k]++;
for(auto [v,w] : ke[u]) if(v != par)
{
dfsup(v,u);
fo(i,0,k)
{
if(i < w)
{
res[v] += up[v][i] * (n - sz[v]);
// if(up[v][i])
// {
// cout << v << ' ' << i << ' ' << up[v][i] << ' ' << (n - sz[v]) << "\n";
// }
up[u][k - w] += up[v][i];
}
else up[u][i - w] += up[v][i];
}
}
}
void dfsdown(int u,int par = 0)
{
vector<vi> pre(ss(ke[u]) + 2,vi(k + 1,0)),suf(ss(ke[u]) + 2,vi(k + 1,0));
down[u][k]++;
fo(i,1,ss(ke[u]))
{
auto [v,w] = ke[u][i - 1];
fo(j,0,k) pre[i][j] = pre[i - 1][j];
if(v == par) continue;
fo(j,0,k)
{
if(j < w) pre[i][k - w] += up[v][j];
else pre[i][j - w] += up[v][j];
}
}
fod(i,ss(ke[u]),1)
{
auto [v,w] = ke[u][i - 1];
fo(j,0,k) suf[i][j] = suf[i + 1][j];
if(v == par) continue;
fo(j,0,k)
{
if(j < w) suf[i][k - w] += up[v][j];
else suf[i][j - w] += up[v][j];
if(j < w)
{
down[v][k - w] += pre[i -1][j] + suf[i + 1][j] + down[u][j];
int val = (pre[i - 1][j] + suf[i + 1][j] + down[u][j]) * sz[v];
res[u] += (pre[i - 1][j] + suf[i + 1][j] + down[u][j]) * sz[v];
}
else down[v][j - w] += pre[i - 1][j] + suf[i + 1][j] + down[u][j];
}
dfsdown(v,u);
}
}
void solve()
{
dfs(1);
dfsup(1);
dfsdown(1);
fo(i,1,n) cout << res[i] << "\n";
}
}
main()
{
#define name "stations"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
bool sub3 = 1;
fo(i,1,n - 1)
{
int u,v,w;
cin >> u >> v >> w;
u++,v++;
if(w != 1) sub3 = 0;
ke[u].pb(v,w);
ke[v].pb(u,w);
}
if(n <= N) sub1::solve();
else if(sub3) sub3::solve();
else sub5::solve();
}
Compilation message (stderr)
Main.cpp:194:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
194 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:199:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
199 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:200:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
200 | freopen(name ".out", "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... |