#include "race.h"
#include "bits/stdc++.h"
#define ll long long
using namespace std;
typedef pair<int, int> pii;
const int N = 2e5+5;
const int INF = 1e9;
vector<pii> adj[N];
int K, answer;
int dp[N][105];
void dfs(int nd, int pr){
dp[nd][0] = 0;
for (int i = 1; i <= K; i++)
dp[nd][i] = INF;
for (auto [c, e]: adj[nd]){
if (c == pr)
continue;
dfs(c, nd);
//update answer
for (int w1 = 0; w1 + e <= K; w1++){
int w2 = K - e - w1;
answer = min(answer, dp[nd][w1] + dp[c][w2]+1);
}
//update dp
for (int p = 0; p+e <= K; p++)
dp[nd][p+e] = min(dp[nd][p+e],dp[c][p]+1);
}
}
void dfs1(int nd, int pr, int lvl, ll weight){
if (weight == K)
answer = min(answer, lvl);
if (weight >= K)
return;
for (auto [c, e]: adj[nd]){
if (c == pr)
continue;
dfs1(c, nd, lvl+1, weight+e);
}
}
map<ll, int> m[N];
void dfs2(int nd, int pr, ll W, int L){
m[nd].clear();
m[nd][W] = L;
for (auto [c, e]: adj[nd]){
if (c == pr)
continue;
dfs2(c, nd, W+e, L+1);
if (m[nd].size() < m[c].size())
swap(m[nd], m[c]);
//update answer
for (auto [w1, l1]: m[c]){
int w2 = K - w1 + 2*W;
if (m[nd].find(w2) != m[nd].end()){
int l2 = m[nd][w2];
answer = min(answer, l1 + l2 - 2*L);
}
}
//update map
for (auto [w1, l1]: m[c]){
if (m[nd].find(w1) == m[nd].end())
m[nd][w1] = l1;
else
m[nd][w1] = min(m[nd][w1], l1);
}
m[c].clear();
}
}
int best_path(int n, int k, int H[][2], int L[]){
K = k;
answer = INF;
for (int i = 0; i < n - 1; i++){
int u = H[i][0], v = H[i][1], w = L[i];
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
if (k <= 100) //subtask 1 and 3
dfs(1, -1);
else if(n <= 1000){ //subtask 2
for (int i = 0; i < n; i++)
dfs1(i, -1, 0, 0);
}
else{
dfs2(1, -1, 0, 0);
}
if (answer == INF)
answer = -1;
return answer;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14420 KB |
Output is correct |
2 |
Correct |
6 ms |
14420 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
6 ms |
14420 KB |
Output is correct |
5 |
Correct |
6 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14464 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14392 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14468 KB |
Output is correct |
16 |
Correct |
7 ms |
14352 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
6 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14420 KB |
Output is correct |
2 |
Correct |
6 ms |
14420 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
6 ms |
14420 KB |
Output is correct |
5 |
Correct |
6 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14464 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14392 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14468 KB |
Output is correct |
16 |
Correct |
7 ms |
14352 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
6 ms |
14420 KB |
Output is correct |
19 |
Correct |
7 ms |
14292 KB |
Output is correct |
20 |
Correct |
6 ms |
14420 KB |
Output is correct |
21 |
Correct |
17 ms |
14344 KB |
Output is correct |
22 |
Correct |
7 ms |
14420 KB |
Output is correct |
23 |
Correct |
7 ms |
14420 KB |
Output is correct |
24 |
Correct |
7 ms |
14420 KB |
Output is correct |
25 |
Correct |
17 ms |
14420 KB |
Output is correct |
26 |
Correct |
9 ms |
14548 KB |
Output is correct |
27 |
Correct |
7 ms |
14420 KB |
Output is correct |
28 |
Correct |
9 ms |
14420 KB |
Output is correct |
29 |
Correct |
12 ms |
14420 KB |
Output is correct |
30 |
Correct |
13 ms |
14420 KB |
Output is correct |
31 |
Correct |
18 ms |
14456 KB |
Output is correct |
32 |
Correct |
17 ms |
14460 KB |
Output is correct |
33 |
Correct |
15 ms |
14460 KB |
Output is correct |
34 |
Correct |
7 ms |
14420 KB |
Output is correct |
35 |
Correct |
8 ms |
14420 KB |
Output is correct |
36 |
Correct |
7 ms |
14404 KB |
Output is correct |
37 |
Correct |
11 ms |
14420 KB |
Output is correct |
38 |
Correct |
13 ms |
14464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14420 KB |
Output is correct |
2 |
Correct |
6 ms |
14420 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
6 ms |
14420 KB |
Output is correct |
5 |
Correct |
6 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14464 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14392 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14468 KB |
Output is correct |
16 |
Correct |
7 ms |
14352 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
6 ms |
14420 KB |
Output is correct |
19 |
Correct |
99 ms |
60316 KB |
Output is correct |
20 |
Correct |
101 ms |
60280 KB |
Output is correct |
21 |
Correct |
108 ms |
60304 KB |
Output is correct |
22 |
Correct |
114 ms |
60336 KB |
Output is correct |
23 |
Correct |
84 ms |
60564 KB |
Output is correct |
24 |
Correct |
80 ms |
60464 KB |
Output is correct |
25 |
Correct |
112 ms |
68352 KB |
Output is correct |
26 |
Correct |
74 ms |
69064 KB |
Output is correct |
27 |
Correct |
153 ms |
105956 KB |
Output is correct |
28 |
Correct |
201 ms |
122516 KB |
Output is correct |
29 |
Correct |
232 ms |
116360 KB |
Output is correct |
30 |
Correct |
172 ms |
105928 KB |
Output is correct |
31 |
Correct |
153 ms |
105880 KB |
Output is correct |
32 |
Correct |
217 ms |
105876 KB |
Output is correct |
33 |
Correct |
188 ms |
104960 KB |
Output is correct |
34 |
Correct |
147 ms |
104512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14420 KB |
Output is correct |
2 |
Correct |
6 ms |
14420 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
6 ms |
14420 KB |
Output is correct |
5 |
Correct |
6 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14464 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14392 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14468 KB |
Output is correct |
16 |
Correct |
7 ms |
14352 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
6 ms |
14420 KB |
Output is correct |
19 |
Correct |
7 ms |
14292 KB |
Output is correct |
20 |
Correct |
6 ms |
14420 KB |
Output is correct |
21 |
Correct |
17 ms |
14344 KB |
Output is correct |
22 |
Correct |
7 ms |
14420 KB |
Output is correct |
23 |
Correct |
7 ms |
14420 KB |
Output is correct |
24 |
Correct |
7 ms |
14420 KB |
Output is correct |
25 |
Correct |
17 ms |
14420 KB |
Output is correct |
26 |
Correct |
9 ms |
14548 KB |
Output is correct |
27 |
Correct |
7 ms |
14420 KB |
Output is correct |
28 |
Correct |
9 ms |
14420 KB |
Output is correct |
29 |
Correct |
12 ms |
14420 KB |
Output is correct |
30 |
Correct |
13 ms |
14420 KB |
Output is correct |
31 |
Correct |
18 ms |
14456 KB |
Output is correct |
32 |
Correct |
17 ms |
14460 KB |
Output is correct |
33 |
Correct |
15 ms |
14460 KB |
Output is correct |
34 |
Correct |
7 ms |
14420 KB |
Output is correct |
35 |
Correct |
8 ms |
14420 KB |
Output is correct |
36 |
Correct |
7 ms |
14404 KB |
Output is correct |
37 |
Correct |
11 ms |
14420 KB |
Output is correct |
38 |
Correct |
13 ms |
14464 KB |
Output is correct |
39 |
Correct |
99 ms |
60316 KB |
Output is correct |
40 |
Correct |
101 ms |
60280 KB |
Output is correct |
41 |
Correct |
108 ms |
60304 KB |
Output is correct |
42 |
Correct |
114 ms |
60336 KB |
Output is correct |
43 |
Correct |
84 ms |
60564 KB |
Output is correct |
44 |
Correct |
80 ms |
60464 KB |
Output is correct |
45 |
Correct |
112 ms |
68352 KB |
Output is correct |
46 |
Correct |
74 ms |
69064 KB |
Output is correct |
47 |
Correct |
153 ms |
105956 KB |
Output is correct |
48 |
Correct |
201 ms |
122516 KB |
Output is correct |
49 |
Correct |
232 ms |
116360 KB |
Output is correct |
50 |
Correct |
172 ms |
105928 KB |
Output is correct |
51 |
Correct |
153 ms |
105880 KB |
Output is correct |
52 |
Correct |
217 ms |
105876 KB |
Output is correct |
53 |
Correct |
188 ms |
104960 KB |
Output is correct |
54 |
Correct |
147 ms |
104512 KB |
Output is correct |
55 |
Correct |
16 ms |
15316 KB |
Output is correct |
56 |
Correct |
12 ms |
14928 KB |
Output is correct |
57 |
Correct |
58 ms |
61832 KB |
Output is correct |
58 |
Correct |
48 ms |
20492 KB |
Output is correct |
59 |
Correct |
71 ms |
45000 KB |
Output is correct |
60 |
Correct |
226 ms |
62916 KB |
Output is correct |
61 |
Correct |
150 ms |
29296 KB |
Output is correct |
62 |
Correct |
120 ms |
27600 KB |
Output is correct |
63 |
Correct |
150 ms |
27684 KB |
Output is correct |
64 |
Correct |
347 ms |
36572 KB |
Output is correct |
65 |
Correct |
331 ms |
40740 KB |
Output is correct |
66 |
Correct |
216 ms |
72340 KB |
Output is correct |
67 |
Correct |
116 ms |
27964 KB |
Output is correct |
68 |
Correct |
258 ms |
38704 KB |
Output is correct |
69 |
Correct |
259 ms |
43320 KB |
Output is correct |
70 |
Correct |
248 ms |
37704 KB |
Output is correct |