#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define en cout << '\n'
#define vi vector<int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
const int N = 1e5+100;
int n, k, tin[N], tout[N], timer = 0;
vector<pii> g[N];
ll dp[N], s[N], dist[N], pd[N];
bool is_ancestor(int u, int v){
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
void f(int v, int p, int up, ll parw){
// cout << v << ' ' << p << ' ' << up << ' ' << parw << ' ' << dist[v] << "f\n";
if(dist[v] - dist[up] > k) return;
if(dist[v] - dist[up] <= k && dist[v] - dist[up] + parw > k){
// cout << "gh\n";
dp[up] += dp[v];
}
for(auto U: g[v]){
int u = U.ff;
if(p != u){
f(u, v, up, parw);
}
}
}
void f2(int v, int p, int up, ll parw, ll D){
// cout << v << ' ' << p << ' ' << up << ' ' << parw << ' ' << dist[v] << "f\n";
if(D > k) return;
if(D <= k && D + parw > k){
if(is_ancestor(v, up))
pd[up] += pd[v];
else
pd[up] += dp[v];
}
for(auto U: g[v]){
int u = U.ff;
if(p != u){
f2(u, v, up, parw, D+U.ss);
}
}
}
void dfs(int v, int p, ll parw){
dp[v] = 1;
s[v] = 1;
tin[v] = timer++;
for(auto U: g[v]){
int u = U.ff;
if(u != p){
dist[u] = dist[v] + U.ss;
dfs(u, v, U.ss);
s[v] += s[u];
}
}
tout[v] = timer++;
f(v, p, v, parw);
}
void dfs2(int v, int p, ll parw){
pd[v] = 1;
if(p != v)
f2(p, v, p, parw, 0ll);
for(auto U: g[v]){
int u = U.ff;
if(u != p){
dfs2(u, v, U.ss);
}
}
}
void solve(){
cin >> n >> k;
for(int i = 1; i < n; ++i){
int u, v, w; cin >> u >> v >> w;
++u, ++v;
g[u].pb({v, w});
g[v].pb({u, w});
}
dfs(1, 1, 0);
// for(int i = 1; i <= n; ++i){
// cout << dp[i] << ' ';
// }
dfs2(1, 1, 0);
// for(int i = 1; i <= n; ++i) cout << dp[i] << ' ' << pd[i] << '\n';
for(int i = 1; i <= n; ++i){
cout << (dp[i] - 1) * (n - s[i]) + (pd[i] - 1) * (s[i] - 1) << '\n';
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5720 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5720 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Incorrect |
5 ms |
3676 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5724 KB |
Output is correct |
2 |
Incorrect |
32 ms |
14164 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5720 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Incorrect |
32 ms |
14164 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5720 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Incorrect |
72 ms |
10104 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5720 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Incorrect |
72 ms |
10104 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5720 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Incorrect |
5 ms |
3676 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |