#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef pair<int,int> ii;
const int INF = 1e9;
int ans, k;
vector<int> sz, dis, dep;
vector<bool> vis;
vector<vector<ii>> adj;
map<int,int> len, tmp;
void dfs(int cur, int par) {
sz[cur] = 1;
for (auto nx : adj[cur]) {
if (nx.fi == par || vis[nx.fi]) continue;
dfs(nx.fi,cur);
sz[cur] += sz[nx.fi];
}
}
int centroid(int cur, int par, int sub) {
for (auto nx : adj[cur]) {
if (nx.fi == par || vis[nx.fi]) continue;
if (sz[nx.fi]*2 > sub) return centroid(nx.fi,cur,sub);
}
return cur;
}
void getLength(int cur, int par) {
if (!tmp.count(dis[cur])) tmp[dis[cur]] = dep[cur];
else tmp[dis[cur]] = min(tmp[dis[cur]],dep[cur]);
for (auto nx : adj[cur]) {
if (nx.fi == par || vis[nx.fi]) continue;
dis[nx.fi] = dis[cur]+nx.se;
dep[nx.fi] = dep[cur]+1;
getLength(nx.fi,cur);
}
}
void solve(int cur) {
len.clear();
len[0] = dis[cur] = dep[cur] = 0;
for (auto nx : adj[cur]) {
if (vis[nx.fi]) continue;
tmp.clear();
dis[nx.fi] = nx.se;
dep[nx.fi] = 1;
getLength(nx.fi,cur);
// cout << nx.fi << " -> ";
for (auto i : tmp) {
// cout << i.fi << ' ';
if (len.count(k-i.fi)) ans = min(ans,i.se+len[k-i.fi]);
}
// cout << '\n';
for (auto i : tmp) {
if (!len.count(i.fi)) len[i.fi] = i.se;
else len[i.fi] = min(len[i.fi],i.se);
}
}
}
void DnC(int cur) {
dfs(cur,-1);
cur = centroid(cur,-1,sz[cur]);
// cout << cur << " --------------\n";
vis[cur] = 1;
solve(cur);
for (auto nx : adj[cur]) {
if (vis[nx.fi]) continue;
DnC(nx.fi);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
adj = vector<vector<ii>>(N);
for (int i = 0; i < N-1; i++) {
adj[H[i][0]].push_back(ii(H[i][1],L[i]));
adj[H[i][1]].push_back(ii(H[i][0],L[i]));
}
k = K, ans = INF;
vis = vector<bool>(N,0);
sz = dis = dep = vector<int>(N);
DnC(1);
if (ans == INF) return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
3 ms |
332 KB |
Output is correct |
22 |
Correct |
3 ms |
460 KB |
Output is correct |
23 |
Correct |
3 ms |
460 KB |
Output is correct |
24 |
Correct |
3 ms |
460 KB |
Output is correct |
25 |
Correct |
3 ms |
444 KB |
Output is correct |
26 |
Correct |
3 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
3 ms |
460 KB |
Output is correct |
29 |
Correct |
3 ms |
460 KB |
Output is correct |
30 |
Correct |
3 ms |
460 KB |
Output is correct |
31 |
Correct |
3 ms |
332 KB |
Output is correct |
32 |
Correct |
3 ms |
436 KB |
Output is correct |
33 |
Correct |
4 ms |
460 KB |
Output is correct |
34 |
Correct |
3 ms |
436 KB |
Output is correct |
35 |
Correct |
3 ms |
332 KB |
Output is correct |
36 |
Correct |
3 ms |
332 KB |
Output is correct |
37 |
Correct |
3 ms |
332 KB |
Output is correct |
38 |
Correct |
3 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
310 ms |
9948 KB |
Output is correct |
20 |
Correct |
305 ms |
10020 KB |
Output is correct |
21 |
Correct |
290 ms |
10140 KB |
Output is correct |
22 |
Correct |
263 ms |
10244 KB |
Output is correct |
23 |
Correct |
466 ms |
10452 KB |
Output is correct |
24 |
Correct |
200 ms |
10180 KB |
Output is correct |
25 |
Correct |
255 ms |
15716 KB |
Output is correct |
26 |
Correct |
118 ms |
15684 KB |
Output is correct |
27 |
Correct |
437 ms |
20348 KB |
Output is correct |
28 |
Correct |
1558 ms |
44704 KB |
Output is correct |
29 |
Correct |
1522 ms |
40992 KB |
Output is correct |
30 |
Correct |
451 ms |
20528 KB |
Output is correct |
31 |
Correct |
439 ms |
20344 KB |
Output is correct |
32 |
Correct |
523 ms |
20428 KB |
Output is correct |
33 |
Correct |
788 ms |
19064 KB |
Output is correct |
34 |
Correct |
1449 ms |
31652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
3 ms |
332 KB |
Output is correct |
22 |
Correct |
3 ms |
460 KB |
Output is correct |
23 |
Correct |
3 ms |
460 KB |
Output is correct |
24 |
Correct |
3 ms |
460 KB |
Output is correct |
25 |
Correct |
3 ms |
444 KB |
Output is correct |
26 |
Correct |
3 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
3 ms |
460 KB |
Output is correct |
29 |
Correct |
3 ms |
460 KB |
Output is correct |
30 |
Correct |
3 ms |
460 KB |
Output is correct |
31 |
Correct |
3 ms |
332 KB |
Output is correct |
32 |
Correct |
3 ms |
436 KB |
Output is correct |
33 |
Correct |
4 ms |
460 KB |
Output is correct |
34 |
Correct |
3 ms |
436 KB |
Output is correct |
35 |
Correct |
3 ms |
332 KB |
Output is correct |
36 |
Correct |
3 ms |
332 KB |
Output is correct |
37 |
Correct |
3 ms |
332 KB |
Output is correct |
38 |
Correct |
3 ms |
332 KB |
Output is correct |
39 |
Correct |
310 ms |
9948 KB |
Output is correct |
40 |
Correct |
305 ms |
10020 KB |
Output is correct |
41 |
Correct |
290 ms |
10140 KB |
Output is correct |
42 |
Correct |
263 ms |
10244 KB |
Output is correct |
43 |
Correct |
466 ms |
10452 KB |
Output is correct |
44 |
Correct |
200 ms |
10180 KB |
Output is correct |
45 |
Correct |
255 ms |
15716 KB |
Output is correct |
46 |
Correct |
118 ms |
15684 KB |
Output is correct |
47 |
Correct |
437 ms |
20348 KB |
Output is correct |
48 |
Correct |
1558 ms |
44704 KB |
Output is correct |
49 |
Correct |
1522 ms |
40992 KB |
Output is correct |
50 |
Correct |
451 ms |
20528 KB |
Output is correct |
51 |
Correct |
439 ms |
20344 KB |
Output is correct |
52 |
Correct |
523 ms |
20428 KB |
Output is correct |
53 |
Correct |
788 ms |
19064 KB |
Output is correct |
54 |
Correct |
1449 ms |
31652 KB |
Output is correct |
55 |
Correct |
33 ms |
1632 KB |
Output is correct |
56 |
Correct |
19 ms |
1228 KB |
Output is correct |
57 |
Correct |
179 ms |
10180 KB |
Output is correct |
58 |
Correct |
60 ms |
10040 KB |
Output is correct |
59 |
Correct |
539 ms |
21620 KB |
Output is correct |
60 |
Correct |
1667 ms |
40020 KB |
Output is correct |
61 |
Correct |
540 ms |
21792 KB |
Output is correct |
62 |
Correct |
477 ms |
20276 KB |
Output is correct |
63 |
Correct |
565 ms |
20404 KB |
Output is correct |
64 |
Correct |
1740 ms |
27092 KB |
Output is correct |
65 |
Correct |
1515 ms |
30816 KB |
Output is correct |
66 |
Correct |
1731 ms |
43816 KB |
Output is correct |
67 |
Correct |
246 ms |
20868 KB |
Output is correct |
68 |
Correct |
851 ms |
28888 KB |
Output is correct |
69 |
Correct |
871 ms |
29276 KB |
Output is correct |
70 |
Correct |
839 ms |
27636 KB |
Output is correct |