#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define file "input"
template<class T> bool minimize(T &a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> bool maximize(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }
void setIO() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(file".inp", "r")) {
freopen(file".inp", "r", stdin);
freopen(file".out", "w", stdout);
}
}
const int N = 1e5+5;
int n, k;
ll ans;
vector<ii> adj[N];
// centroid
int cent, tree_size, max_dep;
struct fenwick {
int tmp;
vector<int> bit;
void init(int n) {
tmp = n;
bit = vector<int>(n+2, 0);
}
void update(int i, int v) {
assert(i <= tmp);
for(++i; i<=tmp+1; i+=i&-i) bit[i] += v;
}
int query(int i) {
i = min(tmp, i);
int ans = 0;
for(++i; i>=1; i-=i&-i) ans += bit[i];
return ans;
}
} branch[N], al;
int high[N], sub[N], who[N];
bool process[N];
int get_subsize(int u, int p) {
sub[u] = 1;
fore(x, adj[u]) {
int v = x.fi;
if (v == p || process[v]) continue;
sub[u] += get_subsize(v, u);
}
return sub[u];
}
int get_centroid(int u, int p) {
fore(x, adj[u]) {
int v = x.fi;
if (v == p || process[v]) continue;
if (sub[v] * 2 > tree_size) return get_centroid(v, u);
}
return u;
}
vector<ii> cands;
int dfs_prep(int u, int p, int mark, int mx = 0) {
if (mx - high[u] >= k) ++ans;
if (u != cent) {
int ans = 0;
who[u] = mark;
fore(x, adj[u]) {
int v = x.fi, w = x.se;
if (v == p || process[v]) continue;
cands.emplace_back(max(mx, w), v);
high[v] = high[u] + 1;
maximize(ans, dfs_prep(v, u, mark, max(mx, w)) + 1);
}
return ans;
}
else {
int ans = 0;
fore(x, adj[u]) {
int v = x.fi, w = x.se;
if (v == p || process[v]) continue;
cands.emplace_back(max(mx, w), v);
high[v] = high[u] + 1;
int tmp = dfs_prep(v, u, mark, max(mx, w));
maximize(ans, tmp + 1);
branch[mark].init(tmp+1);
++mark;
}
return ans;
}
}
void solve(int u) {
tree_size = get_subsize(u, 0);
cent = get_centroid(u, 0);
cands.clear();
high[cent] = 0;
max_dep = dfs_prep(cent, cent, 0);
al.init(max_dep);
// cout << ans << "\n";
sort(all(cands));
fore(x, cands) {
int j = x.fi - high[x.se] - k;
if (j >= 0) ans += al.query(j) - branch[who[x.se]].query(j);
// if (x.se == 2) cout << al.query(j) << "\n";
// cout << x.fi - high[x.se] - k << "\n";
al.update(high[x.se], 1);
branch[who[x.se]].update(high[x.se], 1);
}
// cout << cent << " " << ans << "\n";
process[cent] = 1;
fore(x, adj[cent]) {
int v = x.fi, w = x.se;
if (process[v]) continue;
solve(v);
}
}
int main() {
setIO();
cin >> n >> k;
foru(i, 1, n-1) {
int u, v, w;
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
solve(1);
cout << ans*2;
}
/*
5 2
1 2 5
2 3 2
1 4 3
3 5 4
*/
Compilation message
Main.cpp: In function 'void solve(int)':
Main.cpp:134:19: warning: unused variable 'w' [-Wunused-variable]
134 | int v = x.fi, w = x.se;
| ^
Main.cpp: In function 'void setIO()':
Main.cpp:22:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | freopen(file".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:23:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | freopen(file".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7000 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
2 ms |
7048 KB |
Output is correct |
5 |
Correct |
2 ms |
7004 KB |
Output is correct |
6 |
Correct |
2 ms |
7172 KB |
Output is correct |
7 |
Correct |
2 ms |
7168 KB |
Output is correct |
8 |
Correct |
3 ms |
7000 KB |
Output is correct |
9 |
Correct |
2 ms |
7004 KB |
Output is correct |
10 |
Correct |
2 ms |
7004 KB |
Output is correct |
11 |
Correct |
2 ms |
7004 KB |
Output is correct |
12 |
Correct |
2 ms |
7008 KB |
Output is correct |
13 |
Correct |
2 ms |
7004 KB |
Output is correct |
14 |
Correct |
3 ms |
7004 KB |
Output is correct |
15 |
Correct |
2 ms |
7004 KB |
Output is correct |
16 |
Correct |
2 ms |
7004 KB |
Output is correct |
17 |
Correct |
2 ms |
7004 KB |
Output is correct |
18 |
Correct |
3 ms |
7004 KB |
Output is correct |
19 |
Correct |
2 ms |
7004 KB |
Output is correct |
20 |
Correct |
2 ms |
7072 KB |
Output is correct |
21 |
Correct |
3 ms |
7256 KB |
Output is correct |
22 |
Correct |
2 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7004 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
3 ms |
7004 KB |
Output is correct |
5 |
Correct |
11 ms |
8392 KB |
Output is correct |
6 |
Correct |
63 ms |
13424 KB |
Output is correct |
7 |
Correct |
124 ms |
19400 KB |
Output is correct |
8 |
Correct |
140 ms |
19976 KB |
Output is correct |
9 |
Correct |
132 ms |
19400 KB |
Output is correct |
10 |
Correct |
141 ms |
19740 KB |
Output is correct |
11 |
Correct |
123 ms |
19400 KB |
Output is correct |
12 |
Correct |
139 ms |
19664 KB |
Output is correct |
13 |
Correct |
128 ms |
19436 KB |
Output is correct |
14 |
Correct |
140 ms |
19656 KB |
Output is correct |
15 |
Correct |
145 ms |
19648 KB |
Output is correct |
16 |
Correct |
141 ms |
19912 KB |
Output is correct |
17 |
Correct |
134 ms |
19764 KB |
Output is correct |
18 |
Correct |
135 ms |
19652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7000 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
2 ms |
7048 KB |
Output is correct |
5 |
Correct |
2 ms |
7004 KB |
Output is correct |
6 |
Correct |
2 ms |
7172 KB |
Output is correct |
7 |
Correct |
2 ms |
7168 KB |
Output is correct |
8 |
Correct |
3 ms |
7000 KB |
Output is correct |
9 |
Correct |
2 ms |
7004 KB |
Output is correct |
10 |
Correct |
2 ms |
7004 KB |
Output is correct |
11 |
Correct |
2 ms |
7004 KB |
Output is correct |
12 |
Correct |
2 ms |
7008 KB |
Output is correct |
13 |
Correct |
2 ms |
7004 KB |
Output is correct |
14 |
Correct |
3 ms |
7004 KB |
Output is correct |
15 |
Correct |
2 ms |
7004 KB |
Output is correct |
16 |
Correct |
2 ms |
7004 KB |
Output is correct |
17 |
Correct |
2 ms |
7004 KB |
Output is correct |
18 |
Correct |
3 ms |
7004 KB |
Output is correct |
19 |
Correct |
2 ms |
7004 KB |
Output is correct |
20 |
Correct |
2 ms |
7072 KB |
Output is correct |
21 |
Correct |
3 ms |
7256 KB |
Output is correct |
22 |
Correct |
2 ms |
7004 KB |
Output is correct |
23 |
Correct |
2 ms |
7004 KB |
Output is correct |
24 |
Correct |
1 ms |
7004 KB |
Output is correct |
25 |
Correct |
1 ms |
7004 KB |
Output is correct |
26 |
Correct |
3 ms |
7004 KB |
Output is correct |
27 |
Correct |
11 ms |
8392 KB |
Output is correct |
28 |
Correct |
63 ms |
13424 KB |
Output is correct |
29 |
Correct |
124 ms |
19400 KB |
Output is correct |
30 |
Correct |
140 ms |
19976 KB |
Output is correct |
31 |
Correct |
132 ms |
19400 KB |
Output is correct |
32 |
Correct |
141 ms |
19740 KB |
Output is correct |
33 |
Correct |
123 ms |
19400 KB |
Output is correct |
34 |
Correct |
139 ms |
19664 KB |
Output is correct |
35 |
Correct |
128 ms |
19436 KB |
Output is correct |
36 |
Correct |
140 ms |
19656 KB |
Output is correct |
37 |
Correct |
145 ms |
19648 KB |
Output is correct |
38 |
Correct |
141 ms |
19912 KB |
Output is correct |
39 |
Correct |
134 ms |
19764 KB |
Output is correct |
40 |
Correct |
135 ms |
19652 KB |
Output is correct |
41 |
Correct |
1 ms |
7000 KB |
Output is correct |
42 |
Correct |
124 ms |
19348 KB |
Output is correct |
43 |
Correct |
138 ms |
20032 KB |
Output is correct |
44 |
Correct |
139 ms |
19408 KB |
Output is correct |
45 |
Correct |
148 ms |
19736 KB |
Output is correct |
46 |
Correct |
149 ms |
19364 KB |
Output is correct |
47 |
Correct |
137 ms |
19776 KB |
Output is correct |
48 |
Correct |
129 ms |
19476 KB |
Output is correct |
49 |
Correct |
148 ms |
19780 KB |
Output is correct |
50 |
Correct |
141 ms |
19604 KB |
Output is correct |
51 |
Correct |
143 ms |
19608 KB |
Output is correct |
52 |
Correct |
60 ms |
13768 KB |
Output is correct |
53 |
Correct |
64 ms |
14096 KB |
Output is correct |
54 |
Correct |
55 ms |
13512 KB |
Output is correct |
55 |
Correct |
64 ms |
13876 KB |
Output is correct |
56 |
Correct |
63 ms |
14044 KB |
Output is correct |
57 |
Correct |
132 ms |
13276 KB |
Output is correct |
58 |
Correct |
144 ms |
13524 KB |
Output is correct |
59 |
Correct |
132 ms |
13772 KB |
Output is correct |
60 |
Correct |
134 ms |
13524 KB |
Output is correct |
61 |
Correct |
135 ms |
13732 KB |
Output is correct |
62 |
Correct |
106 ms |
13264 KB |
Output is correct |
63 |
Correct |
118 ms |
13512 KB |
Output is correct |
64 |
Correct |
130 ms |
14028 KB |
Output is correct |
65 |
Correct |
6 ms |
7664 KB |
Output is correct |
66 |
Correct |
1 ms |
7004 KB |
Output is correct |