#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
struct Edge{
int v, c;
Edge(int _v, int _c){
v = _v;
c = _c;
}
};
struct Bit{
int d[N];
void update(int x, int val){
for(; x < N; x += x & -x)
d[x] += val;
}
int get(int x){
int res = 0;
for(; x > 0; x -= x & -x)
res += d[x];
return res;
}
};
int n, k, sz[N];
vector<Edge> adj[N];
ll answer = 0;
Bit bit;
bool done[N];
void DfsForCentroid(int u, int p){
sz[u] = 1;
for(Edge e : adj[u]){
int v = e.v;
if (v == p || done[v])
continue;
DfsForCentroid(v, u);
sz[u] += sz[v];
}
}
int FindCentroid(int u, int p, int treeSize){
for(Edge e : adj[u]){
int v = e.v;
if (v == p || done[v])
continue;
if (sz[v] * 2 > treeSize)
return FindCentroid(v, u, treeSize);
}
return u;
}
void Dfs(int u, int p, int len, int maxW, vector<pair<int, int>> &a){
if (done[u])
return;
a.push_back({maxW, len});
for(Edge e : adj[u]){
int v = e.v;
int c = e.c;
if (v == p)
continue;
Dfs(v, u, len + 1, max(maxW, c), a);
}
}
ll Calc(vector<pair<int, int>> &a){
ll res = 0;
sort(a.begin(), a.end());
for(auto p : a){
res += bit.get(p.first - p.second);
bit.update(p.second + k, 1);
}
for(auto p : a)
bit.update(p.second + k, -1);
return res;
}
void Solve(int u){
if (done[u])
return;
DfsForCentroid(u, -1);
u = FindCentroid(u, -1, sz[u]);
vector<pair<int, int>> a;
Dfs(u, -1, 0, 0, a);
answer += Calc(a);
for(Edge e : adj[u]){
a.clear();
int v = e.v;
int c = e.c;
Dfs(v, u, 1, c, a);
answer -= Calc(a);
}
done[u] = 1;
for(Edge e : adj[u])
Solve(e.v);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for(int i = 1; i < n; i++){
int u, v, c;
cin >> u >> v >> c;
adj[u].push_back(Edge(v, c));
adj[v].push_back(Edge(u, c));
}
Solve(1);
cout << answer * 2;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
3 ms |
2668 KB |
Output is correct |
4 |
Correct |
5 ms |
2796 KB |
Output is correct |
5 |
Correct |
5 ms |
2796 KB |
Output is correct |
6 |
Correct |
5 ms |
2796 KB |
Output is correct |
7 |
Correct |
5 ms |
2796 KB |
Output is correct |
8 |
Correct |
5 ms |
2796 KB |
Output is correct |
9 |
Correct |
4 ms |
2796 KB |
Output is correct |
10 |
Correct |
4 ms |
2796 KB |
Output is correct |
11 |
Correct |
4 ms |
2796 KB |
Output is correct |
12 |
Correct |
5 ms |
2796 KB |
Output is correct |
13 |
Correct |
6 ms |
2796 KB |
Output is correct |
14 |
Correct |
6 ms |
2796 KB |
Output is correct |
15 |
Correct |
5 ms |
2796 KB |
Output is correct |
16 |
Correct |
5 ms |
2796 KB |
Output is correct |
17 |
Correct |
4 ms |
2796 KB |
Output is correct |
18 |
Correct |
4 ms |
2796 KB |
Output is correct |
19 |
Correct |
5 ms |
2796 KB |
Output is correct |
20 |
Correct |
4 ms |
2796 KB |
Output is correct |
21 |
Correct |
4 ms |
2796 KB |
Output is correct |
22 |
Correct |
4 ms |
2796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2668 KB |
Output is correct |
2 |
Correct |
3 ms |
2668 KB |
Output is correct |
3 |
Correct |
3 ms |
2688 KB |
Output is correct |
4 |
Correct |
5 ms |
2796 KB |
Output is correct |
5 |
Correct |
31 ms |
4204 KB |
Output is correct |
6 |
Correct |
171 ms |
10216 KB |
Output is correct |
7 |
Correct |
240 ms |
17436 KB |
Output is correct |
8 |
Correct |
425 ms |
17888 KB |
Output is correct |
9 |
Correct |
253 ms |
17400 KB |
Output is correct |
10 |
Correct |
431 ms |
17888 KB |
Output is correct |
11 |
Correct |
345 ms |
17496 KB |
Output is correct |
12 |
Correct |
365 ms |
17896 KB |
Output is correct |
13 |
Correct |
372 ms |
17496 KB |
Output is correct |
14 |
Correct |
394 ms |
17860 KB |
Output is correct |
15 |
Correct |
392 ms |
17632 KB |
Output is correct |
16 |
Correct |
372 ms |
17880 KB |
Output is correct |
17 |
Correct |
380 ms |
17760 KB |
Output is correct |
18 |
Correct |
381 ms |
17888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
3 ms |
2668 KB |
Output is correct |
4 |
Correct |
5 ms |
2796 KB |
Output is correct |
5 |
Correct |
5 ms |
2796 KB |
Output is correct |
6 |
Correct |
5 ms |
2796 KB |
Output is correct |
7 |
Correct |
5 ms |
2796 KB |
Output is correct |
8 |
Correct |
5 ms |
2796 KB |
Output is correct |
9 |
Correct |
4 ms |
2796 KB |
Output is correct |
10 |
Correct |
4 ms |
2796 KB |
Output is correct |
11 |
Correct |
4 ms |
2796 KB |
Output is correct |
12 |
Correct |
5 ms |
2796 KB |
Output is correct |
13 |
Correct |
6 ms |
2796 KB |
Output is correct |
14 |
Correct |
6 ms |
2796 KB |
Output is correct |
15 |
Correct |
5 ms |
2796 KB |
Output is correct |
16 |
Correct |
5 ms |
2796 KB |
Output is correct |
17 |
Correct |
4 ms |
2796 KB |
Output is correct |
18 |
Correct |
4 ms |
2796 KB |
Output is correct |
19 |
Correct |
5 ms |
2796 KB |
Output is correct |
20 |
Correct |
4 ms |
2796 KB |
Output is correct |
21 |
Correct |
4 ms |
2796 KB |
Output is correct |
22 |
Correct |
4 ms |
2796 KB |
Output is correct |
23 |
Correct |
3 ms |
2668 KB |
Output is correct |
24 |
Correct |
3 ms |
2668 KB |
Output is correct |
25 |
Correct |
3 ms |
2688 KB |
Output is correct |
26 |
Correct |
5 ms |
2796 KB |
Output is correct |
27 |
Correct |
31 ms |
4204 KB |
Output is correct |
28 |
Correct |
171 ms |
10216 KB |
Output is correct |
29 |
Correct |
240 ms |
17436 KB |
Output is correct |
30 |
Correct |
425 ms |
17888 KB |
Output is correct |
31 |
Correct |
253 ms |
17400 KB |
Output is correct |
32 |
Correct |
431 ms |
17888 KB |
Output is correct |
33 |
Correct |
345 ms |
17496 KB |
Output is correct |
34 |
Correct |
365 ms |
17896 KB |
Output is correct |
35 |
Correct |
372 ms |
17496 KB |
Output is correct |
36 |
Correct |
394 ms |
17860 KB |
Output is correct |
37 |
Correct |
392 ms |
17632 KB |
Output is correct |
38 |
Correct |
372 ms |
17880 KB |
Output is correct |
39 |
Correct |
380 ms |
17760 KB |
Output is correct |
40 |
Correct |
381 ms |
17888 KB |
Output is correct |
41 |
Correct |
3 ms |
2668 KB |
Output is correct |
42 |
Correct |
236 ms |
17240 KB |
Output is correct |
43 |
Correct |
427 ms |
17888 KB |
Output is correct |
44 |
Correct |
269 ms |
17248 KB |
Output is correct |
45 |
Correct |
432 ms |
17760 KB |
Output is correct |
46 |
Correct |
336 ms |
17376 KB |
Output is correct |
47 |
Correct |
392 ms |
17964 KB |
Output is correct |
48 |
Correct |
365 ms |
17496 KB |
Output is correct |
49 |
Correct |
383 ms |
18008 KB |
Output is correct |
50 |
Correct |
391 ms |
17632 KB |
Output is correct |
51 |
Correct |
410 ms |
18016 KB |
Output is correct |
52 |
Correct |
184 ms |
9828 KB |
Output is correct |
53 |
Correct |
157 ms |
10084 KB |
Output is correct |
54 |
Correct |
121 ms |
9828 KB |
Output is correct |
55 |
Correct |
209 ms |
10084 KB |
Output is correct |
56 |
Correct |
166 ms |
10084 KB |
Output is correct |
57 |
Correct |
350 ms |
10092 KB |
Output is correct |
58 |
Correct |
353 ms |
10344 KB |
Output is correct |
59 |
Correct |
404 ms |
10600 KB |
Output is correct |
60 |
Correct |
414 ms |
10664 KB |
Output is correct |
61 |
Correct |
369 ms |
10436 KB |
Output is correct |
62 |
Correct |
299 ms |
9952 KB |
Output is correct |
63 |
Correct |
307 ms |
10284 KB |
Output is correct |
64 |
Correct |
384 ms |
10316 KB |
Output is correct |
65 |
Correct |
13 ms |
3052 KB |
Output is correct |
66 |
Correct |
3 ms |
2668 KB |
Output is correct |