#include <iostream>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
typedef long long ll;
ll linf = 1e15+1;
inline void scoobydoobydoo(){
ios::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
const int MAXN = 1e6+7;
bool isVisited[MAXN];
vector<pair<int, int>> adj[MAXN];
bool visited[MAXN];
int subSz[MAXN];
int ans = 1e9+1;
int cnt[MAXN];
int subTreeSz(int node, int parent){
subSz[node] = 1;
for (auto& e : adj[node]){
if (e.first != parent && !isVisited[e.first])subSz[node] += subTreeSz(e.first, node);
}
return subSz[node];
}
int centroidFind(int node, int parent, int target){
for (auto& e : adj[node]){
if (e.first != parent && !isVisited[e.first] && target <= subSz[e.first]){
return centroidFind(e.first, node, target);
}
}
return node;
}
void cleaning(int node, int parent, int w, int k){
if (w > k)return;
cnt[w] = 1e9;
//cout << node << " " << parent << " " << w << endl;
for (auto& e : adj[node]){
if (e.first != parent && !isVisited[e.first]){
cleaning(e.first, node, w+e.second, k);
}
}
}
void bfs(int node, int parent, int k, int w, bool filling, int depth = 1){
if (w > k)return;
if (filling)(cnt[w] = min(cnt[w], depth));
else {
ans = min(ans, cnt[k-w]+depth);
}
//cout << node << " " << parent << " " << k << " " << w << " " << filling << " " << depth << endl;
for (auto& e : adj[node]){
if (e.first != parent && !isVisited[e.first]){
bfs(e.first, node, k, w + e.second, filling, depth+1);
}
}
}
void centroidDecomp(int node, int k){
node = centroidFind(node, -1, subTreeSz(node, -1)/2);
//cout << "CENTROID: " << node << endl;
isVisited[node] = true;
cnt[0] = 0;
for (auto& e : adj[node]){
if (isVisited[e.first])continue;
bfs(e.first, node, k, e.second, 0);
bfs(e.first, node, k, e.second, 1);
}
//cout << "CLEANING" << endl;
cleaning(node, node, 0, k);
//cout << ans << endl;
for (auto& e : adj[node]){
if (!isVisited[e.first])centroidDecomp(e.first, k);
}
}
int best_path(int N, int K, int H[][2], int L[]){
//cout << "no!" << endl;
for (int i = 0; i <= K; i++){
cnt[i] = 1e9;
}
//cout << "yo!" << endl;
for (int i = 0; i < N-1; i++){
adj[H[i][0]].push_back({H[i][1], L[i]});
adj[H[i][1]].push_back({H[i][0], L[i]});
}
centroidDecomp(0, K);
return (ans < 1e8 ? ans : -1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
30040 KB |
Output is correct |
2 |
Correct |
6 ms |
30044 KB |
Output is correct |
3 |
Correct |
6 ms |
29996 KB |
Output is correct |
4 |
Correct |
8 ms |
30044 KB |
Output is correct |
5 |
Correct |
6 ms |
30220 KB |
Output is correct |
6 |
Correct |
6 ms |
30040 KB |
Output is correct |
7 |
Correct |
7 ms |
30044 KB |
Output is correct |
8 |
Correct |
6 ms |
30148 KB |
Output is correct |
9 |
Correct |
7 ms |
30044 KB |
Output is correct |
10 |
Correct |
6 ms |
30044 KB |
Output is correct |
11 |
Correct |
7 ms |
30044 KB |
Output is correct |
12 |
Correct |
7 ms |
30044 KB |
Output is correct |
13 |
Correct |
7 ms |
30204 KB |
Output is correct |
14 |
Correct |
6 ms |
30044 KB |
Output is correct |
15 |
Correct |
7 ms |
30044 KB |
Output is correct |
16 |
Correct |
7 ms |
30044 KB |
Output is correct |
17 |
Correct |
7 ms |
30044 KB |
Output is correct |
18 |
Correct |
7 ms |
30044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
30040 KB |
Output is correct |
2 |
Correct |
6 ms |
30044 KB |
Output is correct |
3 |
Correct |
6 ms |
29996 KB |
Output is correct |
4 |
Correct |
8 ms |
30044 KB |
Output is correct |
5 |
Correct |
6 ms |
30220 KB |
Output is correct |
6 |
Correct |
6 ms |
30040 KB |
Output is correct |
7 |
Correct |
7 ms |
30044 KB |
Output is correct |
8 |
Correct |
6 ms |
30148 KB |
Output is correct |
9 |
Correct |
7 ms |
30044 KB |
Output is correct |
10 |
Correct |
6 ms |
30044 KB |
Output is correct |
11 |
Correct |
7 ms |
30044 KB |
Output is correct |
12 |
Correct |
7 ms |
30044 KB |
Output is correct |
13 |
Correct |
7 ms |
30204 KB |
Output is correct |
14 |
Correct |
6 ms |
30044 KB |
Output is correct |
15 |
Correct |
7 ms |
30044 KB |
Output is correct |
16 |
Correct |
7 ms |
30044 KB |
Output is correct |
17 |
Correct |
7 ms |
30044 KB |
Output is correct |
18 |
Correct |
7 ms |
30044 KB |
Output is correct |
19 |
Correct |
7 ms |
30204 KB |
Output is correct |
20 |
Correct |
7 ms |
30044 KB |
Output is correct |
21 |
Correct |
7 ms |
30040 KB |
Output is correct |
22 |
Correct |
8 ms |
32332 KB |
Output is correct |
23 |
Correct |
8 ms |
32224 KB |
Output is correct |
24 |
Correct |
7 ms |
32092 KB |
Output is correct |
25 |
Correct |
8 ms |
32284 KB |
Output is correct |
26 |
Correct |
7 ms |
32092 KB |
Output is correct |
27 |
Correct |
8 ms |
32092 KB |
Output is correct |
28 |
Correct |
7 ms |
32092 KB |
Output is correct |
29 |
Correct |
8 ms |
32088 KB |
Output is correct |
30 |
Correct |
8 ms |
32092 KB |
Output is correct |
31 |
Correct |
7 ms |
32092 KB |
Output is correct |
32 |
Correct |
8 ms |
32092 KB |
Output is correct |
33 |
Correct |
8 ms |
32092 KB |
Output is correct |
34 |
Correct |
8 ms |
32180 KB |
Output is correct |
35 |
Correct |
8 ms |
32256 KB |
Output is correct |
36 |
Correct |
8 ms |
32216 KB |
Output is correct |
37 |
Correct |
8 ms |
32092 KB |
Output is correct |
38 |
Correct |
8 ms |
32216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
30040 KB |
Output is correct |
2 |
Correct |
6 ms |
30044 KB |
Output is correct |
3 |
Correct |
6 ms |
29996 KB |
Output is correct |
4 |
Correct |
8 ms |
30044 KB |
Output is correct |
5 |
Correct |
6 ms |
30220 KB |
Output is correct |
6 |
Correct |
6 ms |
30040 KB |
Output is correct |
7 |
Correct |
7 ms |
30044 KB |
Output is correct |
8 |
Correct |
6 ms |
30148 KB |
Output is correct |
9 |
Correct |
7 ms |
30044 KB |
Output is correct |
10 |
Correct |
6 ms |
30044 KB |
Output is correct |
11 |
Correct |
7 ms |
30044 KB |
Output is correct |
12 |
Correct |
7 ms |
30044 KB |
Output is correct |
13 |
Correct |
7 ms |
30204 KB |
Output is correct |
14 |
Correct |
6 ms |
30044 KB |
Output is correct |
15 |
Correct |
7 ms |
30044 KB |
Output is correct |
16 |
Correct |
7 ms |
30044 KB |
Output is correct |
17 |
Correct |
7 ms |
30044 KB |
Output is correct |
18 |
Correct |
7 ms |
30044 KB |
Output is correct |
19 |
Correct |
117 ms |
37536 KB |
Output is correct |
20 |
Correct |
97 ms |
37528 KB |
Output is correct |
21 |
Correct |
109 ms |
37552 KB |
Output is correct |
22 |
Correct |
94 ms |
37460 KB |
Output is correct |
23 |
Correct |
58 ms |
37664 KB |
Output is correct |
24 |
Correct |
47 ms |
37760 KB |
Output is correct |
25 |
Correct |
133 ms |
40536 KB |
Output is correct |
26 |
Correct |
95 ms |
43220 KB |
Output is correct |
27 |
Correct |
122 ms |
45132 KB |
Output is correct |
28 |
Correct |
182 ms |
56476 KB |
Output is correct |
29 |
Correct |
185 ms |
55576 KB |
Output is correct |
30 |
Correct |
114 ms |
44952 KB |
Output is correct |
31 |
Correct |
120 ms |
45136 KB |
Output is correct |
32 |
Correct |
150 ms |
45032 KB |
Output is correct |
33 |
Correct |
169 ms |
44044 KB |
Output is correct |
34 |
Correct |
132 ms |
44684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
30040 KB |
Output is correct |
2 |
Correct |
6 ms |
30044 KB |
Output is correct |
3 |
Correct |
6 ms |
29996 KB |
Output is correct |
4 |
Correct |
8 ms |
30044 KB |
Output is correct |
5 |
Correct |
6 ms |
30220 KB |
Output is correct |
6 |
Correct |
6 ms |
30040 KB |
Output is correct |
7 |
Correct |
7 ms |
30044 KB |
Output is correct |
8 |
Correct |
6 ms |
30148 KB |
Output is correct |
9 |
Correct |
7 ms |
30044 KB |
Output is correct |
10 |
Correct |
6 ms |
30044 KB |
Output is correct |
11 |
Correct |
7 ms |
30044 KB |
Output is correct |
12 |
Correct |
7 ms |
30044 KB |
Output is correct |
13 |
Correct |
7 ms |
30204 KB |
Output is correct |
14 |
Correct |
6 ms |
30044 KB |
Output is correct |
15 |
Correct |
7 ms |
30044 KB |
Output is correct |
16 |
Correct |
7 ms |
30044 KB |
Output is correct |
17 |
Correct |
7 ms |
30044 KB |
Output is correct |
18 |
Correct |
7 ms |
30044 KB |
Output is correct |
19 |
Correct |
7 ms |
30204 KB |
Output is correct |
20 |
Correct |
7 ms |
30044 KB |
Output is correct |
21 |
Correct |
7 ms |
30040 KB |
Output is correct |
22 |
Correct |
8 ms |
32332 KB |
Output is correct |
23 |
Correct |
8 ms |
32224 KB |
Output is correct |
24 |
Correct |
7 ms |
32092 KB |
Output is correct |
25 |
Correct |
8 ms |
32284 KB |
Output is correct |
26 |
Correct |
7 ms |
32092 KB |
Output is correct |
27 |
Correct |
8 ms |
32092 KB |
Output is correct |
28 |
Correct |
7 ms |
32092 KB |
Output is correct |
29 |
Correct |
8 ms |
32088 KB |
Output is correct |
30 |
Correct |
8 ms |
32092 KB |
Output is correct |
31 |
Correct |
7 ms |
32092 KB |
Output is correct |
32 |
Correct |
8 ms |
32092 KB |
Output is correct |
33 |
Correct |
8 ms |
32092 KB |
Output is correct |
34 |
Correct |
8 ms |
32180 KB |
Output is correct |
35 |
Correct |
8 ms |
32256 KB |
Output is correct |
36 |
Correct |
8 ms |
32216 KB |
Output is correct |
37 |
Correct |
8 ms |
32092 KB |
Output is correct |
38 |
Correct |
8 ms |
32216 KB |
Output is correct |
39 |
Correct |
117 ms |
37536 KB |
Output is correct |
40 |
Correct |
97 ms |
37528 KB |
Output is correct |
41 |
Correct |
109 ms |
37552 KB |
Output is correct |
42 |
Correct |
94 ms |
37460 KB |
Output is correct |
43 |
Correct |
58 ms |
37664 KB |
Output is correct |
44 |
Correct |
47 ms |
37760 KB |
Output is correct |
45 |
Correct |
133 ms |
40536 KB |
Output is correct |
46 |
Correct |
95 ms |
43220 KB |
Output is correct |
47 |
Correct |
122 ms |
45132 KB |
Output is correct |
48 |
Correct |
182 ms |
56476 KB |
Output is correct |
49 |
Correct |
185 ms |
55576 KB |
Output is correct |
50 |
Correct |
114 ms |
44952 KB |
Output is correct |
51 |
Correct |
120 ms |
45136 KB |
Output is correct |
52 |
Correct |
150 ms |
45032 KB |
Output is correct |
53 |
Correct |
169 ms |
44044 KB |
Output is correct |
54 |
Correct |
132 ms |
44684 KB |
Output is correct |
55 |
Correct |
13 ms |
30556 KB |
Output is correct |
56 |
Correct |
15 ms |
30556 KB |
Output is correct |
57 |
Correct |
58 ms |
37772 KB |
Output is correct |
58 |
Correct |
34 ms |
37260 KB |
Output is correct |
59 |
Correct |
92 ms |
45188 KB |
Output is correct |
60 |
Correct |
370 ms |
57608 KB |
Output is correct |
61 |
Correct |
133 ms |
45096 KB |
Output is correct |
62 |
Correct |
154 ms |
47184 KB |
Output is correct |
63 |
Correct |
211 ms |
47096 KB |
Output is correct |
64 |
Correct |
360 ms |
47316 KB |
Output is correct |
65 |
Correct |
159 ms |
45968 KB |
Output is correct |
66 |
Correct |
293 ms |
55708 KB |
Output is correct |
67 |
Correct |
83 ms |
47812 KB |
Output is correct |
68 |
Correct |
179 ms |
48088 KB |
Output is correct |
69 |
Correct |
243 ms |
48208 KB |
Output is correct |
70 |
Correct |
151 ms |
47444 KB |
Output is correct |