#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<n;++i)
#define pi pair<int,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
const int inf=1e9;
int ans=inf;
const int N=2e5+5;
map<int,int> z[N];
int add1[N], add2[N];
vector<pi> adj[N];
int k;
void dfs(int u, int p) {
z[u][0]=0;
for(auto&e:adj[u]) {
int v=e.f, w=e.s;
if (v==p) continue;
dfs(v,u);
add1[v]++; add2[v]+=w;
if (z[v].size()>z[u].size()) {
swap(z[u],z[v]);
swap(add1[u],add1[v]);
swap(add2[u],add2[v]);
}
for(auto&it:z[v]) {
int x=it.f, y=it.s;
//x + add2[v] + add2[u] + len = k
if (z[u].count(k-x-add2[v]-add2[u])) {
ans=min(ans,z[u][k-x-add2[v]-add2[u]]+add1[u]+add1[v]+y);
}
}
for(auto&it:z[v]) {
int x=it.f, y=it.s;
if (z[u].count(x+add2[v]-add2[u])) {
z[u][x+add2[v]-add2[u]]=min(z[u][x+add2[v]-add2[u]],y+add1[v]-add1[u]);
} else {
z[u][x+add2[v]-add2[u]]=y+add1[v]-add1[u];
}
}
}
}
int best_path(int n, int _k, int h[][2], int l[]) {
k=_k;
forn(i,n-1) {
int u=h[i][0], v=h[i][1];
adj[u].pb({v,l[i]}); adj[v].pb({u,l[i]});
}
dfs(0,-1);
if (ans==inf) return -1;
else return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14368 KB |
Output is correct |
2 |
Correct |
6 ms |
14352 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 |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14408 KB |
Output is correct |
8 |
Correct |
6 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14404 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14420 KB |
Output is correct |
12 |
Correct |
6 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14420 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
6 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
6 ms |
14408 KB |
Output is correct |
18 |
Correct |
7 ms |
14404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14368 KB |
Output is correct |
2 |
Correct |
6 ms |
14352 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 |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14408 KB |
Output is correct |
8 |
Correct |
6 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14404 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14420 KB |
Output is correct |
12 |
Correct |
6 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14420 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
6 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
6 ms |
14408 KB |
Output is correct |
18 |
Correct |
7 ms |
14404 KB |
Output is correct |
19 |
Correct |
6 ms |
14408 KB |
Output is correct |
20 |
Correct |
6 ms |
14404 KB |
Output is correct |
21 |
Correct |
7 ms |
14540 KB |
Output is correct |
22 |
Correct |
7 ms |
14676 KB |
Output is correct |
23 |
Correct |
7 ms |
14676 KB |
Output is correct |
24 |
Correct |
7 ms |
14548 KB |
Output is correct |
25 |
Correct |
7 ms |
14624 KB |
Output is correct |
26 |
Correct |
7 ms |
14676 KB |
Output is correct |
27 |
Correct |
7 ms |
14548 KB |
Output is correct |
28 |
Correct |
7 ms |
14548 KB |
Output is correct |
29 |
Correct |
7 ms |
14676 KB |
Output is correct |
30 |
Correct |
7 ms |
14676 KB |
Output is correct |
31 |
Correct |
7 ms |
14676 KB |
Output is correct |
32 |
Correct |
7 ms |
14548 KB |
Output is correct |
33 |
Correct |
7 ms |
14676 KB |
Output is correct |
34 |
Correct |
7 ms |
14548 KB |
Output is correct |
35 |
Correct |
7 ms |
14540 KB |
Output is correct |
36 |
Correct |
7 ms |
14616 KB |
Output is correct |
37 |
Correct |
9 ms |
14592 KB |
Output is correct |
38 |
Correct |
7 ms |
14548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14368 KB |
Output is correct |
2 |
Correct |
6 ms |
14352 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 |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14408 KB |
Output is correct |
8 |
Correct |
6 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14404 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14420 KB |
Output is correct |
12 |
Correct |
6 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14420 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
6 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
6 ms |
14408 KB |
Output is correct |
18 |
Correct |
7 ms |
14404 KB |
Output is correct |
19 |
Correct |
94 ms |
32488 KB |
Output is correct |
20 |
Correct |
87 ms |
32472 KB |
Output is correct |
21 |
Correct |
88 ms |
32188 KB |
Output is correct |
22 |
Correct |
100 ms |
32044 KB |
Output is correct |
23 |
Correct |
122 ms |
41548 KB |
Output is correct |
24 |
Correct |
96 ms |
34544 KB |
Output is correct |
25 |
Correct |
74 ms |
33640 KB |
Output is correct |
26 |
Correct |
48 ms |
40552 KB |
Output is correct |
27 |
Correct |
139 ms |
40888 KB |
Output is correct |
28 |
Correct |
207 ms |
75272 KB |
Output is correct |
29 |
Correct |
222 ms |
73840 KB |
Output is correct |
30 |
Correct |
133 ms |
40940 KB |
Output is correct |
31 |
Correct |
135 ms |
40916 KB |
Output is correct |
32 |
Correct |
180 ms |
41012 KB |
Output is correct |
33 |
Correct |
177 ms |
44328 KB |
Output is correct |
34 |
Correct |
261 ms |
68324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14368 KB |
Output is correct |
2 |
Correct |
6 ms |
14352 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 |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14408 KB |
Output is correct |
8 |
Correct |
6 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14404 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14420 KB |
Output is correct |
12 |
Correct |
6 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14420 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
6 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
6 ms |
14408 KB |
Output is correct |
18 |
Correct |
7 ms |
14404 KB |
Output is correct |
19 |
Correct |
6 ms |
14408 KB |
Output is correct |
20 |
Correct |
6 ms |
14404 KB |
Output is correct |
21 |
Correct |
7 ms |
14540 KB |
Output is correct |
22 |
Correct |
7 ms |
14676 KB |
Output is correct |
23 |
Correct |
7 ms |
14676 KB |
Output is correct |
24 |
Correct |
7 ms |
14548 KB |
Output is correct |
25 |
Correct |
7 ms |
14624 KB |
Output is correct |
26 |
Correct |
7 ms |
14676 KB |
Output is correct |
27 |
Correct |
7 ms |
14548 KB |
Output is correct |
28 |
Correct |
7 ms |
14548 KB |
Output is correct |
29 |
Correct |
7 ms |
14676 KB |
Output is correct |
30 |
Correct |
7 ms |
14676 KB |
Output is correct |
31 |
Correct |
7 ms |
14676 KB |
Output is correct |
32 |
Correct |
7 ms |
14548 KB |
Output is correct |
33 |
Correct |
7 ms |
14676 KB |
Output is correct |
34 |
Correct |
7 ms |
14548 KB |
Output is correct |
35 |
Correct |
7 ms |
14540 KB |
Output is correct |
36 |
Correct |
7 ms |
14616 KB |
Output is correct |
37 |
Correct |
9 ms |
14592 KB |
Output is correct |
38 |
Correct |
7 ms |
14548 KB |
Output is correct |
39 |
Correct |
94 ms |
32488 KB |
Output is correct |
40 |
Correct |
87 ms |
32472 KB |
Output is correct |
41 |
Correct |
88 ms |
32188 KB |
Output is correct |
42 |
Correct |
100 ms |
32044 KB |
Output is correct |
43 |
Correct |
122 ms |
41548 KB |
Output is correct |
44 |
Correct |
96 ms |
34544 KB |
Output is correct |
45 |
Correct |
74 ms |
33640 KB |
Output is correct |
46 |
Correct |
48 ms |
40552 KB |
Output is correct |
47 |
Correct |
139 ms |
40888 KB |
Output is correct |
48 |
Correct |
207 ms |
75272 KB |
Output is correct |
49 |
Correct |
222 ms |
73840 KB |
Output is correct |
50 |
Correct |
133 ms |
40940 KB |
Output is correct |
51 |
Correct |
135 ms |
40916 KB |
Output is correct |
52 |
Correct |
180 ms |
41012 KB |
Output is correct |
53 |
Correct |
177 ms |
44328 KB |
Output is correct |
54 |
Correct |
261 ms |
68324 KB |
Output is correct |
55 |
Correct |
17 ms |
16980 KB |
Output is correct |
56 |
Correct |
12 ms |
15956 KB |
Output is correct |
57 |
Correct |
58 ms |
30576 KB |
Output is correct |
58 |
Correct |
44 ms |
25664 KB |
Output is correct |
59 |
Correct |
74 ms |
45184 KB |
Output is correct |
60 |
Correct |
211 ms |
74188 KB |
Output is correct |
61 |
Correct |
160 ms |
43420 KB |
Output is correct |
62 |
Correct |
132 ms |
40916 KB |
Output is correct |
63 |
Correct |
172 ms |
40976 KB |
Output is correct |
64 |
Correct |
356 ms |
81868 KB |
Output is correct |
65 |
Correct |
351 ms |
80288 KB |
Output is correct |
66 |
Correct |
221 ms |
70012 KB |
Output is correct |
67 |
Correct |
142 ms |
36516 KB |
Output is correct |
68 |
Correct |
257 ms |
53264 KB |
Output is correct |
69 |
Correct |
286 ms |
57036 KB |
Output is correct |
70 |
Correct |
239 ms |
51656 KB |
Output is correct |