#include<bits/stdc++.h>
using namespace std;
#define range(v) begin(v), end(v)
#define compact(v) v.erase(unique(range(v)), end(v))
template<class T> bool minimize(T& a, T b){
if(a > b) return a = b, true;
return false;
}
template<class T> bool maximize(T& a, T b){
if(a < b) return a = b, true;
return false;
}
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int N = 2e5 + 5;
const int inf = 1e9;
int n, k, ans = inf, sz[N];
bool del[N];
vector<pair<int, int>> g[N];
int dfsSize(int u, int e){
sz[u] = 1;
for(auto [v, w] : g[u]) if(v != e and !del[v]){
sz[u] += dfsSize(v, u);
}
return sz[u];
}
int getCentroid(int u, int e, int target){
for(auto [v, w] : g[u]) if(v != e and !del[v] and sz[v] * 2 > target){
return getCentroid(v, u, target);
}
return u;
}
map<int, int> cnt;
stack<pair<int, int>> op;
void init(){
cnt.clear();
}
void add(int w, int l){
if(cnt.find(w) == cnt.end()) cnt[w] = l;
else minimize(cnt[w], l);
}
int query(int target){
if(target == 0) return 0;
if(cnt.find(target) == cnt.end()) return inf;
return cnt[target];
}
void dfs(int u, int e, int sumW, int sumL){
if(sumW > k) return;
op.push({sumW, sumL});
minimize(ans, query(k - sumW) + sumL);
for(auto [v, w] : g[u]) if(v != e and !del[v]){
dfs(v, u, sumW + w, sumL + 1);
}
}
void decompose(int u, int e){
int c = getCentroid(u, e, dfsSize(u, e));
del[c] = true;
init();
for(auto [v, w] : g[c]) if(!del[v]){
dfs(v, c, w, 1);
while(!op.empty()){
add(op.top().first, op.top().second);
op.pop();
}
}
for(auto [v, w] : g[c]) if(!del[v]){
decompose(v, c);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N, k = K;
for(int i = 0; i + 1 < n; ++i){
int u = H[i][0], v = H[i][1], w = L[i];
g[u].push_back({v, w});
g[v].push_back({u, w});
}
decompose(0, 0);
return (ans == inf ? -1 : ans);
}
//void Zero_OP(){
// cin >> n >> k;
// for(int i = 1; i < n; ++i){
// int u, v, w;
// cin >> u >> v >> w;
// g[u].push_back({v, w});
// g[v].push_back({u, w});
// }
//
// decompose(0, 0);
// cout << (ans == inf ? -1 : ans) << '\n';
//}
//
//int main(){
// ios_base::sync_with_stdio(0);
// cin.tie(0);
//
// #define task "antuvu"
// if(fopen(task".inp", "r")){
// freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
// }
//
// Zero_OP();
//
// return 0;
//}
Compilation message
race.cpp: In function 'int dfsSize(int, int)':
race.cpp:31:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
31 | for(auto [v, w] : g[u]) if(v != e and !del[v]){
| ^
race.cpp: In function 'int getCentroid(int, int, int)':
race.cpp:38:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
38 | for(auto [v, w] : g[u]) if(v != e and !del[v] and sz[v] * 2 > target){
| ^
race.cpp: In function 'void dfs(int, int, int, int)':
race.cpp:66:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
66 | for(auto [v, w] : g[u]) if(v != e and !del[v]){
| ^
race.cpp: In function 'void decompose(int, int)':
race.cpp:75:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
75 | for(auto [v, w] : g[c]) if(!del[v]){
| ^
race.cpp:83:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
83 | for(auto [v, w] : g[c]) if(!del[v]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8624 KB |
Output is correct |
20 |
Correct |
4 ms |
8636 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
2 ms |
8796 KB |
Output is correct |
23 |
Correct |
2 ms |
8796 KB |
Output is correct |
24 |
Correct |
3 ms |
8796 KB |
Output is correct |
25 |
Correct |
2 ms |
8796 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
3 ms |
9048 KB |
Output is correct |
29 |
Correct |
2 ms |
8792 KB |
Output is correct |
30 |
Correct |
3 ms |
8876 KB |
Output is correct |
31 |
Correct |
3 ms |
8632 KB |
Output is correct |
32 |
Correct |
3 ms |
8796 KB |
Output is correct |
33 |
Correct |
3 ms |
8796 KB |
Output is correct |
34 |
Correct |
2 ms |
8796 KB |
Output is correct |
35 |
Correct |
2 ms |
8796 KB |
Output is correct |
36 |
Correct |
2 ms |
8788 KB |
Output is correct |
37 |
Correct |
3 ms |
8796 KB |
Output is correct |
38 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
116 ms |
15196 KB |
Output is correct |
20 |
Correct |
113 ms |
15168 KB |
Output is correct |
21 |
Correct |
134 ms |
15344 KB |
Output is correct |
22 |
Correct |
116 ms |
15656 KB |
Output is correct |
23 |
Correct |
53 ms |
15388 KB |
Output is correct |
24 |
Correct |
48 ms |
15096 KB |
Output is correct |
25 |
Correct |
91 ms |
17720 KB |
Output is correct |
26 |
Correct |
73 ms |
21176 KB |
Output is correct |
27 |
Correct |
141 ms |
19520 KB |
Output is correct |
28 |
Correct |
148 ms |
33764 KB |
Output is correct |
29 |
Correct |
159 ms |
32876 KB |
Output is correct |
30 |
Correct |
126 ms |
22420 KB |
Output is correct |
31 |
Correct |
130 ms |
22228 KB |
Output is correct |
32 |
Correct |
123 ms |
22356 KB |
Output is correct |
33 |
Correct |
158 ms |
21076 KB |
Output is correct |
34 |
Correct |
100 ms |
22180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8624 KB |
Output is correct |
20 |
Correct |
4 ms |
8636 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
2 ms |
8796 KB |
Output is correct |
23 |
Correct |
2 ms |
8796 KB |
Output is correct |
24 |
Correct |
3 ms |
8796 KB |
Output is correct |
25 |
Correct |
2 ms |
8796 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
3 ms |
9048 KB |
Output is correct |
29 |
Correct |
2 ms |
8792 KB |
Output is correct |
30 |
Correct |
3 ms |
8876 KB |
Output is correct |
31 |
Correct |
3 ms |
8632 KB |
Output is correct |
32 |
Correct |
3 ms |
8796 KB |
Output is correct |
33 |
Correct |
3 ms |
8796 KB |
Output is correct |
34 |
Correct |
2 ms |
8796 KB |
Output is correct |
35 |
Correct |
2 ms |
8796 KB |
Output is correct |
36 |
Correct |
2 ms |
8788 KB |
Output is correct |
37 |
Correct |
3 ms |
8796 KB |
Output is correct |
38 |
Correct |
2 ms |
8796 KB |
Output is correct |
39 |
Correct |
116 ms |
15196 KB |
Output is correct |
40 |
Correct |
113 ms |
15168 KB |
Output is correct |
41 |
Correct |
134 ms |
15344 KB |
Output is correct |
42 |
Correct |
116 ms |
15656 KB |
Output is correct |
43 |
Correct |
53 ms |
15388 KB |
Output is correct |
44 |
Correct |
48 ms |
15096 KB |
Output is correct |
45 |
Correct |
91 ms |
17720 KB |
Output is correct |
46 |
Correct |
73 ms |
21176 KB |
Output is correct |
47 |
Correct |
141 ms |
19520 KB |
Output is correct |
48 |
Correct |
148 ms |
33764 KB |
Output is correct |
49 |
Correct |
159 ms |
32876 KB |
Output is correct |
50 |
Correct |
126 ms |
22420 KB |
Output is correct |
51 |
Correct |
130 ms |
22228 KB |
Output is correct |
52 |
Correct |
123 ms |
22356 KB |
Output is correct |
53 |
Correct |
158 ms |
21076 KB |
Output is correct |
54 |
Correct |
100 ms |
22180 KB |
Output is correct |
55 |
Correct |
14 ms |
9520 KB |
Output is correct |
56 |
Correct |
10 ms |
9404 KB |
Output is correct |
57 |
Correct |
94 ms |
16936 KB |
Output is correct |
58 |
Correct |
35 ms |
16324 KB |
Output is correct |
59 |
Correct |
197 ms |
25680 KB |
Output is correct |
60 |
Correct |
513 ms |
41436 KB |
Output is correct |
61 |
Correct |
240 ms |
22812 KB |
Output is correct |
62 |
Correct |
179 ms |
22612 KB |
Output is correct |
63 |
Correct |
194 ms |
22688 KB |
Output is correct |
64 |
Correct |
582 ms |
27472 KB |
Output is correct |
65 |
Correct |
108 ms |
23124 KB |
Output is correct |
66 |
Correct |
363 ms |
31220 KB |
Output is correct |
67 |
Correct |
80 ms |
22980 KB |
Output is correct |
68 |
Correct |
250 ms |
26728 KB |
Output is correct |
69 |
Correct |
276 ms |
26892 KB |
Output is correct |
70 |
Correct |
241 ms |
26200 KB |
Output is correct |