#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
const static ll INF = 1e18;
const static ll MAXN = 200001;
static ll N, sumNeed;
static vector<pair<ll, ll>> adj[MAXN];
static ll depth[MAXN], sum[MAXN];
static map<ll, ll> maps[MAXN];
static ll ans = INF;
void dfs (ll node = 0, ll parent = -1) {
maps[node][sum[node]] = depth[node];
for (pair<ll, ll> nxt : adj[node]) {
ll nodenxt = nxt.f, weightnxt = nxt.s;
if (nodenxt == parent) continue;
depth[nodenxt] = depth[node] + 1;
sum[nodenxt] = sum[node] + weightnxt;
dfs(nodenxt, node);
if (maps[nodenxt].size() > maps[node].size()) swap(maps[nodenxt], maps[node]);
for (pair<ll, ll> elem : maps[nodenxt]) {
ll sumOn = elem.f, depthOn = elem.s;
ll need = -sumOn + 2*sum[node] + sumNeed;
if (maps[node].find(need) != maps[node].end()) {
ans = min(ans, maps[node][need] - 2*depth[node] + depthOn);
}
}
for (pair<ll, ll> elem : maps[nodenxt]) {
ll sumOn = elem.f, depthOn = elem.s;
ll need = -sumOn + 2*sum[node] + sumNeed;
if (maps[node].find(sumOn) == maps[node].end()) {
maps[node][sumOn] = depthOn;
}
else {
maps[node][sumOn] = min(maps[node][sumOn], depthOn);
}
}
maps[nodenxt].clear();
}
}
int best_path(int N, int K, int H[][2], int L[])
{
::N = (ll) N;
sumNeed = (ll) K;
if (sumNeed == 1) return 0;
for (int i = 0; i < N-1; i++) {
adj[H[i][0]].push_back({(ll) H[i][1], (ll) L[i]});
adj[H[i][1]].push_back({(ll) H[i][0], (ll) L[i]});
}
dfs();
if (ans == INF) return -1;
else return (int) ans;
}
Compilation message
race.cpp: In function 'void dfs(long long int, long long int)':
race.cpp:42:7: warning: unused variable 'need' [-Wunused-variable]
42 | ll need = -sumOn + 2*sum[node] + sumNeed;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
2 ms |
21084 KB |
Output is correct |
4 |
Correct |
2 ms |
21084 KB |
Output is correct |
5 |
Correct |
2 ms |
21148 KB |
Output is correct |
6 |
Correct |
2 ms |
21084 KB |
Output is correct |
7 |
Correct |
2 ms |
21084 KB |
Output is correct |
8 |
Correct |
2 ms |
21176 KB |
Output is correct |
9 |
Correct |
3 ms |
21084 KB |
Output is correct |
10 |
Correct |
3 ms |
21084 KB |
Output is correct |
11 |
Correct |
2 ms |
21084 KB |
Output is correct |
12 |
Correct |
2 ms |
21084 KB |
Output is correct |
13 |
Correct |
2 ms |
21084 KB |
Output is correct |
14 |
Correct |
2 ms |
21084 KB |
Output is correct |
15 |
Correct |
2 ms |
21176 KB |
Output is correct |
16 |
Correct |
2 ms |
21084 KB |
Output is correct |
17 |
Correct |
2 ms |
21084 KB |
Output is correct |
18 |
Correct |
2 ms |
21084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
2 ms |
21084 KB |
Output is correct |
4 |
Correct |
2 ms |
21084 KB |
Output is correct |
5 |
Correct |
2 ms |
21148 KB |
Output is correct |
6 |
Correct |
2 ms |
21084 KB |
Output is correct |
7 |
Correct |
2 ms |
21084 KB |
Output is correct |
8 |
Correct |
2 ms |
21176 KB |
Output is correct |
9 |
Correct |
3 ms |
21084 KB |
Output is correct |
10 |
Correct |
3 ms |
21084 KB |
Output is correct |
11 |
Correct |
2 ms |
21084 KB |
Output is correct |
12 |
Correct |
2 ms |
21084 KB |
Output is correct |
13 |
Correct |
2 ms |
21084 KB |
Output is correct |
14 |
Correct |
2 ms |
21084 KB |
Output is correct |
15 |
Correct |
2 ms |
21176 KB |
Output is correct |
16 |
Correct |
2 ms |
21084 KB |
Output is correct |
17 |
Correct |
2 ms |
21084 KB |
Output is correct |
18 |
Correct |
2 ms |
21084 KB |
Output is correct |
19 |
Correct |
3 ms |
21080 KB |
Output is correct |
20 |
Correct |
2 ms |
21084 KB |
Output is correct |
21 |
Correct |
3 ms |
21376 KB |
Output is correct |
22 |
Correct |
3 ms |
21340 KB |
Output is correct |
23 |
Correct |
3 ms |
21340 KB |
Output is correct |
24 |
Correct |
3 ms |
21340 KB |
Output is correct |
25 |
Correct |
3 ms |
21340 KB |
Output is correct |
26 |
Correct |
3 ms |
21340 KB |
Output is correct |
27 |
Correct |
3 ms |
21280 KB |
Output is correct |
28 |
Correct |
3 ms |
21340 KB |
Output is correct |
29 |
Correct |
3 ms |
21340 KB |
Output is correct |
30 |
Correct |
3 ms |
21340 KB |
Output is correct |
31 |
Correct |
3 ms |
21424 KB |
Output is correct |
32 |
Correct |
3 ms |
21372 KB |
Output is correct |
33 |
Correct |
3 ms |
21340 KB |
Output is correct |
34 |
Correct |
3 ms |
21336 KB |
Output is correct |
35 |
Correct |
3 ms |
21340 KB |
Output is correct |
36 |
Correct |
3 ms |
21340 KB |
Output is correct |
37 |
Correct |
4 ms |
21340 KB |
Output is correct |
38 |
Correct |
3 ms |
21340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
2 ms |
21084 KB |
Output is correct |
4 |
Correct |
2 ms |
21084 KB |
Output is correct |
5 |
Correct |
2 ms |
21148 KB |
Output is correct |
6 |
Correct |
2 ms |
21084 KB |
Output is correct |
7 |
Correct |
2 ms |
21084 KB |
Output is correct |
8 |
Correct |
2 ms |
21176 KB |
Output is correct |
9 |
Correct |
3 ms |
21084 KB |
Output is correct |
10 |
Correct |
3 ms |
21084 KB |
Output is correct |
11 |
Correct |
2 ms |
21084 KB |
Output is correct |
12 |
Correct |
2 ms |
21084 KB |
Output is correct |
13 |
Correct |
2 ms |
21084 KB |
Output is correct |
14 |
Correct |
2 ms |
21084 KB |
Output is correct |
15 |
Correct |
2 ms |
21176 KB |
Output is correct |
16 |
Correct |
2 ms |
21084 KB |
Output is correct |
17 |
Correct |
2 ms |
21084 KB |
Output is correct |
18 |
Correct |
2 ms |
21084 KB |
Output is correct |
19 |
Correct |
52 ms |
28656 KB |
Output is correct |
20 |
Correct |
53 ms |
28504 KB |
Output is correct |
21 |
Correct |
46 ms |
28504 KB |
Output is correct |
22 |
Correct |
52 ms |
28496 KB |
Output is correct |
23 |
Correct |
74 ms |
29220 KB |
Output is correct |
24 |
Correct |
57 ms |
28252 KB |
Output is correct |
25 |
Correct |
44 ms |
40784 KB |
Output is correct |
26 |
Correct |
40 ms |
51468 KB |
Output is correct |
27 |
Correct |
95 ms |
34528 KB |
Output is correct |
28 |
Correct |
154 ms |
79700 KB |
Output is correct |
29 |
Correct |
162 ms |
78044 KB |
Output is correct |
30 |
Correct |
95 ms |
34388 KB |
Output is correct |
31 |
Correct |
91 ms |
34528 KB |
Output is correct |
32 |
Correct |
95 ms |
34632 KB |
Output is correct |
33 |
Correct |
108 ms |
33876 KB |
Output is correct |
34 |
Correct |
181 ms |
46420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
2 ms |
21084 KB |
Output is correct |
4 |
Correct |
2 ms |
21084 KB |
Output is correct |
5 |
Correct |
2 ms |
21148 KB |
Output is correct |
6 |
Correct |
2 ms |
21084 KB |
Output is correct |
7 |
Correct |
2 ms |
21084 KB |
Output is correct |
8 |
Correct |
2 ms |
21176 KB |
Output is correct |
9 |
Correct |
3 ms |
21084 KB |
Output is correct |
10 |
Correct |
3 ms |
21084 KB |
Output is correct |
11 |
Correct |
2 ms |
21084 KB |
Output is correct |
12 |
Correct |
2 ms |
21084 KB |
Output is correct |
13 |
Correct |
2 ms |
21084 KB |
Output is correct |
14 |
Correct |
2 ms |
21084 KB |
Output is correct |
15 |
Correct |
2 ms |
21176 KB |
Output is correct |
16 |
Correct |
2 ms |
21084 KB |
Output is correct |
17 |
Correct |
2 ms |
21084 KB |
Output is correct |
18 |
Correct |
2 ms |
21084 KB |
Output is correct |
19 |
Correct |
3 ms |
21080 KB |
Output is correct |
20 |
Correct |
2 ms |
21084 KB |
Output is correct |
21 |
Correct |
3 ms |
21376 KB |
Output is correct |
22 |
Correct |
3 ms |
21340 KB |
Output is correct |
23 |
Correct |
3 ms |
21340 KB |
Output is correct |
24 |
Correct |
3 ms |
21340 KB |
Output is correct |
25 |
Correct |
3 ms |
21340 KB |
Output is correct |
26 |
Correct |
3 ms |
21340 KB |
Output is correct |
27 |
Correct |
3 ms |
21280 KB |
Output is correct |
28 |
Correct |
3 ms |
21340 KB |
Output is correct |
29 |
Correct |
3 ms |
21340 KB |
Output is correct |
30 |
Correct |
3 ms |
21340 KB |
Output is correct |
31 |
Correct |
3 ms |
21424 KB |
Output is correct |
32 |
Correct |
3 ms |
21372 KB |
Output is correct |
33 |
Correct |
3 ms |
21340 KB |
Output is correct |
34 |
Correct |
3 ms |
21336 KB |
Output is correct |
35 |
Correct |
3 ms |
21340 KB |
Output is correct |
36 |
Correct |
3 ms |
21340 KB |
Output is correct |
37 |
Correct |
4 ms |
21340 KB |
Output is correct |
38 |
Correct |
3 ms |
21340 KB |
Output is correct |
39 |
Correct |
52 ms |
28656 KB |
Output is correct |
40 |
Correct |
53 ms |
28504 KB |
Output is correct |
41 |
Correct |
46 ms |
28504 KB |
Output is correct |
42 |
Correct |
52 ms |
28496 KB |
Output is correct |
43 |
Correct |
74 ms |
29220 KB |
Output is correct |
44 |
Correct |
57 ms |
28252 KB |
Output is correct |
45 |
Correct |
44 ms |
40784 KB |
Output is correct |
46 |
Correct |
40 ms |
51468 KB |
Output is correct |
47 |
Correct |
95 ms |
34528 KB |
Output is correct |
48 |
Correct |
154 ms |
79700 KB |
Output is correct |
49 |
Correct |
162 ms |
78044 KB |
Output is correct |
50 |
Correct |
95 ms |
34388 KB |
Output is correct |
51 |
Correct |
91 ms |
34528 KB |
Output is correct |
52 |
Correct |
95 ms |
34632 KB |
Output is correct |
53 |
Correct |
108 ms |
33876 KB |
Output is correct |
54 |
Correct |
181 ms |
46420 KB |
Output is correct |
55 |
Correct |
10 ms |
22108 KB |
Output is correct |
56 |
Correct |
6 ms |
21848 KB |
Output is correct |
57 |
Correct |
40 ms |
28900 KB |
Output is correct |
58 |
Correct |
26 ms |
29132 KB |
Output is correct |
59 |
Correct |
58 ms |
52828 KB |
Output is correct |
60 |
Correct |
158 ms |
81488 KB |
Output is correct |
61 |
Correct |
121 ms |
39252 KB |
Output is correct |
62 |
Correct |
93 ms |
37456 KB |
Output is correct |
63 |
Correct |
97 ms |
37456 KB |
Output is correct |
64 |
Correct |
233 ms |
45768 KB |
Output is correct |
65 |
Correct |
224 ms |
50260 KB |
Output is correct |
66 |
Correct |
162 ms |
77652 KB |
Output is correct |
67 |
Correct |
77 ms |
37060 KB |
Output is correct |
68 |
Correct |
183 ms |
47952 KB |
Output is correct |
69 |
Correct |
193 ms |
52304 KB |
Output is correct |
70 |
Correct |
167 ms |
46932 KB |
Output is correct |