#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef long long ll;
const int MAXN = 1000100;
int n, k;
vii adj[MAXN];
int ans = -1;
bool vis[MAXN];
int sz[MAXN];
//vi reach;
int c = -1;
//vector<pair<int, ll>> paths;
//unordered_map<int, int> minLen;
int minLen[MAXN];
vi changed;
void calcSizes (int v, int p = -1) {
sz[v] = 1;
//reach.pb(v);
for (auto pp : adj[v]) {
int u = pp.f;
// ll w = pp.s;
if (u == p) continue;
if (vis[u]) continue;
calcSizes(u, v);
sz[v] += sz[u];
}
}
void findCentroid (int v, int allSz, int p = -1) {
bool can = true;
for (auto pp : adj[v]) {
int u = pp.f;
//ll w = pp.s;
if (u == p) continue;
if (vis[u]) continue;
findCentroid(u, allSz, v);
if (sz[u] > allSz/2) can = false;
}
if ((allSz - sz[v]) > allSz/2) can = false;
if (can) c = v;
}
void getPaths (int v, ll len, int dep, int p = -1) {
for (auto pp : adj[v]) {
int u = pp.f;
ll w = pp.s;
if (u == p) continue;
if (vis[u]) continue;
getPaths(u, len+w, dep+1, v);
}
//paths.pb({dep, len});
if (len <= k) {
int remLen = k - len;
if (minLen[remLen] != -1) {
int dep2 = minLen[remLen] + dep;
if (ans == -1) ans = dep2;
else ans = min(ans, dep2);
}
}
}
void getPaths2 (int v, ll len, int dep, int p = -1) {
for (auto pp : adj[v]) {
int u = pp.f;
ll w = pp.s;
if (u == p) continue;
if (vis[u]) continue;
getPaths2(u, len+w, dep+1, v);
}
//paths.pb({dep, len});
if (len < k) {
if (minLen[len] == -1) minLen[len] = dep;
else minLen[len] = min(minLen[len], dep);
changed.pb(len);
}
}
void trav (int v) {
//reach.clear();
c = -1;
calcSizes (v);
findCentroid (v, sz[v]);
//minLen.clear();
//FOR(i, 0, k) minLen[i] = -1;
minLen[0] = 0;
changed.clear();
for (auto pp : adj[c]) {
int u = pp.f;
ll w = pp.s;
if (vis[u]) continue;
//paths.clear();
getPaths(u, w, 1, c);
//cout << " paths from v = " << v << " trrough u = " << u << endl;
getPaths2(u,w,1,c);
}
for (int x : changed)
minLen[x] = -1;
vis[c] = true;
for (auto pp : adj[c]) {
int u = pp.f;
// ll w = pp.s;
if (vis[u]) continue;
trav(u);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N;
k = K;
FOR(i, 0, n-2) {
int u = H[i][0], v = H[i][1], w = L[i];
u++;
v++;
adj[u].pb({v, w});
adj[v].pb({u, w});
}
FOR(i, 1, k) minLen[i] = -1;
trav (1);
return ans;
}
/*
2 2
0 1 1
-1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23916 KB |
Output is correct |
2 |
Correct |
15 ms |
23788 KB |
Output is correct |
3 |
Correct |
14 ms |
23788 KB |
Output is correct |
4 |
Correct |
15 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
14 ms |
23788 KB |
Output is correct |
7 |
Correct |
15 ms |
23788 KB |
Output is correct |
8 |
Correct |
17 ms |
23788 KB |
Output is correct |
9 |
Correct |
15 ms |
23788 KB |
Output is correct |
10 |
Correct |
15 ms |
23788 KB |
Output is correct |
11 |
Correct |
14 ms |
23788 KB |
Output is correct |
12 |
Correct |
15 ms |
23788 KB |
Output is correct |
13 |
Correct |
15 ms |
23788 KB |
Output is correct |
14 |
Correct |
15 ms |
23788 KB |
Output is correct |
15 |
Correct |
14 ms |
23788 KB |
Output is correct |
16 |
Correct |
15 ms |
23788 KB |
Output is correct |
17 |
Correct |
14 ms |
23788 KB |
Output is correct |
18 |
Correct |
15 ms |
23788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23916 KB |
Output is correct |
2 |
Correct |
15 ms |
23788 KB |
Output is correct |
3 |
Correct |
14 ms |
23788 KB |
Output is correct |
4 |
Correct |
15 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
14 ms |
23788 KB |
Output is correct |
7 |
Correct |
15 ms |
23788 KB |
Output is correct |
8 |
Correct |
17 ms |
23788 KB |
Output is correct |
9 |
Correct |
15 ms |
23788 KB |
Output is correct |
10 |
Correct |
15 ms |
23788 KB |
Output is correct |
11 |
Correct |
14 ms |
23788 KB |
Output is correct |
12 |
Correct |
15 ms |
23788 KB |
Output is correct |
13 |
Correct |
15 ms |
23788 KB |
Output is correct |
14 |
Correct |
15 ms |
23788 KB |
Output is correct |
15 |
Correct |
14 ms |
23788 KB |
Output is correct |
16 |
Correct |
15 ms |
23788 KB |
Output is correct |
17 |
Correct |
14 ms |
23788 KB |
Output is correct |
18 |
Correct |
15 ms |
23788 KB |
Output is correct |
19 |
Correct |
15 ms |
23788 KB |
Output is correct |
20 |
Correct |
14 ms |
23788 KB |
Output is correct |
21 |
Correct |
16 ms |
23916 KB |
Output is correct |
22 |
Correct |
18 ms |
27500 KB |
Output is correct |
23 |
Correct |
17 ms |
26860 KB |
Output is correct |
24 |
Correct |
17 ms |
27244 KB |
Output is correct |
25 |
Correct |
20 ms |
27244 KB |
Output is correct |
26 |
Correct |
16 ms |
25196 KB |
Output is correct |
27 |
Correct |
18 ms |
26988 KB |
Output is correct |
28 |
Correct |
16 ms |
24684 KB |
Output is correct |
29 |
Correct |
16 ms |
25196 KB |
Output is correct |
30 |
Correct |
16 ms |
25324 KB |
Output is correct |
31 |
Correct |
17 ms |
26496 KB |
Output is correct |
32 |
Correct |
17 ms |
26732 KB |
Output is correct |
33 |
Correct |
20 ms |
26988 KB |
Output is correct |
34 |
Correct |
16 ms |
26220 KB |
Output is correct |
35 |
Correct |
17 ms |
27116 KB |
Output is correct |
36 |
Correct |
18 ms |
27500 KB |
Output is correct |
37 |
Correct |
17 ms |
26988 KB |
Output is correct |
38 |
Correct |
16 ms |
25836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23916 KB |
Output is correct |
2 |
Correct |
15 ms |
23788 KB |
Output is correct |
3 |
Correct |
14 ms |
23788 KB |
Output is correct |
4 |
Correct |
15 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
14 ms |
23788 KB |
Output is correct |
7 |
Correct |
15 ms |
23788 KB |
Output is correct |
8 |
Correct |
17 ms |
23788 KB |
Output is correct |
9 |
Correct |
15 ms |
23788 KB |
Output is correct |
10 |
Correct |
15 ms |
23788 KB |
Output is correct |
11 |
Correct |
14 ms |
23788 KB |
Output is correct |
12 |
Correct |
15 ms |
23788 KB |
Output is correct |
13 |
Correct |
15 ms |
23788 KB |
Output is correct |
14 |
Correct |
15 ms |
23788 KB |
Output is correct |
15 |
Correct |
14 ms |
23788 KB |
Output is correct |
16 |
Correct |
15 ms |
23788 KB |
Output is correct |
17 |
Correct |
14 ms |
23788 KB |
Output is correct |
18 |
Correct |
15 ms |
23788 KB |
Output is correct |
19 |
Correct |
260 ms |
29420 KB |
Output is correct |
20 |
Correct |
264 ms |
29420 KB |
Output is correct |
21 |
Correct |
232 ms |
29420 KB |
Output is correct |
22 |
Correct |
209 ms |
29800 KB |
Output is correct |
23 |
Correct |
162 ms |
29420 KB |
Output is correct |
24 |
Correct |
98 ms |
29292 KB |
Output is correct |
25 |
Correct |
222 ms |
33644 KB |
Output is correct |
26 |
Correct |
136 ms |
38512 KB |
Output is correct |
27 |
Correct |
268 ms |
34796 KB |
Output is correct |
28 |
Correct |
637 ms |
52108 KB |
Output is correct |
29 |
Correct |
652 ms |
50816 KB |
Output is correct |
30 |
Correct |
284 ms |
34888 KB |
Output is correct |
31 |
Correct |
293 ms |
34796 KB |
Output is correct |
32 |
Correct |
360 ms |
34796 KB |
Output is correct |
33 |
Correct |
422 ms |
33772 KB |
Output is correct |
34 |
Correct |
469 ms |
33644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23916 KB |
Output is correct |
2 |
Correct |
15 ms |
23788 KB |
Output is correct |
3 |
Correct |
14 ms |
23788 KB |
Output is correct |
4 |
Correct |
15 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
14 ms |
23788 KB |
Output is correct |
7 |
Correct |
15 ms |
23788 KB |
Output is correct |
8 |
Correct |
17 ms |
23788 KB |
Output is correct |
9 |
Correct |
15 ms |
23788 KB |
Output is correct |
10 |
Correct |
15 ms |
23788 KB |
Output is correct |
11 |
Correct |
14 ms |
23788 KB |
Output is correct |
12 |
Correct |
15 ms |
23788 KB |
Output is correct |
13 |
Correct |
15 ms |
23788 KB |
Output is correct |
14 |
Correct |
15 ms |
23788 KB |
Output is correct |
15 |
Correct |
14 ms |
23788 KB |
Output is correct |
16 |
Correct |
15 ms |
23788 KB |
Output is correct |
17 |
Correct |
14 ms |
23788 KB |
Output is correct |
18 |
Correct |
15 ms |
23788 KB |
Output is correct |
19 |
Correct |
15 ms |
23788 KB |
Output is correct |
20 |
Correct |
14 ms |
23788 KB |
Output is correct |
21 |
Correct |
16 ms |
23916 KB |
Output is correct |
22 |
Correct |
18 ms |
27500 KB |
Output is correct |
23 |
Correct |
17 ms |
26860 KB |
Output is correct |
24 |
Correct |
17 ms |
27244 KB |
Output is correct |
25 |
Correct |
20 ms |
27244 KB |
Output is correct |
26 |
Correct |
16 ms |
25196 KB |
Output is correct |
27 |
Correct |
18 ms |
26988 KB |
Output is correct |
28 |
Correct |
16 ms |
24684 KB |
Output is correct |
29 |
Correct |
16 ms |
25196 KB |
Output is correct |
30 |
Correct |
16 ms |
25324 KB |
Output is correct |
31 |
Correct |
17 ms |
26496 KB |
Output is correct |
32 |
Correct |
17 ms |
26732 KB |
Output is correct |
33 |
Correct |
20 ms |
26988 KB |
Output is correct |
34 |
Correct |
16 ms |
26220 KB |
Output is correct |
35 |
Correct |
17 ms |
27116 KB |
Output is correct |
36 |
Correct |
18 ms |
27500 KB |
Output is correct |
37 |
Correct |
17 ms |
26988 KB |
Output is correct |
38 |
Correct |
16 ms |
25836 KB |
Output is correct |
39 |
Correct |
260 ms |
29420 KB |
Output is correct |
40 |
Correct |
264 ms |
29420 KB |
Output is correct |
41 |
Correct |
232 ms |
29420 KB |
Output is correct |
42 |
Correct |
209 ms |
29800 KB |
Output is correct |
43 |
Correct |
162 ms |
29420 KB |
Output is correct |
44 |
Correct |
98 ms |
29292 KB |
Output is correct |
45 |
Correct |
222 ms |
33644 KB |
Output is correct |
46 |
Correct |
136 ms |
38512 KB |
Output is correct |
47 |
Correct |
268 ms |
34796 KB |
Output is correct |
48 |
Correct |
637 ms |
52108 KB |
Output is correct |
49 |
Correct |
652 ms |
50816 KB |
Output is correct |
50 |
Correct |
284 ms |
34888 KB |
Output is correct |
51 |
Correct |
293 ms |
34796 KB |
Output is correct |
52 |
Correct |
360 ms |
34796 KB |
Output is correct |
53 |
Correct |
422 ms |
33772 KB |
Output is correct |
54 |
Correct |
469 ms |
33644 KB |
Output is correct |
55 |
Correct |
24 ms |
24428 KB |
Output is correct |
56 |
Correct |
26 ms |
24428 KB |
Output is correct |
57 |
Correct |
118 ms |
29932 KB |
Output is correct |
58 |
Correct |
56 ms |
30304 KB |
Output is correct |
59 |
Correct |
137 ms |
39304 KB |
Output is correct |
60 |
Correct |
686 ms |
59236 KB |
Output is correct |
61 |
Correct |
269 ms |
38128 KB |
Output is correct |
62 |
Correct |
277 ms |
42600 KB |
Output is correct |
63 |
Correct |
371 ms |
42600 KB |
Output is correct |
64 |
Correct |
792 ms |
40932 KB |
Output is correct |
65 |
Correct |
558 ms |
38604 KB |
Output is correct |
66 |
Correct |
711 ms |
54888 KB |
Output is correct |
67 |
Correct |
158 ms |
44124 KB |
Output is correct |
68 |
Correct |
402 ms |
42984 KB |
Output is correct |
69 |
Correct |
392 ms |
42988 KB |
Output is correct |
70 |
Correct |
403 ms |
42344 KB |
Output is correct |