#include <bits/stdc++.h>
using namespace std;
/*<DEBUG>*/
#define tem template <typename
#define can_shift(_X_, ...) enable_if_t<sizeof test<_X_>(0) __VA_ARGS__ 8, debug&> operator<<(T i)
#define _op debug& operator<<
tem C > auto test(C *x) -> decltype(cerr << *x, 0LL);
tem C > char test(...);
tem C > struct itr{C begin, end; };
tem C > itr<C> get_range(C b, C e) { return itr<C>{b, e}; }
struct debug{
#ifdef _LOCAL
~debug(){ cerr << endl; }
tem T > can_shift(T, ==){ cerr << boolalpha << i; return *this; }
tem T> can_shift(T, !=){ return *this << get_range(begin(i), end(i)); }
tem T, typename U > _op (pair<T, U> i){
return *this << "< " << i.first << " , " << i.second << " >"; }
tem T> _op (itr<T> i){
*this << "{ ";
for(auto it = i.begin; it != i.end; it++){
*this << " , " + (it==i.begin?2:0) << *it;
}
return *this << " }";
}
#else
tem T> _op (const T&) { return *this; }
#endif
};
string _ARR_(int* arr, int sz){
string ret = "{ " + to_string(arr[0]);
for(int i = 1; i < sz; i++) ret += " , " + to_string(arr[i]);
ret += " }"; return ret;
}
#define exp(...) " [ " << #__VA_ARGS__ << " : " << (__VA_ARGS__) << " ]"
/*</DEBUG>*/
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> pii;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define TC int __TC__; cin >> __TC__; while(__TC__--)
#define ar array
const int INF = 1e9 + 7, N = 1e5;
/*
*
* This solution could be better (maybe not faster, but consume less memory) by using only one fenwick tree
*
* Im not gonna implement that tho, my solution passes so its ok
*
*/
int n, k, blocked[N], sz[N], d[N];
ll ans = 0;
vector<ar<int,2>> adj[N];
vector<ar<int,3>> ord[N];
int id=0, par[N], iter = 0, upper;
unordered_map<int, int> mx, fen[2*N];
void add(int i, int j){
for(; i <= upper+1; i += i&-i){
++fen[j][i];
}
}
int get(int i, int j){
int ret = 0;
for(; i > 0; i -= i&-i){
ret += fen[j][i];
}
return ret;
}
void dfs(int u, int p, int W){
d[u] = d[p] + 1;
upper = max(upper, d[u]);
mx[u] = max(W, mx[p]);
ord[iter].pb({mx[u], d[u], id});
if(mx[u] - d[u] >= k) ++ans;
for(auto [v ,w] : adj[u]){
if(!blocked[v] && v != p){
dfs(v, u, w);
}
}
}
void solve_root(int u){
int all = id++;
d[u] = 0;
mx[u] = 0;
upper = 0;
for(auto [v, W] : adj[u]){
if(blocked[v]) continue;
dfs(v, u, W);
id++;
}
sort(ord[iter].begin(), ord[iter].end());
for(auto [w, l, id] : ord[iter]){
if(w - l - k > 0){
ans += get(min(w-l-k, upper+1), all) - get(min(w-l-k, upper+1), id);
}
add(l, all);
add(l, id);
}
iter++;
return;
}
void getsz(int u, int p){
sz[u] = 1;
par[u] = p;
for(auto [v, w] : adj[u]){
if(v != p && !blocked[v]){
getsz(v, u);
sz[u] += sz[v];
}
}
}
void decomp(int root){
getsz(root, root);
if(sz[root] == 1) return;
ar<int,2> centroid = {INF, -1};
queue<int> q;
q.push(root);
while(q.size()){
int u = q.front();
q.pop();
ar<int,2> mx_subtree = {sz[root] - sz[u], u};
for(auto [v, w] : adj[u]){
if(v != par[u] && !blocked[v]){
q.push(v);
mx_subtree = max(mx_subtree, {sz[v], u});
}
}
centroid = min(centroid, mx_subtree);
}
solve_root(centroid[1]);
// debug() << exp(centroid[1]);
blocked[centroid[1]] = 1;
for(auto [v, w] : adj[centroid[1]]){
if(!blocked[v]) decomp(v);
}
return;
}
int main(void)
{
FAST;
cin >> n >> k;
for(int i = 1; i < n; ++i){
int u, v, w; cin >> u >> v >> w;
--u; --v;
adj[u].pb({v,w});
adj[v].pb({u,w});
}
decomp(0);
cout << ans*2 << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'void dfs(int, int, int)':
Main.cpp:90:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
90 | for(auto [v ,w] : adj[u]){
| ^
Main.cpp: In function 'void solve_root(int)':
Main.cpp:104:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
104 | for(auto [v, W] : adj[u]){
| ^
Main.cpp:112:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
112 | for(auto [w, l, id] : ord[iter]){
| ^
Main.cpp: In function 'void getsz(int, int)':
Main.cpp:126:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
126 | for(auto [v, w] : adj[u]){
| ^
Main.cpp: In function 'void decomp(int)':
Main.cpp:148:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
148 | for(auto [v, w] : adj[u]){
| ^
Main.cpp:164:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
164 | for(auto [v, w] : adj[centroid[1]]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
15980 KB |
Output is correct |
2 |
Correct |
12 ms |
15980 KB |
Output is correct |
3 |
Correct |
12 ms |
16108 KB |
Output is correct |
4 |
Correct |
17 ms |
16876 KB |
Output is correct |
5 |
Correct |
16 ms |
16876 KB |
Output is correct |
6 |
Correct |
17 ms |
16876 KB |
Output is correct |
7 |
Correct |
16 ms |
16876 KB |
Output is correct |
8 |
Correct |
18 ms |
16876 KB |
Output is correct |
9 |
Correct |
15 ms |
16236 KB |
Output is correct |
10 |
Correct |
13 ms |
16236 KB |
Output is correct |
11 |
Correct |
13 ms |
16236 KB |
Output is correct |
12 |
Correct |
14 ms |
16364 KB |
Output is correct |
13 |
Correct |
15 ms |
16364 KB |
Output is correct |
14 |
Correct |
15 ms |
16364 KB |
Output is correct |
15 |
Correct |
14 ms |
16364 KB |
Output is correct |
16 |
Correct |
14 ms |
16364 KB |
Output is correct |
17 |
Correct |
14 ms |
16364 KB |
Output is correct |
18 |
Correct |
14 ms |
16364 KB |
Output is correct |
19 |
Correct |
14 ms |
16364 KB |
Output is correct |
20 |
Correct |
14 ms |
16364 KB |
Output is correct |
21 |
Correct |
14 ms |
16364 KB |
Output is correct |
22 |
Correct |
14 ms |
16364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
15980 KB |
Output is correct |
2 |
Correct |
12 ms |
15980 KB |
Output is correct |
3 |
Correct |
12 ms |
16108 KB |
Output is correct |
4 |
Correct |
16 ms |
16876 KB |
Output is correct |
5 |
Correct |
74 ms |
27244 KB |
Output is correct |
6 |
Correct |
487 ms |
82224 KB |
Output is correct |
7 |
Correct |
850 ms |
153284 KB |
Output is correct |
8 |
Correct |
1025 ms |
155972 KB |
Output is correct |
9 |
Correct |
852 ms |
153268 KB |
Output is correct |
10 |
Correct |
1010 ms |
155844 KB |
Output is correct |
11 |
Correct |
845 ms |
153156 KB |
Output is correct |
12 |
Correct |
1012 ms |
156100 KB |
Output is correct |
13 |
Correct |
846 ms |
153284 KB |
Output is correct |
14 |
Correct |
1000 ms |
155972 KB |
Output is correct |
15 |
Correct |
1006 ms |
155700 KB |
Output is correct |
16 |
Correct |
973 ms |
155940 KB |
Output is correct |
17 |
Correct |
982 ms |
155924 KB |
Output is correct |
18 |
Correct |
970 ms |
154824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
15980 KB |
Output is correct |
2 |
Correct |
12 ms |
15980 KB |
Output is correct |
3 |
Correct |
12 ms |
16108 KB |
Output is correct |
4 |
Correct |
17 ms |
16876 KB |
Output is correct |
5 |
Correct |
16 ms |
16876 KB |
Output is correct |
6 |
Correct |
17 ms |
16876 KB |
Output is correct |
7 |
Correct |
16 ms |
16876 KB |
Output is correct |
8 |
Correct |
18 ms |
16876 KB |
Output is correct |
9 |
Correct |
15 ms |
16236 KB |
Output is correct |
10 |
Correct |
13 ms |
16236 KB |
Output is correct |
11 |
Correct |
13 ms |
16236 KB |
Output is correct |
12 |
Correct |
14 ms |
16364 KB |
Output is correct |
13 |
Correct |
15 ms |
16364 KB |
Output is correct |
14 |
Correct |
15 ms |
16364 KB |
Output is correct |
15 |
Correct |
14 ms |
16364 KB |
Output is correct |
16 |
Correct |
14 ms |
16364 KB |
Output is correct |
17 |
Correct |
14 ms |
16364 KB |
Output is correct |
18 |
Correct |
14 ms |
16364 KB |
Output is correct |
19 |
Correct |
14 ms |
16364 KB |
Output is correct |
20 |
Correct |
14 ms |
16364 KB |
Output is correct |
21 |
Correct |
14 ms |
16364 KB |
Output is correct |
22 |
Correct |
14 ms |
16364 KB |
Output is correct |
23 |
Correct |
12 ms |
15980 KB |
Output is correct |
24 |
Correct |
12 ms |
15980 KB |
Output is correct |
25 |
Correct |
12 ms |
16108 KB |
Output is correct |
26 |
Correct |
16 ms |
16876 KB |
Output is correct |
27 |
Correct |
74 ms |
27244 KB |
Output is correct |
28 |
Correct |
487 ms |
82224 KB |
Output is correct |
29 |
Correct |
850 ms |
153284 KB |
Output is correct |
30 |
Correct |
1025 ms |
155972 KB |
Output is correct |
31 |
Correct |
852 ms |
153268 KB |
Output is correct |
32 |
Correct |
1010 ms |
155844 KB |
Output is correct |
33 |
Correct |
845 ms |
153156 KB |
Output is correct |
34 |
Correct |
1012 ms |
156100 KB |
Output is correct |
35 |
Correct |
846 ms |
153284 KB |
Output is correct |
36 |
Correct |
1000 ms |
155972 KB |
Output is correct |
37 |
Correct |
1006 ms |
155700 KB |
Output is correct |
38 |
Correct |
973 ms |
155940 KB |
Output is correct |
39 |
Correct |
982 ms |
155924 KB |
Output is correct |
40 |
Correct |
970 ms |
154824 KB |
Output is correct |
41 |
Correct |
12 ms |
15980 KB |
Output is correct |
42 |
Correct |
849 ms |
153284 KB |
Output is correct |
43 |
Correct |
1014 ms |
155924 KB |
Output is correct |
44 |
Correct |
846 ms |
153156 KB |
Output is correct |
45 |
Correct |
1010 ms |
155972 KB |
Output is correct |
46 |
Correct |
849 ms |
153284 KB |
Output is correct |
47 |
Correct |
1025 ms |
156100 KB |
Output is correct |
48 |
Correct |
851 ms |
153284 KB |
Output is correct |
49 |
Correct |
1014 ms |
155972 KB |
Output is correct |
50 |
Correct |
1010 ms |
155460 KB |
Output is correct |
51 |
Correct |
996 ms |
155844 KB |
Output is correct |
52 |
Correct |
285 ms |
41924 KB |
Output is correct |
53 |
Correct |
303 ms |
42308 KB |
Output is correct |
54 |
Correct |
277 ms |
42160 KB |
Output is correct |
55 |
Correct |
295 ms |
42308 KB |
Output is correct |
56 |
Correct |
303 ms |
42540 KB |
Output is correct |
57 |
Correct |
624 ms |
59676 KB |
Output is correct |
58 |
Correct |
639 ms |
61380 KB |
Output is correct |
59 |
Correct |
681 ms |
62020 KB |
Output is correct |
60 |
Correct |
670 ms |
61768 KB |
Output is correct |
61 |
Correct |
730 ms |
61888 KB |
Output is correct |
62 |
Correct |
519 ms |
56516 KB |
Output is correct |
63 |
Correct |
594 ms |
59144 KB |
Output is correct |
64 |
Correct |
597 ms |
59204 KB |
Output is correct |
65 |
Correct |
28 ms |
18028 KB |
Output is correct |
66 |
Correct |
12 ms |
15980 KB |
Output is correct |