#include <bits/stdc++.h>
#include "race.h"
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 5e5 + 2;
const int M = 1e9;
int mod = 1000000007;
vector<array<int, 2>> g[N];
bool was[N];
int sz[N], k;
void Dfs(int s, int e) {
sz[s] = 1;
for (auto u : g[s]) {
if (u[0] == e || was[u[0]]) continue;
Dfs(u[0], s);
sz[s] += sz[u[0]];
}
}
int Find(int s, int e, int n) {
for (auto u : g[s]) {
if (u[0] == e || was[u[0]]) continue;
if (2 * sz[u[0]] > n) return Find(u[0], s, n);
}
return s;
}
int FindCentroid(int s) {
Dfs(s, -1);
return Find(s, -1, sz[s]);
}
map<ll, int> m;
int ans;
vector<pair<ll, int>> tmp;
void Solve(int s, int e, int d, ll o) {
if (o > k || d > ans) return;
tmp.push_back({o, d});
if (m.find(k - o) != m.end()) smin(ans, d + m[k - o]);
for (auto u : g[s]) {
if (u[0] == e || was[u[0]]) continue;
Solve(u[0], s, d + 1, o + u[1]);
}
}
void Decompose(int s) {
s = FindCentroid(s);
was[s] = 1;
m.clear();
m[0] = 0;
for (auto u : g[s]) {
if (was[u[0]]) continue;
tmp.clear();
Solve(u[0], s, 1, u[1]);
for (auto v : tmp) {
if (m.find(v.first) == m.end()) m[v.first] = v.second;
else smin(m[v.first], v.second);
}
}
for (auto u : g[s]) {
if (was[u[0]] == 0) Decompose(u[0]);
}
}
int best_path(int n, int K, int h[][2], int l[]) {
k = K;
for (int i = 0; i < n - 1; i++) {
g[h[i][0]].push_back({h[i][1], l[i]});
g[h[i][1]].push_back({h[i][0], l[i]});
}
ans = n;
Decompose(0);
if (ans == n) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12056 KB |
Output is correct |
3 |
Correct |
7 ms |
12048 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12028 KB |
Output is correct |
7 |
Correct |
8 ms |
12064 KB |
Output is correct |
8 |
Correct |
7 ms |
11964 KB |
Output is correct |
9 |
Correct |
6 ms |
11996 KB |
Output is correct |
10 |
Correct |
6 ms |
12056 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
6 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
12080 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
12056 KB |
Output is correct |
17 |
Correct |
7 ms |
12048 KB |
Output is correct |
18 |
Correct |
7 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12056 KB |
Output is correct |
3 |
Correct |
7 ms |
12048 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12028 KB |
Output is correct |
7 |
Correct |
8 ms |
12064 KB |
Output is correct |
8 |
Correct |
7 ms |
11964 KB |
Output is correct |
9 |
Correct |
6 ms |
11996 KB |
Output is correct |
10 |
Correct |
6 ms |
12056 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
6 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
12080 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
12056 KB |
Output is correct |
17 |
Correct |
7 ms |
12048 KB |
Output is correct |
18 |
Correct |
7 ms |
11988 KB |
Output is correct |
19 |
Correct |
7 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
12040 KB |
Output is correct |
21 |
Correct |
8 ms |
12116 KB |
Output is correct |
22 |
Correct |
7 ms |
12072 KB |
Output is correct |
23 |
Correct |
7 ms |
12060 KB |
Output is correct |
24 |
Correct |
8 ms |
12104 KB |
Output is correct |
25 |
Correct |
8 ms |
12116 KB |
Output is correct |
26 |
Correct |
8 ms |
12116 KB |
Output is correct |
27 |
Correct |
7 ms |
12068 KB |
Output is correct |
28 |
Correct |
9 ms |
12072 KB |
Output is correct |
29 |
Correct |
7 ms |
12200 KB |
Output is correct |
30 |
Correct |
8 ms |
12116 KB |
Output is correct |
31 |
Correct |
9 ms |
12116 KB |
Output is correct |
32 |
Correct |
9 ms |
12244 KB |
Output is correct |
33 |
Correct |
10 ms |
12100 KB |
Output is correct |
34 |
Correct |
9 ms |
12116 KB |
Output is correct |
35 |
Correct |
7 ms |
12116 KB |
Output is correct |
36 |
Correct |
8 ms |
12092 KB |
Output is correct |
37 |
Correct |
8 ms |
12124 KB |
Output is correct |
38 |
Correct |
7 ms |
12092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12056 KB |
Output is correct |
3 |
Correct |
7 ms |
12048 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12028 KB |
Output is correct |
7 |
Correct |
8 ms |
12064 KB |
Output is correct |
8 |
Correct |
7 ms |
11964 KB |
Output is correct |
9 |
Correct |
6 ms |
11996 KB |
Output is correct |
10 |
Correct |
6 ms |
12056 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
6 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
12080 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
12056 KB |
Output is correct |
17 |
Correct |
7 ms |
12048 KB |
Output is correct |
18 |
Correct |
7 ms |
11988 KB |
Output is correct |
19 |
Correct |
174 ms |
18780 KB |
Output is correct |
20 |
Correct |
155 ms |
18724 KB |
Output is correct |
21 |
Correct |
167 ms |
19032 KB |
Output is correct |
22 |
Correct |
169 ms |
19120 KB |
Output is correct |
23 |
Correct |
96 ms |
19020 KB |
Output is correct |
24 |
Correct |
74 ms |
18956 KB |
Output is correct |
25 |
Correct |
142 ms |
22348 KB |
Output is correct |
26 |
Correct |
109 ms |
26964 KB |
Output is correct |
27 |
Correct |
192 ms |
25860 KB |
Output is correct |
28 |
Correct |
293 ms |
40356 KB |
Output is correct |
29 |
Correct |
246 ms |
39124 KB |
Output is correct |
30 |
Correct |
210 ms |
25860 KB |
Output is correct |
31 |
Correct |
186 ms |
25972 KB |
Output is correct |
32 |
Correct |
222 ms |
25960 KB |
Output is correct |
33 |
Correct |
269 ms |
24800 KB |
Output is correct |
34 |
Correct |
182 ms |
25724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12056 KB |
Output is correct |
3 |
Correct |
7 ms |
12048 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12028 KB |
Output is correct |
7 |
Correct |
8 ms |
12064 KB |
Output is correct |
8 |
Correct |
7 ms |
11964 KB |
Output is correct |
9 |
Correct |
6 ms |
11996 KB |
Output is correct |
10 |
Correct |
6 ms |
12056 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
6 ms |
11988 KB |
Output is correct |
13 |
Correct |
6 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
12080 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
12056 KB |
Output is correct |
17 |
Correct |
7 ms |
12048 KB |
Output is correct |
18 |
Correct |
7 ms |
11988 KB |
Output is correct |
19 |
Correct |
7 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
12040 KB |
Output is correct |
21 |
Correct |
8 ms |
12116 KB |
Output is correct |
22 |
Correct |
7 ms |
12072 KB |
Output is correct |
23 |
Correct |
7 ms |
12060 KB |
Output is correct |
24 |
Correct |
8 ms |
12104 KB |
Output is correct |
25 |
Correct |
8 ms |
12116 KB |
Output is correct |
26 |
Correct |
8 ms |
12116 KB |
Output is correct |
27 |
Correct |
7 ms |
12068 KB |
Output is correct |
28 |
Correct |
9 ms |
12072 KB |
Output is correct |
29 |
Correct |
7 ms |
12200 KB |
Output is correct |
30 |
Correct |
8 ms |
12116 KB |
Output is correct |
31 |
Correct |
9 ms |
12116 KB |
Output is correct |
32 |
Correct |
9 ms |
12244 KB |
Output is correct |
33 |
Correct |
10 ms |
12100 KB |
Output is correct |
34 |
Correct |
9 ms |
12116 KB |
Output is correct |
35 |
Correct |
7 ms |
12116 KB |
Output is correct |
36 |
Correct |
8 ms |
12092 KB |
Output is correct |
37 |
Correct |
8 ms |
12124 KB |
Output is correct |
38 |
Correct |
7 ms |
12092 KB |
Output is correct |
39 |
Correct |
174 ms |
18780 KB |
Output is correct |
40 |
Correct |
155 ms |
18724 KB |
Output is correct |
41 |
Correct |
167 ms |
19032 KB |
Output is correct |
42 |
Correct |
169 ms |
19120 KB |
Output is correct |
43 |
Correct |
96 ms |
19020 KB |
Output is correct |
44 |
Correct |
74 ms |
18956 KB |
Output is correct |
45 |
Correct |
142 ms |
22348 KB |
Output is correct |
46 |
Correct |
109 ms |
26964 KB |
Output is correct |
47 |
Correct |
192 ms |
25860 KB |
Output is correct |
48 |
Correct |
293 ms |
40356 KB |
Output is correct |
49 |
Correct |
246 ms |
39124 KB |
Output is correct |
50 |
Correct |
210 ms |
25860 KB |
Output is correct |
51 |
Correct |
186 ms |
25972 KB |
Output is correct |
52 |
Correct |
222 ms |
25960 KB |
Output is correct |
53 |
Correct |
269 ms |
24800 KB |
Output is correct |
54 |
Correct |
182 ms |
25724 KB |
Output is correct |
55 |
Correct |
23 ms |
12836 KB |
Output is correct |
56 |
Correct |
19 ms |
12932 KB |
Output is correct |
57 |
Correct |
143 ms |
19912 KB |
Output is correct |
58 |
Correct |
49 ms |
18688 KB |
Output is correct |
59 |
Correct |
291 ms |
31428 KB |
Output is correct |
60 |
Correct |
852 ms |
52408 KB |
Output is correct |
61 |
Correct |
253 ms |
26380 KB |
Output is correct |
62 |
Correct |
279 ms |
26144 KB |
Output is correct |
63 |
Correct |
329 ms |
26068 KB |
Output is correct |
64 |
Correct |
947 ms |
33676 KB |
Output is correct |
65 |
Correct |
217 ms |
26700 KB |
Output is correct |
66 |
Correct |
590 ms |
37052 KB |
Output is correct |
67 |
Correct |
121 ms |
26660 KB |
Output is correct |
68 |
Correct |
221 ms |
27108 KB |
Output is correct |
69 |
Correct |
222 ms |
26956 KB |
Output is correct |
70 |
Correct |
200 ms |
26444 KB |
Output is correct |