#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define pi pair<int, int>
#define pl pair<ll,ll>
#define all(x) (x).begin(),(x).end()
const int maxn=2e5+10;
vector<vector<pi>> graph(maxn);
vi sze(maxn);
vi cen_num(maxn,-1);
int ans=1e9;
int needed;
unordered_map<int,int> added;
unordered_map<int,int> now;
void dfs2(int cur, int prev, int len, int dist) {
if (now.find(len)==now.end() || now[len]>dist) {
if (len==needed) {
ans=min(ans,dist);
}
else {
now[len]=dist;
}
}
for (auto [to,w]:graph[cur]) {
if (to==prev || len+w>needed || cen_num[to]!=-1) {
continue;
}
dfs2(to,cur,len+w,dist+1);
}
}
void dfs(int cur, int prev, int num, int cur_sze) {
sze[cur]=1;
bool is_cen=1;
for (auto [to,_]:graph[cur]) {
if (to==prev || cen_num[to]!=-1) {
continue;
}
dfs(to,cur,num,cur_sze);
if (cen_num[cur]!=-1) {
return;
}
sze[cur]+=sze[to];
if (sze[to]>cur_sze/2) {
is_cen=0;
}
}
if (!is_cen || cur_sze-sze[cur]>cur_sze/2) {
return;
}
cen_num[cur]=num;
for (auto [to,w]:graph[cur]) {
if (cen_num[to]!=-1 || w>needed) {
continue;
}
dfs2(to,cur,w,1);
if (now.size()>added.size()) {
swap(now,added);
}
for (auto [a,b]:now) {
if (added.find(needed-a)!=added.end() && added[needed-a]+b<ans) {
ans=added[needed-a]+b;
}
}
for (auto [a,b]:now) {
if (added.find(a)==added.end() || added[a]>b) {
added[a]=b;
}
}
now.clear();
}
added.clear();
for (auto [to,_]:graph[cur]) {
if (cen_num[to]!=-1) {
continue;
}
if (prev==to) {
dfs(to,cur,num+1,cur_sze-sze[cur]);
}
else {
dfs(to,cur,num+1,sze[to]);
}
}
}
int best_path(int n, int k, int edge[][2], int len[]) {
for (int i=0; i<n-1; i++) {
graph[edge[i][0]].pb({edge[i][1],len[i]});
graph[edge[i][1]].pb({edge[i][0],len[i]});
}
needed=k;
dfs(0,-1,0,n);
return (ans==1e9?-1:ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6492 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
4 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6496 KB |
Output is correct |
6 |
Correct |
3 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
11 |
Correct |
3 ms |
6736 KB |
Output is correct |
12 |
Correct |
3 ms |
6492 KB |
Output is correct |
13 |
Correct |
3 ms |
6572 KB |
Output is correct |
14 |
Correct |
3 ms |
6492 KB |
Output is correct |
15 |
Correct |
3 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
3 ms |
6524 KB |
Output is correct |
18 |
Correct |
3 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6492 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
4 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6496 KB |
Output is correct |
6 |
Correct |
3 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
11 |
Correct |
3 ms |
6736 KB |
Output is correct |
12 |
Correct |
3 ms |
6492 KB |
Output is correct |
13 |
Correct |
3 ms |
6572 KB |
Output is correct |
14 |
Correct |
3 ms |
6492 KB |
Output is correct |
15 |
Correct |
3 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
3 ms |
6524 KB |
Output is correct |
18 |
Correct |
3 ms |
6748 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
6492 KB |
Output is correct |
21 |
Correct |
4 ms |
6748 KB |
Output is correct |
22 |
Correct |
4 ms |
6748 KB |
Output is correct |
23 |
Correct |
3 ms |
6748 KB |
Output is correct |
24 |
Correct |
3 ms |
6748 KB |
Output is correct |
25 |
Correct |
4 ms |
6628 KB |
Output is correct |
26 |
Correct |
4 ms |
6748 KB |
Output is correct |
27 |
Correct |
5 ms |
6748 KB |
Output is correct |
28 |
Correct |
4 ms |
6748 KB |
Output is correct |
29 |
Correct |
3 ms |
6748 KB |
Output is correct |
30 |
Correct |
3 ms |
6748 KB |
Output is correct |
31 |
Correct |
6 ms |
6608 KB |
Output is correct |
32 |
Correct |
3 ms |
6744 KB |
Output is correct |
33 |
Correct |
4 ms |
6748 KB |
Output is correct |
34 |
Correct |
5 ms |
6808 KB |
Output is correct |
35 |
Correct |
3 ms |
6748 KB |
Output is correct |
36 |
Correct |
3 ms |
6748 KB |
Output is correct |
37 |
Correct |
3 ms |
6748 KB |
Output is correct |
38 |
Correct |
4 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6492 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
4 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6496 KB |
Output is correct |
6 |
Correct |
3 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
11 |
Correct |
3 ms |
6736 KB |
Output is correct |
12 |
Correct |
3 ms |
6492 KB |
Output is correct |
13 |
Correct |
3 ms |
6572 KB |
Output is correct |
14 |
Correct |
3 ms |
6492 KB |
Output is correct |
15 |
Correct |
3 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
3 ms |
6524 KB |
Output is correct |
18 |
Correct |
3 ms |
6748 KB |
Output is correct |
19 |
Correct |
89 ms |
11696 KB |
Output is correct |
20 |
Correct |
84 ms |
12636 KB |
Output is correct |
21 |
Correct |
86 ms |
12124 KB |
Output is correct |
22 |
Correct |
82 ms |
12888 KB |
Output is correct |
23 |
Correct |
54 ms |
13148 KB |
Output is correct |
24 |
Correct |
55 ms |
13000 KB |
Output is correct |
25 |
Correct |
86 ms |
19480 KB |
Output is correct |
26 |
Correct |
70 ms |
25792 KB |
Output is correct |
27 |
Correct |
116 ms |
18040 KB |
Output is correct |
28 |
Correct |
178 ms |
44988 KB |
Output is correct |
29 |
Correct |
172 ms |
43352 KB |
Output is correct |
30 |
Correct |
104 ms |
18816 KB |
Output is correct |
31 |
Correct |
110 ms |
18768 KB |
Output is correct |
32 |
Correct |
130 ms |
18772 KB |
Output is correct |
33 |
Correct |
147 ms |
17956 KB |
Output is correct |
34 |
Correct |
101 ms |
18160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6492 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
4 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6496 KB |
Output is correct |
6 |
Correct |
3 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
11 |
Correct |
3 ms |
6736 KB |
Output is correct |
12 |
Correct |
3 ms |
6492 KB |
Output is correct |
13 |
Correct |
3 ms |
6572 KB |
Output is correct |
14 |
Correct |
3 ms |
6492 KB |
Output is correct |
15 |
Correct |
3 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
3 ms |
6524 KB |
Output is correct |
18 |
Correct |
3 ms |
6748 KB |
Output is correct |
19 |
Correct |
3 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
6492 KB |
Output is correct |
21 |
Correct |
4 ms |
6748 KB |
Output is correct |
22 |
Correct |
4 ms |
6748 KB |
Output is correct |
23 |
Correct |
3 ms |
6748 KB |
Output is correct |
24 |
Correct |
3 ms |
6748 KB |
Output is correct |
25 |
Correct |
4 ms |
6628 KB |
Output is correct |
26 |
Correct |
4 ms |
6748 KB |
Output is correct |
27 |
Correct |
5 ms |
6748 KB |
Output is correct |
28 |
Correct |
4 ms |
6748 KB |
Output is correct |
29 |
Correct |
3 ms |
6748 KB |
Output is correct |
30 |
Correct |
3 ms |
6748 KB |
Output is correct |
31 |
Correct |
6 ms |
6608 KB |
Output is correct |
32 |
Correct |
3 ms |
6744 KB |
Output is correct |
33 |
Correct |
4 ms |
6748 KB |
Output is correct |
34 |
Correct |
5 ms |
6808 KB |
Output is correct |
35 |
Correct |
3 ms |
6748 KB |
Output is correct |
36 |
Correct |
3 ms |
6748 KB |
Output is correct |
37 |
Correct |
3 ms |
6748 KB |
Output is correct |
38 |
Correct |
4 ms |
6748 KB |
Output is correct |
39 |
Correct |
89 ms |
11696 KB |
Output is correct |
40 |
Correct |
84 ms |
12636 KB |
Output is correct |
41 |
Correct |
86 ms |
12124 KB |
Output is correct |
42 |
Correct |
82 ms |
12888 KB |
Output is correct |
43 |
Correct |
54 ms |
13148 KB |
Output is correct |
44 |
Correct |
55 ms |
13000 KB |
Output is correct |
45 |
Correct |
86 ms |
19480 KB |
Output is correct |
46 |
Correct |
70 ms |
25792 KB |
Output is correct |
47 |
Correct |
116 ms |
18040 KB |
Output is correct |
48 |
Correct |
178 ms |
44988 KB |
Output is correct |
49 |
Correct |
172 ms |
43352 KB |
Output is correct |
50 |
Correct |
104 ms |
18816 KB |
Output is correct |
51 |
Correct |
110 ms |
18768 KB |
Output is correct |
52 |
Correct |
130 ms |
18772 KB |
Output is correct |
53 |
Correct |
147 ms |
17956 KB |
Output is correct |
54 |
Correct |
101 ms |
18160 KB |
Output is correct |
55 |
Correct |
16 ms |
7276 KB |
Output is correct |
56 |
Correct |
8 ms |
7004 KB |
Output is correct |
57 |
Correct |
57 ms |
11992 KB |
Output is correct |
58 |
Correct |
26 ms |
12752 KB |
Output is correct |
59 |
Correct |
2246 ms |
31232 KB |
Output is correct |
60 |
Execution timed out |
3057 ms |
57800 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |