#include <bits/stdc++.h>
using namespace std;
//#include "race.h"
#define int long long
int n,k;
#define MAXN 200007
unordered_map<int, int> al[MAXN];
int ans = 2*MAXN;
unordered_map<int, int> merger[MAXN];
int offset[MAXN] = {0};
int valoffset[MAXN] = {0};
int dfs(int node, int p){
int largest = -1, largestVal = -1;
int totalSize = 1;
for (auto x : al[node]){
if (x.first == p) continue;
int ss = dfs(x.first, node);
totalSize += ss;
if (ss > largestVal){
largestVal = ss; largest = x.first;
}
}
//we need to merge while checking :D
if (largest != -1){
swap(merger[node], merger[largest]);
offset[node] = offset[largest] + al[node][largest];
valoffset[node] = valoffset[largest] + 1;
merger[node][-offset[node]] = -valoffset[node];
if (merger[node].count(k - offset[node]) != 0){
int torture = merger[node][k - offset[node]];
ans = min(ans, torture + valoffset[node]);
}
for (auto x : al[node]){
if (x.first == p || x.first == largest) continue;
for (auto y : merger[x.first]){
//check if x.second + y.first - offset[x.first] can form k with a value from (merger[node]) but offset by offset[node]
//check if k - (x.second + y.first - offset[x.first] - offset[node]) can be found
int trueValSec = y.first + offset[x.first] + al[node][x.first];
//cerr << node << ' ' << x.first << ' ' << trueValSec << ' ' << k - trueValSec + offset[node] << '\n';
/*
cout << "For node " << node << '\n';
for (auto x : merger[node]){
if (x.first + offset[node] == k){
//ans = min(ans, x.second + valoffset[node]);
}
cout << x.first + offset[node] << ' ' << x.second + valoffset[node] << '\n';
}
* */
//we want k = trueval + trueValSec
//k - truevalSec = trueVal
//k - trueValSec = valFound - offset
//k - trueValSec + offset[node] = valFound
//valFound = trueval + offset[]
//if (node == 0 && x.first == 1) cout << trueValSec << ' ' << k - trueValSec - offset[node] << "\n\n\n";
if (merger[node].count(k - trueValSec - offset[node]) != 0){
int torture = merger[node][k - trueValSec - offset[node]] + valoffset[x.first];
ans = min(ans, torture + y.second + valoffset[node] + 1);
}
}
for (auto y : merger[x.first]){
int intendedAdd = y.first + offset[x.first] - offset[node] + x.second;
if (merger[node].count(intendedAdd) == 0 || merger[node][intendedAdd] > y.second + 1 - valoffset[x.first] - valoffset[node]){
merger[node][intendedAdd] = y.second + 1 + valoffset[x.first] - valoffset[node];
}
}
}
}
merger[node][-offset[node]] = -valoffset[node];
//cout << "For node " << node << '\n';
if (merger[node].count(k - offset[node]) != 0){
ans = min(ans, merger[node][k - offset[node]] + valoffset[node]);
}
/*
for (auto x : merger[node]){
if (x.first + offset[node] == k){
ans = min(ans, x.second + valoffset[node]);
}
//cout << x.first + offset[node] << ' ' << x.second + valoffset[node] << '\n';
}
*/
return totalSize;
}
mt19937 mt_rng(chrono::steady_clock::now().time_since_epoch().count());
int randint(int a, int b) {
return uniform_int_distribution<int>(a, b)(mt_rng);
}
int32_t best_path(int32_t N, int32_t K, int32_t H[][2], int32_t L[]){
n = N;
k = K;
for (int x = 0; x < n-1; x++){
int a, b; a = H[x][0], b = H[x][1];
al[a][b] = L[x];
al[b][a] = L[x];
}
dfs(randint(0, n-1), -1);
if (ans > MAXN) return -1;
return ans;
}
/*
main(){
int32_t H[][2] = {{0,1},{1,2},{1,3}, {0,4},{4,5},{4,6}};
int32_t L[] = {4,3,2,50,7,9};
cout << best_path(7, 56, H, L);
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
22228 KB |
Output is correct |
2 |
Correct |
14 ms |
22272 KB |
Output is correct |
3 |
Correct |
14 ms |
22240 KB |
Output is correct |
4 |
Correct |
14 ms |
22228 KB |
Output is correct |
5 |
Correct |
16 ms |
22244 KB |
Output is correct |
6 |
Correct |
14 ms |
22228 KB |
Output is correct |
7 |
Correct |
14 ms |
22236 KB |
Output is correct |
8 |
Correct |
13 ms |
22248 KB |
Output is correct |
9 |
Correct |
13 ms |
22276 KB |
Output is correct |
10 |
Correct |
13 ms |
22236 KB |
Output is correct |
11 |
Correct |
13 ms |
22284 KB |
Output is correct |
12 |
Correct |
14 ms |
22240 KB |
Output is correct |
13 |
Correct |
13 ms |
22220 KB |
Output is correct |
14 |
Correct |
13 ms |
22228 KB |
Output is correct |
15 |
Correct |
15 ms |
22228 KB |
Output is correct |
16 |
Correct |
13 ms |
22236 KB |
Output is correct |
17 |
Correct |
13 ms |
22228 KB |
Output is correct |
18 |
Correct |
13 ms |
22228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
22228 KB |
Output is correct |
2 |
Correct |
14 ms |
22272 KB |
Output is correct |
3 |
Correct |
14 ms |
22240 KB |
Output is correct |
4 |
Correct |
14 ms |
22228 KB |
Output is correct |
5 |
Correct |
16 ms |
22244 KB |
Output is correct |
6 |
Correct |
14 ms |
22228 KB |
Output is correct |
7 |
Correct |
14 ms |
22236 KB |
Output is correct |
8 |
Correct |
13 ms |
22248 KB |
Output is correct |
9 |
Correct |
13 ms |
22276 KB |
Output is correct |
10 |
Correct |
13 ms |
22236 KB |
Output is correct |
11 |
Correct |
13 ms |
22284 KB |
Output is correct |
12 |
Correct |
14 ms |
22240 KB |
Output is correct |
13 |
Correct |
13 ms |
22220 KB |
Output is correct |
14 |
Correct |
13 ms |
22228 KB |
Output is correct |
15 |
Correct |
15 ms |
22228 KB |
Output is correct |
16 |
Correct |
13 ms |
22236 KB |
Output is correct |
17 |
Correct |
13 ms |
22228 KB |
Output is correct |
18 |
Correct |
13 ms |
22228 KB |
Output is correct |
19 |
Correct |
13 ms |
22236 KB |
Output is correct |
20 |
Correct |
12 ms |
22232 KB |
Output is correct |
21 |
Correct |
13 ms |
22532 KB |
Output is correct |
22 |
Correct |
13 ms |
22612 KB |
Output is correct |
23 |
Correct |
14 ms |
22640 KB |
Output is correct |
24 |
Correct |
15 ms |
22636 KB |
Output is correct |
25 |
Correct |
15 ms |
22612 KB |
Output is correct |
26 |
Correct |
14 ms |
22632 KB |
Output is correct |
27 |
Correct |
14 ms |
22504 KB |
Output is correct |
28 |
Correct |
14 ms |
22612 KB |
Output is correct |
29 |
Correct |
14 ms |
22640 KB |
Output is correct |
30 |
Correct |
14 ms |
22616 KB |
Output is correct |
31 |
Correct |
14 ms |
22612 KB |
Output is correct |
32 |
Correct |
13 ms |
22612 KB |
Output is correct |
33 |
Correct |
16 ms |
22612 KB |
Output is correct |
34 |
Correct |
14 ms |
22528 KB |
Output is correct |
35 |
Correct |
14 ms |
22492 KB |
Output is correct |
36 |
Correct |
14 ms |
22504 KB |
Output is correct |
37 |
Correct |
14 ms |
22484 KB |
Output is correct |
38 |
Correct |
14 ms |
22500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
22228 KB |
Output is correct |
2 |
Correct |
14 ms |
22272 KB |
Output is correct |
3 |
Correct |
14 ms |
22240 KB |
Output is correct |
4 |
Correct |
14 ms |
22228 KB |
Output is correct |
5 |
Correct |
16 ms |
22244 KB |
Output is correct |
6 |
Correct |
14 ms |
22228 KB |
Output is correct |
7 |
Correct |
14 ms |
22236 KB |
Output is correct |
8 |
Correct |
13 ms |
22248 KB |
Output is correct |
9 |
Correct |
13 ms |
22276 KB |
Output is correct |
10 |
Correct |
13 ms |
22236 KB |
Output is correct |
11 |
Correct |
13 ms |
22284 KB |
Output is correct |
12 |
Correct |
14 ms |
22240 KB |
Output is correct |
13 |
Correct |
13 ms |
22220 KB |
Output is correct |
14 |
Correct |
13 ms |
22228 KB |
Output is correct |
15 |
Correct |
15 ms |
22228 KB |
Output is correct |
16 |
Correct |
13 ms |
22236 KB |
Output is correct |
17 |
Correct |
13 ms |
22228 KB |
Output is correct |
18 |
Correct |
13 ms |
22228 KB |
Output is correct |
19 |
Correct |
160 ms |
54404 KB |
Output is correct |
20 |
Correct |
168 ms |
54352 KB |
Output is correct |
21 |
Incorrect |
173 ms |
54576 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
22228 KB |
Output is correct |
2 |
Correct |
14 ms |
22272 KB |
Output is correct |
3 |
Correct |
14 ms |
22240 KB |
Output is correct |
4 |
Correct |
14 ms |
22228 KB |
Output is correct |
5 |
Correct |
16 ms |
22244 KB |
Output is correct |
6 |
Correct |
14 ms |
22228 KB |
Output is correct |
7 |
Correct |
14 ms |
22236 KB |
Output is correct |
8 |
Correct |
13 ms |
22248 KB |
Output is correct |
9 |
Correct |
13 ms |
22276 KB |
Output is correct |
10 |
Correct |
13 ms |
22236 KB |
Output is correct |
11 |
Correct |
13 ms |
22284 KB |
Output is correct |
12 |
Correct |
14 ms |
22240 KB |
Output is correct |
13 |
Correct |
13 ms |
22220 KB |
Output is correct |
14 |
Correct |
13 ms |
22228 KB |
Output is correct |
15 |
Correct |
15 ms |
22228 KB |
Output is correct |
16 |
Correct |
13 ms |
22236 KB |
Output is correct |
17 |
Correct |
13 ms |
22228 KB |
Output is correct |
18 |
Correct |
13 ms |
22228 KB |
Output is correct |
19 |
Correct |
13 ms |
22236 KB |
Output is correct |
20 |
Correct |
12 ms |
22232 KB |
Output is correct |
21 |
Correct |
13 ms |
22532 KB |
Output is correct |
22 |
Correct |
13 ms |
22612 KB |
Output is correct |
23 |
Correct |
14 ms |
22640 KB |
Output is correct |
24 |
Correct |
15 ms |
22636 KB |
Output is correct |
25 |
Correct |
15 ms |
22612 KB |
Output is correct |
26 |
Correct |
14 ms |
22632 KB |
Output is correct |
27 |
Correct |
14 ms |
22504 KB |
Output is correct |
28 |
Correct |
14 ms |
22612 KB |
Output is correct |
29 |
Correct |
14 ms |
22640 KB |
Output is correct |
30 |
Correct |
14 ms |
22616 KB |
Output is correct |
31 |
Correct |
14 ms |
22612 KB |
Output is correct |
32 |
Correct |
13 ms |
22612 KB |
Output is correct |
33 |
Correct |
16 ms |
22612 KB |
Output is correct |
34 |
Correct |
14 ms |
22528 KB |
Output is correct |
35 |
Correct |
14 ms |
22492 KB |
Output is correct |
36 |
Correct |
14 ms |
22504 KB |
Output is correct |
37 |
Correct |
14 ms |
22484 KB |
Output is correct |
38 |
Correct |
14 ms |
22500 KB |
Output is correct |
39 |
Correct |
160 ms |
54404 KB |
Output is correct |
40 |
Correct |
168 ms |
54352 KB |
Output is correct |
41 |
Incorrect |
173 ms |
54576 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |