#include "race.h"
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 200000 + 10, MAXK = 1000000 + 10, INF = 100000000;
vector<int> g[MAXN], p[MAXN];
int marc[MAXK], lastSave[MAXK], removed[MAXN], sub[MAXN];
int ans = INF, cnt, globalK;
int debug = 0;
int readMarc(int pos){
if(pos < 0 || pos > MAXK) return INF;
if(lastSave[pos] == cnt) return marc[pos];
else return INF;
}
void writeMarc(int pos, int val){
marc[pos] = val; lastSave[pos] = cnt;
}
void calcSub(int v, int f){
sub[v] = 1;
for(int i = 0;i<(int)g[v].size();i++){
int viz = g[v][i];
if(viz == f || removed[viz] == 1) continue;
calcSub(viz, v);
sub[v] += sub[viz];
}
return;
}
int findCentroid(int v, int f){
int centroid = v;
for(int i = 0;i<(int)g[v].size();i++){
int viz = g[v][i];
if(removed[viz] == 1 || viz == f) continue;
if(sub[viz] > sub[v]/2){
sub[v] -= sub[viz];
sub[viz] += sub[v];
centroid = findCentroid(viz, v);
}
}
return centroid;
}
void computeSubtree(int v, int f, int dist, int edgeCount){
if(dist == globalK){
ans = min(ans, edgeCount);
return;
}
int possible = readMarc(globalK - dist);
if(possible != INF){
ans = min(ans, possible + edgeCount);
}
for(int i = 0;i<(int)g[v].size();i++){
int viz = g[v][i];
if(viz == f || removed[viz] == 1) continue;
computeSubtree(viz, v, dist + p[v][i], edgeCount + 1);
}
if(dist <= MAXK && readMarc(dist) > edgeCount) writeMarc(dist, edgeCount);
}
void doTree(int v){
calcSub(v, -1);
int centroid = findCentroid(v, -1);
removed[centroid] = 1;
if(sub[centroid] == 1) return;
cnt++;
for(int i = 0;i<(int)g[centroid].size();i++){
int viz = g[centroid][i];
if(removed[viz] == 1) continue;
computeSubtree(viz, centroid, p[centroid][i], 1);
}
for(int i = 0;i<(int)g[centroid].size();i++){
int viz = g[centroid][i];
if(removed[viz] == 1) continue;
doTree(viz);
}
}
int best_path(int N, int K, int H[][2], int L[]){
globalK = K;
for(int i = 0;i<N-1;i++){
int a = H[i][0], b = H[i][1];
g[a].push_back(b); g[b].push_back(a);
p[a].push_back(L[i]); p[b].push_back(L[i]);
}
calcSub(0,-1);
doTree(0);
if(ans == INF) return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9740 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
11 |
Correct |
5 ms |
9676 KB |
Output is correct |
12 |
Correct |
4 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9676 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9676 KB |
Output is correct |
16 |
Correct |
5 ms |
9660 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9740 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
11 |
Correct |
5 ms |
9676 KB |
Output is correct |
12 |
Correct |
4 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9676 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9676 KB |
Output is correct |
16 |
Correct |
5 ms |
9660 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
5 ms |
9676 KB |
Output is correct |
20 |
Correct |
5 ms |
9676 KB |
Output is correct |
21 |
Correct |
6 ms |
9764 KB |
Output is correct |
22 |
Correct |
10 ms |
15184 KB |
Output is correct |
23 |
Correct |
10 ms |
15820 KB |
Output is correct |
24 |
Correct |
10 ms |
16324 KB |
Output is correct |
25 |
Correct |
8 ms |
14028 KB |
Output is correct |
26 |
Correct |
10 ms |
17436 KB |
Output is correct |
27 |
Correct |
6 ms |
9712 KB |
Output is correct |
28 |
Correct |
9 ms |
15336 KB |
Output is correct |
29 |
Correct |
9 ms |
14668 KB |
Output is correct |
30 |
Correct |
7 ms |
13124 KB |
Output is correct |
31 |
Correct |
8 ms |
14440 KB |
Output is correct |
32 |
Correct |
8 ms |
14668 KB |
Output is correct |
33 |
Correct |
10 ms |
16972 KB |
Output is correct |
34 |
Correct |
13 ms |
17100 KB |
Output is correct |
35 |
Correct |
10 ms |
17000 KB |
Output is correct |
36 |
Correct |
10 ms |
16728 KB |
Output is correct |
37 |
Correct |
8 ms |
13644 KB |
Output is correct |
38 |
Incorrect |
7 ms |
13032 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9740 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
11 |
Correct |
5 ms |
9676 KB |
Output is correct |
12 |
Correct |
4 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9676 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9676 KB |
Output is correct |
16 |
Correct |
5 ms |
9660 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
661 ms |
17916 KB |
Output is correct |
20 |
Correct |
312 ms |
18148 KB |
Output is correct |
21 |
Correct |
360 ms |
18148 KB |
Output is correct |
22 |
Correct |
238 ms |
18388 KB |
Output is correct |
23 |
Correct |
130 ms |
17988 KB |
Output is correct |
24 |
Correct |
83 ms |
18420 KB |
Output is correct |
25 |
Correct |
138 ms |
22096 KB |
Output is correct |
26 |
Correct |
90 ms |
25940 KB |
Output is correct |
27 |
Correct |
283 ms |
27524 KB |
Output is correct |
28 |
Correct |
517 ms |
49624 KB |
Output is correct |
29 |
Correct |
478 ms |
48432 KB |
Output is correct |
30 |
Correct |
327 ms |
27416 KB |
Output is correct |
31 |
Correct |
273 ms |
27484 KB |
Output is correct |
32 |
Correct |
397 ms |
27504 KB |
Output is correct |
33 |
Correct |
1542 ms |
26744 KB |
Output is correct |
34 |
Correct |
1825 ms |
34080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9740 KB |
Output is correct |
10 |
Correct |
5 ms |
9676 KB |
Output is correct |
11 |
Correct |
5 ms |
9676 KB |
Output is correct |
12 |
Correct |
4 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9676 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9676 KB |
Output is correct |
16 |
Correct |
5 ms |
9660 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Correct |
5 ms |
9676 KB |
Output is correct |
20 |
Correct |
5 ms |
9676 KB |
Output is correct |
21 |
Correct |
6 ms |
9764 KB |
Output is correct |
22 |
Correct |
10 ms |
15184 KB |
Output is correct |
23 |
Correct |
10 ms |
15820 KB |
Output is correct |
24 |
Correct |
10 ms |
16324 KB |
Output is correct |
25 |
Correct |
8 ms |
14028 KB |
Output is correct |
26 |
Correct |
10 ms |
17436 KB |
Output is correct |
27 |
Correct |
6 ms |
9712 KB |
Output is correct |
28 |
Correct |
9 ms |
15336 KB |
Output is correct |
29 |
Correct |
9 ms |
14668 KB |
Output is correct |
30 |
Correct |
7 ms |
13124 KB |
Output is correct |
31 |
Correct |
8 ms |
14440 KB |
Output is correct |
32 |
Correct |
8 ms |
14668 KB |
Output is correct |
33 |
Correct |
10 ms |
16972 KB |
Output is correct |
34 |
Correct |
13 ms |
17100 KB |
Output is correct |
35 |
Correct |
10 ms |
17000 KB |
Output is correct |
36 |
Correct |
10 ms |
16728 KB |
Output is correct |
37 |
Correct |
8 ms |
13644 KB |
Output is correct |
38 |
Incorrect |
7 ms |
13032 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |