// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 1e5 + 5;
const int inf = 1e9 + 7;
int n = 5, k = 1, sz[maxn], fen[maxn];
ll ans;
vector<pii> adj[maxn], vecf;
bool mark[maxn];
void upd(int i, int x){
for(; i <= n; i += i & -i) fen[i] += x;
}
int get(int i){
int res = 0;
for(; i; i -= i & -i) res += fen[i];
return res;
}
ll calc(vector<pii> vec){
vector<int> h;
h.pb(-inf);
sort(all(vec));
for(auto u: vec) h.pb(u.S);
sort(all(h));
h.resize(unique(all(h)) - h.begin());
for(int i = 0; i < vec.size(); i++){
int x = lower_bound(all(h), vec[i].S) - h.begin();
upd(x, +1);
}
ll res = 0;
for(int i = vec.size() - 1; i >= 0; i--){
int x = lower_bound(all(h), vec[i].S) - h.begin();
upd(x, -1);
int j = upper_bound(all(h), vec[i].F - k - vec[i].S) - h.begin();
j--;
res += get(j);
}
return res;
}
int get_sz(int v, int p = -1){
sz[v] = 1;
for(auto [u, w]: adj[v]){
if(u != p && !mark[u]) sz[v] += get_sz(u, v);
}
return sz[v];
}
int find(int v, int p, int s){
for(auto [u, w]: adj[v]) if(u != p && !mark[u] && 2 * sz[u] > s) return find(u, v, s);
return v;
}
void dfs(int v, int p, int h, int mx){
vecf.pb(mp(mx, h));
if(mx - k - h >= 0) ans++;
for(auto [u, w]: adj[v]){
if(u != p && !mark[u]){
dfs(u, v, h + 1, max(mx, w));
}
}
}
void cd(int v){
v = find(v, -1, get_sz(v));
vector<pii> all;
for(auto [u, w]: adj[v]){
if(mark[u]) continue;
vecf.clear();
dfs(u, v, 1, w);
ans -= calc(vecf);
for(auto u: vecf) all.pb(u);
}
ans += calc(all);
mark[v] = 1;
for(auto [u, w]: adj[v]) if(!mark[u]) cd(u);
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
for(int i = 0; i < n - 1; i++){
int u, v, w; cin >> u >> v >> w;
adj[u].pb(mp(v, w));
adj[v].pb(mp(u, w));
}
cd(1);
cout << ans * 2 << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'll calc(std::vector<std::pair<int, int> >)':
Main.cpp:46:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i = 0; i < vec.size(); i++){
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2904 KB |
Output is correct |
5 |
Correct |
3 ms |
2908 KB |
Output is correct |
6 |
Correct |
3 ms |
2940 KB |
Output is correct |
7 |
Correct |
3 ms |
2904 KB |
Output is correct |
8 |
Correct |
3 ms |
2904 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
3 ms |
2648 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2904 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2648 KB |
Output is correct |
21 |
Correct |
2 ms |
2896 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2908 KB |
Output is correct |
5 |
Correct |
37 ms |
3976 KB |
Output is correct |
6 |
Correct |
220 ms |
9096 KB |
Output is correct |
7 |
Correct |
459 ms |
15668 KB |
Output is correct |
8 |
Correct |
476 ms |
15660 KB |
Output is correct |
9 |
Correct |
478 ms |
15660 KB |
Output is correct |
10 |
Correct |
472 ms |
15656 KB |
Output is correct |
11 |
Correct |
454 ms |
15664 KB |
Output is correct |
12 |
Correct |
468 ms |
15420 KB |
Output is correct |
13 |
Correct |
490 ms |
15660 KB |
Output is correct |
14 |
Correct |
477 ms |
15664 KB |
Output is correct |
15 |
Correct |
475 ms |
15664 KB |
Output is correct |
16 |
Correct |
499 ms |
15676 KB |
Output is correct |
17 |
Correct |
491 ms |
15516 KB |
Output is correct |
18 |
Correct |
485 ms |
15548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2904 KB |
Output is correct |
5 |
Correct |
3 ms |
2908 KB |
Output is correct |
6 |
Correct |
3 ms |
2940 KB |
Output is correct |
7 |
Correct |
3 ms |
2904 KB |
Output is correct |
8 |
Correct |
3 ms |
2904 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
3 ms |
2648 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2904 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2648 KB |
Output is correct |
21 |
Correct |
2 ms |
2896 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
37 ms |
3976 KB |
Output is correct |
28 |
Correct |
220 ms |
9096 KB |
Output is correct |
29 |
Correct |
459 ms |
15668 KB |
Output is correct |
30 |
Correct |
476 ms |
15660 KB |
Output is correct |
31 |
Correct |
478 ms |
15660 KB |
Output is correct |
32 |
Correct |
472 ms |
15656 KB |
Output is correct |
33 |
Correct |
454 ms |
15664 KB |
Output is correct |
34 |
Correct |
468 ms |
15420 KB |
Output is correct |
35 |
Correct |
490 ms |
15660 KB |
Output is correct |
36 |
Correct |
477 ms |
15664 KB |
Output is correct |
37 |
Correct |
475 ms |
15664 KB |
Output is correct |
38 |
Correct |
499 ms |
15676 KB |
Output is correct |
39 |
Correct |
491 ms |
15516 KB |
Output is correct |
40 |
Correct |
485 ms |
15548 KB |
Output is correct |
41 |
Correct |
1 ms |
2652 KB |
Output is correct |
42 |
Correct |
470 ms |
15544 KB |
Output is correct |
43 |
Correct |
466 ms |
15808 KB |
Output is correct |
44 |
Correct |
468 ms |
15664 KB |
Output is correct |
45 |
Correct |
492 ms |
15832 KB |
Output is correct |
46 |
Correct |
493 ms |
15652 KB |
Output is correct |
47 |
Correct |
466 ms |
15520 KB |
Output is correct |
48 |
Correct |
463 ms |
15660 KB |
Output is correct |
49 |
Correct |
499 ms |
15556 KB |
Output is correct |
50 |
Correct |
479 ms |
15664 KB |
Output is correct |
51 |
Correct |
484 ms |
15664 KB |
Output is correct |
52 |
Correct |
114 ms |
9760 KB |
Output is correct |
53 |
Correct |
120 ms |
9984 KB |
Output is correct |
54 |
Correct |
92 ms |
9736 KB |
Output is correct |
55 |
Correct |
109 ms |
9804 KB |
Output is correct |
56 |
Correct |
114 ms |
9944 KB |
Output is correct |
57 |
Correct |
262 ms |
9816 KB |
Output is correct |
58 |
Correct |
257 ms |
9672 KB |
Output is correct |
59 |
Correct |
236 ms |
9832 KB |
Output is correct |
60 |
Correct |
274 ms |
9748 KB |
Output is correct |
61 |
Correct |
279 ms |
9792 KB |
Output is correct |
62 |
Correct |
214 ms |
9720 KB |
Output is correct |
63 |
Correct |
229 ms |
9688 KB |
Output is correct |
64 |
Correct |
267 ms |
9804 KB |
Output is correct |
65 |
Correct |
9 ms |
3164 KB |
Output is correct |
66 |
Correct |
1 ms |
2652 KB |
Output is correct |