#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 998244353;
const ll LOGN = 22;
const ll MAXN = 1e5 + 5;
#define int long long
vector<vector<pair<int,int>>> graph;
int marked[MAXN], sz[MAXN], fen[MAXN], fen_len[MAXN];
void update(int k, int x) {
k++;
if (k <= 0)
return ;
while (k < MAXN) {
fen[k] += x;
k += k & -k;
}
}
void update_len(int k, int x) {
k++;
if (k <= 0)
return ;
while (k < MAXN) {
fen_len[k] += x;
k += k & -k;
}
}
int get(int k) {
k++;
if (k <= 0)
return 0;
int res = 0;
while (k) {
res += fen[k];
k -= k & -k;
}
return res;
}
int get_len(int k) {
k++;
if (k <= 0)
return 0;
int res = 0;
while (k) {
res += fen_len[k];
k -= k & -k;
}
return res;
}
int get_sz(int node, int parent) {
sz[node] = 1;
for (auto u : graph[node]) {
if (u.f == parent || !marked[u.f])
continue;
sz[node] += get_sz(u.f, node);
}
return sz[node];
}
int find_centro(int node, int parent, int n) {
for (auto u : graph[node]) {
if (u.f != parent && !marked[u.f] && sz[u.f] * 2 >= n)
return find_centro(u.f, node, n);
}
return node;
}
ll ans = 0;
int k;
void eval(int node, int parent, int mx, int len) {
ans += get_len(mx - len - k);
ans += get(MAXN) - get(len + k - 1);
for (auto u : graph[node]) {
if (u.f != parent && !marked[u.f])
eval(u.f, node, max(mx, u.s), len+1);
}
}
void add(int node, int parent, int mx, int len) {
update_len(len, 1);
update(mx - len, 1);
for (auto u : graph[node]) {
if (u.f != parent && !marked[u.f])
add(u.f, node, max(mx, u.s), len+1);
}
}
void solve(int node, int parent) {
int n = get_sz(node, parent);
int centroid = find_centro(node, parent, n);
marked[centroid] = true;
memset(fen, 0, sizeof(fen));
memset(fen_len, 0, sizeof(fen_len));
update(0, 1);
update_len(0, 1);
for (auto u : graph[centroid]) {
if (!marked[u.f]) {
eval(u.f, centroid, u.s, 1);
add(u.f, centroid, u.s, 1);
}
}
for (auto u : graph[centroid]) {
if (u.f != parent && !marked[u.f])
solve(u.f, node);
}
}
signed main() {
fast
int n;
cin >> n >> k;
graph = vector<vector<pair<int,int>>>(n+1, vector<pair<int,int>>());
for (int i = 1; i < n; i++) {
int x, y, w;
cin >> x >> y >> w;
graph[x].push_back({y, w});
graph[y].push_back({x, w});
}
solve(1, 1);
cout << ans * 2 << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
3 ms |
2648 KB |
Output is correct |
4 |
Correct |
50 ms |
2900 KB |
Output is correct |
5 |
Correct |
37 ms |
2648 KB |
Output is correct |
6 |
Correct |
44 ms |
2820 KB |
Output is correct |
7 |
Correct |
38 ms |
2848 KB |
Output is correct |
8 |
Correct |
43 ms |
2652 KB |
Output is correct |
9 |
Correct |
25 ms |
2652 KB |
Output is correct |
10 |
Incorrect |
24 ms |
2652 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
3 ms |
2652 KB |
Output is correct |
4 |
Correct |
50 ms |
2836 KB |
Output is correct |
5 |
Execution timed out |
1565 ms |
4300 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
3 ms |
2648 KB |
Output is correct |
4 |
Correct |
50 ms |
2900 KB |
Output is correct |
5 |
Correct |
37 ms |
2648 KB |
Output is correct |
6 |
Correct |
44 ms |
2820 KB |
Output is correct |
7 |
Correct |
38 ms |
2848 KB |
Output is correct |
8 |
Correct |
43 ms |
2652 KB |
Output is correct |
9 |
Correct |
25 ms |
2652 KB |
Output is correct |
10 |
Incorrect |
24 ms |
2652 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |