#include <bits/stdc++.h>
using namespace std;
const int N = 200200, oo = 1 << 21;
int n, k, h[N][2], l[N], height[N], pos[N], deltaVal[N], st[N], last, curChild[N], par[N];
long long deltaID[N];
vector<pair<int, int>> a[N];
vector<int> order;
map<long long, int> m[N];
int ans = oo;
int unionMap(int u, int v, int c)
{
int res = oo;
long long offsetID = c + deltaID[u] + deltaID[v];
int offsetVal = 1 + deltaVal[u] + deltaVal[v];
for (auto it : m[v])
if (m[u].count(k - it.first - offsetID))
res = min(res, it.second + m[u][k - it.first - offsetID] + offsetVal);
offsetID = c + deltaID[v] - deltaID[u];
offsetVal = 1 + deltaVal[v] - deltaVal[u];
for (auto it : m[v])
if (!m[u].count(it.first + offsetID) || m[u][it.first + offsetID] > it.second + offsetVal)
m[u][it.first + offsetID] = it.second + offsetVal;
m[v].clear();
return res;
}
int calc(int x)
{
int mxSize = 0, res = oo, tmp;
pos[x] = x;
for (auto u : a[x])
{
int y = u.first;
if (y == par[x]) continue;
int sizeY = m[pos[y]].size();
if (sizeY > mxSize)
{
mxSize = sizeY;
pos[x] = pos[y];
tmp = u.second;
}
}
if (pos[x] == x)
{
m[x][0] = 0;
return res;
}
int u = pos[x];
deltaID[u] += tmp;
deltaVal[u]++;
if (m[u].count(k - deltaID[u]))
res = min(res, m[u][k - deltaID[u]] + deltaVal[u]);
if (!m[u].count(-deltaID[u]) || m[u][-deltaID[u]] > -deltaVal[u])
m[u][-deltaID[u]] = -deltaVal[u];
for (auto u : a[x])
{
int y = u.first;
if (y == par[x] || pos[x] == pos[y]) continue;
res = min(res, unionMap(pos[x], pos[y], u.second));
}
return res;
}
void dfs(int x)
{
for (auto u : a[x])
{
int y = u.first;
if (y == par[x])
continue;
par[y] = x;
dfs(y);
}
ans = min(ans, calc(x));
}
int best_path(int _n, int _k, int h[][2], int l[])
{
n = _n;
k = _k;
for (int i = 0; i < n - 1; i++)
{
a[h[i][0]].push_back({h[i][1], l[i]});
a[h[i][1]].push_back({h[i][0], l[i]});
}
par[0] = -1;
dfs(0);
return ans == oo ? -1 : ans;
}
Compilation message
race.cpp: In function 'int calc(int)':
race.cpp:58:14: warning: 'tmp' may be used uninitialized in this function [-Wmaybe-uninitialized]
deltaID[u] += tmp;
~~~~~~~~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14584 KB |
Output is correct |
2 |
Correct |
15 ms |
14456 KB |
Output is correct |
3 |
Correct |
15 ms |
14584 KB |
Output is correct |
4 |
Correct |
14 ms |
14456 KB |
Output is correct |
5 |
Correct |
14 ms |
14456 KB |
Output is correct |
6 |
Correct |
14 ms |
14428 KB |
Output is correct |
7 |
Correct |
14 ms |
14456 KB |
Output is correct |
8 |
Correct |
14 ms |
14456 KB |
Output is correct |
9 |
Correct |
14 ms |
14456 KB |
Output is correct |
10 |
Correct |
14 ms |
14456 KB |
Output is correct |
11 |
Correct |
14 ms |
14456 KB |
Output is correct |
12 |
Correct |
14 ms |
14456 KB |
Output is correct |
13 |
Correct |
14 ms |
14444 KB |
Output is correct |
14 |
Correct |
14 ms |
14456 KB |
Output is correct |
15 |
Correct |
14 ms |
14456 KB |
Output is correct |
16 |
Correct |
14 ms |
14456 KB |
Output is correct |
17 |
Correct |
14 ms |
14512 KB |
Output is correct |
18 |
Correct |
14 ms |
14456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14584 KB |
Output is correct |
2 |
Correct |
15 ms |
14456 KB |
Output is correct |
3 |
Correct |
15 ms |
14584 KB |
Output is correct |
4 |
Correct |
14 ms |
14456 KB |
Output is correct |
5 |
Correct |
14 ms |
14456 KB |
Output is correct |
6 |
Correct |
14 ms |
14428 KB |
Output is correct |
7 |
Correct |
14 ms |
14456 KB |
Output is correct |
8 |
Correct |
14 ms |
14456 KB |
Output is correct |
9 |
Correct |
14 ms |
14456 KB |
Output is correct |
10 |
Correct |
14 ms |
14456 KB |
Output is correct |
11 |
Correct |
14 ms |
14456 KB |
Output is correct |
12 |
Correct |
14 ms |
14456 KB |
Output is correct |
13 |
Correct |
14 ms |
14444 KB |
Output is correct |
14 |
Correct |
14 ms |
14456 KB |
Output is correct |
15 |
Correct |
14 ms |
14456 KB |
Output is correct |
16 |
Correct |
14 ms |
14456 KB |
Output is correct |
17 |
Correct |
14 ms |
14512 KB |
Output is correct |
18 |
Correct |
14 ms |
14456 KB |
Output is correct |
19 |
Correct |
14 ms |
14456 KB |
Output is correct |
20 |
Correct |
14 ms |
14456 KB |
Output is correct |
21 |
Correct |
15 ms |
14584 KB |
Output is correct |
22 |
Correct |
16 ms |
14628 KB |
Output is correct |
23 |
Correct |
15 ms |
14584 KB |
Output is correct |
24 |
Correct |
15 ms |
14584 KB |
Output is correct |
25 |
Correct |
15 ms |
14584 KB |
Output is correct |
26 |
Correct |
16 ms |
14584 KB |
Output is correct |
27 |
Correct |
15 ms |
14584 KB |
Output is correct |
28 |
Correct |
17 ms |
14572 KB |
Output is correct |
29 |
Correct |
16 ms |
14632 KB |
Output is correct |
30 |
Correct |
15 ms |
14584 KB |
Output is correct |
31 |
Correct |
15 ms |
14584 KB |
Output is correct |
32 |
Correct |
15 ms |
14584 KB |
Output is correct |
33 |
Correct |
16 ms |
14584 KB |
Output is correct |
34 |
Correct |
15 ms |
14844 KB |
Output is correct |
35 |
Correct |
16 ms |
14716 KB |
Output is correct |
36 |
Correct |
15 ms |
14584 KB |
Output is correct |
37 |
Correct |
15 ms |
14584 KB |
Output is correct |
38 |
Correct |
15 ms |
14584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14584 KB |
Output is correct |
2 |
Correct |
15 ms |
14456 KB |
Output is correct |
3 |
Correct |
15 ms |
14584 KB |
Output is correct |
4 |
Correct |
14 ms |
14456 KB |
Output is correct |
5 |
Correct |
14 ms |
14456 KB |
Output is correct |
6 |
Correct |
14 ms |
14428 KB |
Output is correct |
7 |
Correct |
14 ms |
14456 KB |
Output is correct |
8 |
Correct |
14 ms |
14456 KB |
Output is correct |
9 |
Correct |
14 ms |
14456 KB |
Output is correct |
10 |
Correct |
14 ms |
14456 KB |
Output is correct |
11 |
Correct |
14 ms |
14456 KB |
Output is correct |
12 |
Correct |
14 ms |
14456 KB |
Output is correct |
13 |
Correct |
14 ms |
14444 KB |
Output is correct |
14 |
Correct |
14 ms |
14456 KB |
Output is correct |
15 |
Correct |
14 ms |
14456 KB |
Output is correct |
16 |
Correct |
14 ms |
14456 KB |
Output is correct |
17 |
Correct |
14 ms |
14512 KB |
Output is correct |
18 |
Correct |
14 ms |
14456 KB |
Output is correct |
19 |
Correct |
131 ms |
22768 KB |
Output is correct |
20 |
Correct |
135 ms |
22884 KB |
Output is correct |
21 |
Correct |
132 ms |
22748 KB |
Output is correct |
22 |
Correct |
140 ms |
22836 KB |
Output is correct |
23 |
Correct |
174 ms |
23160 KB |
Output is correct |
24 |
Correct |
146 ms |
23092 KB |
Output is correct |
25 |
Correct |
106 ms |
24056 KB |
Output is correct |
26 |
Correct |
70 ms |
25720 KB |
Output is correct |
27 |
Correct |
289 ms |
37112 KB |
Output is correct |
28 |
Correct |
371 ms |
49660 KB |
Output is correct |
29 |
Correct |
357 ms |
49812 KB |
Output is correct |
30 |
Correct |
239 ms |
37240 KB |
Output is correct |
31 |
Correct |
232 ms |
37160 KB |
Output is correct |
32 |
Correct |
268 ms |
36920 KB |
Output is correct |
33 |
Correct |
244 ms |
30260 KB |
Output is correct |
34 |
Correct |
412 ms |
43824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14584 KB |
Output is correct |
2 |
Correct |
15 ms |
14456 KB |
Output is correct |
3 |
Correct |
15 ms |
14584 KB |
Output is correct |
4 |
Correct |
14 ms |
14456 KB |
Output is correct |
5 |
Correct |
14 ms |
14456 KB |
Output is correct |
6 |
Correct |
14 ms |
14428 KB |
Output is correct |
7 |
Correct |
14 ms |
14456 KB |
Output is correct |
8 |
Correct |
14 ms |
14456 KB |
Output is correct |
9 |
Correct |
14 ms |
14456 KB |
Output is correct |
10 |
Correct |
14 ms |
14456 KB |
Output is correct |
11 |
Correct |
14 ms |
14456 KB |
Output is correct |
12 |
Correct |
14 ms |
14456 KB |
Output is correct |
13 |
Correct |
14 ms |
14444 KB |
Output is correct |
14 |
Correct |
14 ms |
14456 KB |
Output is correct |
15 |
Correct |
14 ms |
14456 KB |
Output is correct |
16 |
Correct |
14 ms |
14456 KB |
Output is correct |
17 |
Correct |
14 ms |
14512 KB |
Output is correct |
18 |
Correct |
14 ms |
14456 KB |
Output is correct |
19 |
Correct |
14 ms |
14456 KB |
Output is correct |
20 |
Correct |
14 ms |
14456 KB |
Output is correct |
21 |
Correct |
15 ms |
14584 KB |
Output is correct |
22 |
Correct |
16 ms |
14628 KB |
Output is correct |
23 |
Correct |
15 ms |
14584 KB |
Output is correct |
24 |
Correct |
15 ms |
14584 KB |
Output is correct |
25 |
Correct |
15 ms |
14584 KB |
Output is correct |
26 |
Correct |
16 ms |
14584 KB |
Output is correct |
27 |
Correct |
15 ms |
14584 KB |
Output is correct |
28 |
Correct |
17 ms |
14572 KB |
Output is correct |
29 |
Correct |
16 ms |
14632 KB |
Output is correct |
30 |
Correct |
15 ms |
14584 KB |
Output is correct |
31 |
Correct |
15 ms |
14584 KB |
Output is correct |
32 |
Correct |
15 ms |
14584 KB |
Output is correct |
33 |
Correct |
16 ms |
14584 KB |
Output is correct |
34 |
Correct |
15 ms |
14844 KB |
Output is correct |
35 |
Correct |
16 ms |
14716 KB |
Output is correct |
36 |
Correct |
15 ms |
14584 KB |
Output is correct |
37 |
Correct |
15 ms |
14584 KB |
Output is correct |
38 |
Correct |
15 ms |
14584 KB |
Output is correct |
39 |
Correct |
131 ms |
22768 KB |
Output is correct |
40 |
Correct |
135 ms |
22884 KB |
Output is correct |
41 |
Correct |
132 ms |
22748 KB |
Output is correct |
42 |
Correct |
140 ms |
22836 KB |
Output is correct |
43 |
Correct |
174 ms |
23160 KB |
Output is correct |
44 |
Correct |
146 ms |
23092 KB |
Output is correct |
45 |
Correct |
106 ms |
24056 KB |
Output is correct |
46 |
Correct |
70 ms |
25720 KB |
Output is correct |
47 |
Correct |
289 ms |
37112 KB |
Output is correct |
48 |
Correct |
371 ms |
49660 KB |
Output is correct |
49 |
Correct |
357 ms |
49812 KB |
Output is correct |
50 |
Correct |
239 ms |
37240 KB |
Output is correct |
51 |
Correct |
232 ms |
37160 KB |
Output is correct |
52 |
Correct |
268 ms |
36920 KB |
Output is correct |
53 |
Correct |
244 ms |
30260 KB |
Output is correct |
54 |
Correct |
412 ms |
43824 KB |
Output is correct |
55 |
Correct |
31 ms |
15736 KB |
Output is correct |
56 |
Correct |
22 ms |
15224 KB |
Output is correct |
57 |
Correct |
79 ms |
22264 KB |
Output is correct |
58 |
Correct |
77 ms |
27628 KB |
Output is correct |
59 |
Correct |
119 ms |
31904 KB |
Output is correct |
60 |
Correct |
330 ms |
49524 KB |
Output is correct |
61 |
Correct |
291 ms |
39160 KB |
Output is correct |
62 |
Correct |
233 ms |
37368 KB |
Output is correct |
63 |
Correct |
270 ms |
36816 KB |
Output is correct |
64 |
Correct |
565 ms |
39800 KB |
Output is correct |
65 |
Correct |
569 ms |
45288 KB |
Output is correct |
66 |
Correct |
369 ms |
50540 KB |
Output is correct |
67 |
Correct |
233 ms |
44440 KB |
Output is correct |
68 |
Correct |
460 ms |
42292 KB |
Output is correct |
69 |
Correct |
422 ms |
42744 KB |
Output is correct |
70 |
Correct |
416 ms |
41172 KB |
Output is correct |