#include <bits/stdc++.h>
using namespace std;
void fastIO(){
ios_base::sync_with_stdio(false);
cin.tie(0);
}
typedef long long ll;
vector<pair<int, int> > edges[200100];
int sub[200100];
bool rem[200100];
int LEN;
void dfs(int node, int pa = 0){
sub[node] = 1;
for (pair<int, int> nx : edges[node]){
if (nx.first == pa || rem[nx.first])
continue;
dfs(nx.first, node);
sub[node] += sub[nx.first];
}
}
int centroid(int node, int sz, int pa = 0){
for (pair<int, int> nx : edges[node]){
if (nx.first == pa || rem[nx.first])
continue;
if (sub[nx.first] > sz/2)
return centroid(nx.first, sz, node);
}
return node;
}
void dfs2(int node, ll dep, int cur, map<ll, int> &mp, int pa = 0){
if (mp.count(dep))
mp[dep] = min(mp[dep], cur);
else
mp[dep] = cur;
for (pair<int, int> nx : edges[node]){
if (nx.first == pa || rem[nx.first])
continue;
dfs2(nx.first, dep+nx.second, cur+1, mp, node);
}
}
int INF = 0x3f3f3f3f;
int decomp(int node){
dfs(node);
int cent = centroid(node, sub[node]);
rem[cent] = true;
map<ll, int> mp;
int curmin = INF;
mp[0] = 0;
for (pair<int, int> nx : edges[cent]){
if (rem[nx.first])
continue;
map<ll, int> tmp;
dfs2(nx.first, nx.second, 1, tmp);
for (auto it = tmp.begin(); it != tmp.end(); it++){
if (mp.count(LEN-(*it).first))
curmin = min(curmin, (*it).second+mp[LEN-(*it).first]);
}
for (auto it = tmp.begin(); it != tmp.end(); it++){
if (!mp.count((*it).first))
mp[(*it).first] = (*it).second;
else
mp[(*it).first] = min(mp[(*it).first], (*it).second);
}
}
int totmin = curmin;
for (pair<int, int> nx : edges[cent]){
if (rem[nx.first])
continue;
int val = decomp(nx.first);
if (val == -1)
continue;
totmin = min(totmin, val);
}
if (totmin == INF)
return -1;
return totmin;
}
int best_path(int N, int K, int H[][2], int *L){
LEN = K;
for (int i = 0; i < N-1; i++){
int a = H[i][0], b = H[i][1];
a++, b++;
edges[a].push_back({b, L[i]});
edges[b].push_back({a, L[i]});
}
return decomp(1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4980 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4980 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
5196 KB |
Output is correct |
23 |
Correct |
6 ms |
5144 KB |
Output is correct |
24 |
Correct |
6 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5196 KB |
Output is correct |
26 |
Correct |
5 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5196 KB |
Output is correct |
29 |
Correct |
5 ms |
5196 KB |
Output is correct |
30 |
Correct |
5 ms |
5148 KB |
Output is correct |
31 |
Correct |
5 ms |
5148 KB |
Output is correct |
32 |
Correct |
6 ms |
5196 KB |
Output is correct |
33 |
Correct |
6 ms |
5196 KB |
Output is correct |
34 |
Correct |
6 ms |
5196 KB |
Output is correct |
35 |
Correct |
5 ms |
5068 KB |
Output is correct |
36 |
Correct |
5 ms |
5196 KB |
Output is correct |
37 |
Correct |
5 ms |
5068 KB |
Output is correct |
38 |
Correct |
5 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4980 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
288 ms |
11808 KB |
Output is correct |
20 |
Correct |
287 ms |
11668 KB |
Output is correct |
21 |
Correct |
288 ms |
11812 KB |
Output is correct |
22 |
Correct |
248 ms |
11864 KB |
Output is correct |
23 |
Correct |
449 ms |
12344 KB |
Output is correct |
24 |
Correct |
193 ms |
11972 KB |
Output is correct |
25 |
Correct |
231 ms |
16436 KB |
Output is correct |
26 |
Correct |
106 ms |
18896 KB |
Output is correct |
27 |
Correct |
439 ms |
19172 KB |
Output is correct |
28 |
Correct |
1517 ms |
58104 KB |
Output is correct |
29 |
Correct |
1479 ms |
57028 KB |
Output is correct |
30 |
Correct |
430 ms |
19140 KB |
Output is correct |
31 |
Correct |
435 ms |
19140 KB |
Output is correct |
32 |
Correct |
508 ms |
19296 KB |
Output is correct |
33 |
Correct |
674 ms |
18300 KB |
Output is correct |
34 |
Correct |
1374 ms |
40548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4980 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
5196 KB |
Output is correct |
23 |
Correct |
6 ms |
5144 KB |
Output is correct |
24 |
Correct |
6 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5196 KB |
Output is correct |
26 |
Correct |
5 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5196 KB |
Output is correct |
29 |
Correct |
5 ms |
5196 KB |
Output is correct |
30 |
Correct |
5 ms |
5148 KB |
Output is correct |
31 |
Correct |
5 ms |
5148 KB |
Output is correct |
32 |
Correct |
6 ms |
5196 KB |
Output is correct |
33 |
Correct |
6 ms |
5196 KB |
Output is correct |
34 |
Correct |
6 ms |
5196 KB |
Output is correct |
35 |
Correct |
5 ms |
5068 KB |
Output is correct |
36 |
Correct |
5 ms |
5196 KB |
Output is correct |
37 |
Correct |
5 ms |
5068 KB |
Output is correct |
38 |
Correct |
5 ms |
5068 KB |
Output is correct |
39 |
Correct |
288 ms |
11808 KB |
Output is correct |
40 |
Correct |
287 ms |
11668 KB |
Output is correct |
41 |
Correct |
288 ms |
11812 KB |
Output is correct |
42 |
Correct |
248 ms |
11864 KB |
Output is correct |
43 |
Correct |
449 ms |
12344 KB |
Output is correct |
44 |
Correct |
193 ms |
11972 KB |
Output is correct |
45 |
Correct |
231 ms |
16436 KB |
Output is correct |
46 |
Correct |
106 ms |
18896 KB |
Output is correct |
47 |
Correct |
439 ms |
19172 KB |
Output is correct |
48 |
Correct |
1517 ms |
58104 KB |
Output is correct |
49 |
Correct |
1479 ms |
57028 KB |
Output is correct |
50 |
Correct |
430 ms |
19140 KB |
Output is correct |
51 |
Correct |
435 ms |
19140 KB |
Output is correct |
52 |
Correct |
508 ms |
19296 KB |
Output is correct |
53 |
Correct |
674 ms |
18300 KB |
Output is correct |
54 |
Correct |
1374 ms |
40548 KB |
Output is correct |
55 |
Correct |
37 ms |
6360 KB |
Output is correct |
56 |
Correct |
22 ms |
5700 KB |
Output is correct |
57 |
Correct |
180 ms |
11916 KB |
Output is correct |
58 |
Correct |
60 ms |
11588 KB |
Output is correct |
59 |
Correct |
546 ms |
28420 KB |
Output is correct |
60 |
Correct |
1571 ms |
55368 KB |
Output is correct |
61 |
Correct |
504 ms |
20932 KB |
Output is correct |
62 |
Correct |
431 ms |
19140 KB |
Output is correct |
63 |
Correct |
495 ms |
19140 KB |
Output is correct |
64 |
Correct |
1406 ms |
33972 KB |
Output is correct |
65 |
Correct |
1222 ms |
41556 KB |
Output is correct |
66 |
Correct |
1537 ms |
54684 KB |
Output is correct |
67 |
Correct |
185 ms |
19504 KB |
Output is correct |
68 |
Correct |
715 ms |
35372 KB |
Output is correct |
69 |
Correct |
728 ms |
35992 KB |
Output is correct |
70 |
Correct |
644 ms |
34140 KB |
Output is correct |