#include "race.h"
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9+42;
#define pii pair<int, int>
int nbNodes, requestedSum;
vector<vector<pii>> voisinDe;
vector<int> profSubtree;
vector<bool> interdit;
int lenSubTree;
int dfs(int nde, int ansc)
{
int ans = 1;
for(pii iV : voisinDe[nde])
{
if(!interdit[iV.first] && iV.first != ansc)
{
ans += dfs(iV.first, nde);
}
}
profSubtree[nde] = ans;
return ans;
}
int getCentroid(int nde, int ansc)
{
for(pii iV : voisinDe[nde])
{
if(!interdit[iV.first] && iV.first!= ansc)
{
if(profSubtree[iV.first] > (lenSubTree)/2)
{
return getCentroid(iV.first, nde);
}
}
}
return nde;
}
vector<pii> minDistToLen;
vector<pii> toUpd;//sum, len;
int timer;
int findLenghts(int nde, int ansc, int sum, int len)
{
//printf("[%d]\n", nde);
int ans = INF;
if(sum > requestedSum)
return INF;
if(sum == requestedSum)
return len;
toUpd.push_back({sum, len});
int sumRemain = requestedSum - sum;
if(minDistToLen[sumRemain].first == timer)
ans = len + minDistToLen[sumRemain].second;
for(pii iV : voisinDe[nde])
{
if(!interdit[iV.first] && iV.first != ansc)
{
ans = min(ans, findLenghts(iV.first, nde, sum + iV.second, len+1));
}
}
return ans;
}
int findMinPath(int nde)
{
//printf("[centroid : %d]", nde);
int ans = INF;
for(pii iV : voisinDe[nde])
{
if(!interdit[iV.first])
{
toUpd.clear();
ans = min(findLenghts(iV.first, nde, iV.second, 1), ans);
for(pii updAct : toUpd)
{
minDistToLen[updAct.first] = min(minDistToLen[updAct.first], {timer ,updAct.second});
}
}
}
return ans;
}
int best_path(int N, int K, int H[][2], int L[])
{
nbNodes = N;
requestedSum = K;
profSubtree.resize(nbNodes);
timer = 2*nbNodes;
interdit.assign(nbNodes, false);
voisinDe.resize(nbNodes);
minDistToLen.assign(1000042, {INF, INF});
for(int i =0; i< nbNodes-1; i++)
{
voisinDe[H[i][0]].push_back({H[i][1], L[i]});
voisinDe[H[i][1]].push_back({H[i][0], L[i]});
}
deque<int> process;
lenSubTree = dfs(0, -1);
int centroid = getCentroid(0, -1);
process.push_back(centroid);
int ansMin = INF;
while (!process.empty())
{
int ndeToProcess = process.front();
process.pop_front();
interdit[ndeToProcess] = true;
int ansAct=findMinPath(ndeToProcess);
ansMin = min(ansMin, ansAct);
timer--;
for(pii iV : voisinDe[ndeToProcess])
{
if(!interdit[iV.first])
{
lenSubTree = dfs(iV.first, -1);
centroid = getCentroid(iV.first, -1);
process.push_back(centroid);
}
}
}
if(ansMin == INF)
return -1;
return ansMin;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
3 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10168 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
3 ms |
10328 KB |
Output is correct |
10 |
Correct |
3 ms |
10328 KB |
Output is correct |
11 |
Correct |
3 ms |
10104 KB |
Output is correct |
12 |
Correct |
3 ms |
10332 KB |
Output is correct |
13 |
Correct |
2 ms |
10332 KB |
Output is correct |
14 |
Correct |
3 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
3 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
3 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10168 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
3 ms |
10328 KB |
Output is correct |
10 |
Correct |
3 ms |
10328 KB |
Output is correct |
11 |
Correct |
3 ms |
10104 KB |
Output is correct |
12 |
Correct |
3 ms |
10332 KB |
Output is correct |
13 |
Correct |
2 ms |
10332 KB |
Output is correct |
14 |
Correct |
3 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
3 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10328 KB |
Output is correct |
19 |
Correct |
4 ms |
10328 KB |
Output is correct |
20 |
Correct |
3 ms |
10332 KB |
Output is correct |
21 |
Correct |
3 ms |
10164 KB |
Output is correct |
22 |
Correct |
3 ms |
10332 KB |
Output is correct |
23 |
Correct |
4 ms |
10332 KB |
Output is correct |
24 |
Correct |
3 ms |
10280 KB |
Output is correct |
25 |
Correct |
3 ms |
10328 KB |
Output is correct |
26 |
Correct |
3 ms |
10332 KB |
Output is correct |
27 |
Correct |
3 ms |
10328 KB |
Output is correct |
28 |
Correct |
3 ms |
10332 KB |
Output is correct |
29 |
Correct |
3 ms |
10332 KB |
Output is correct |
30 |
Correct |
3 ms |
10328 KB |
Output is correct |
31 |
Correct |
4 ms |
10332 KB |
Output is correct |
32 |
Correct |
3 ms |
10332 KB |
Output is correct |
33 |
Correct |
3 ms |
10332 KB |
Output is correct |
34 |
Correct |
3 ms |
10332 KB |
Output is correct |
35 |
Correct |
3 ms |
10332 KB |
Output is correct |
36 |
Correct |
3 ms |
10332 KB |
Output is correct |
37 |
Correct |
3 ms |
10292 KB |
Output is correct |
38 |
Correct |
3 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
3 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10168 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
3 ms |
10328 KB |
Output is correct |
10 |
Correct |
3 ms |
10328 KB |
Output is correct |
11 |
Correct |
3 ms |
10104 KB |
Output is correct |
12 |
Correct |
3 ms |
10332 KB |
Output is correct |
13 |
Correct |
2 ms |
10332 KB |
Output is correct |
14 |
Correct |
3 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
3 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10328 KB |
Output is correct |
19 |
Correct |
93 ms |
19116 KB |
Output is correct |
20 |
Correct |
83 ms |
18860 KB |
Output is correct |
21 |
Correct |
105 ms |
19304 KB |
Output is correct |
22 |
Correct |
81 ms |
19296 KB |
Output is correct |
23 |
Correct |
69 ms |
19284 KB |
Output is correct |
24 |
Correct |
41 ms |
19288 KB |
Output is correct |
25 |
Correct |
87 ms |
22540 KB |
Output is correct |
26 |
Correct |
77 ms |
27024 KB |
Output is correct |
27 |
Correct |
140 ms |
25680 KB |
Output is correct |
28 |
Correct |
186 ms |
40020 KB |
Output is correct |
29 |
Correct |
206 ms |
38852 KB |
Output is correct |
30 |
Correct |
127 ms |
25820 KB |
Output is correct |
31 |
Correct |
116 ms |
25688 KB |
Output is correct |
32 |
Correct |
189 ms |
25680 KB |
Output is correct |
33 |
Correct |
151 ms |
24656 KB |
Output is correct |
34 |
Correct |
176 ms |
24656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
3 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10168 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
3 ms |
10328 KB |
Output is correct |
10 |
Correct |
3 ms |
10328 KB |
Output is correct |
11 |
Correct |
3 ms |
10104 KB |
Output is correct |
12 |
Correct |
3 ms |
10332 KB |
Output is correct |
13 |
Correct |
2 ms |
10332 KB |
Output is correct |
14 |
Correct |
3 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
3 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10328 KB |
Output is correct |
19 |
Correct |
4 ms |
10328 KB |
Output is correct |
20 |
Correct |
3 ms |
10332 KB |
Output is correct |
21 |
Correct |
3 ms |
10164 KB |
Output is correct |
22 |
Correct |
3 ms |
10332 KB |
Output is correct |
23 |
Correct |
4 ms |
10332 KB |
Output is correct |
24 |
Correct |
3 ms |
10280 KB |
Output is correct |
25 |
Correct |
3 ms |
10328 KB |
Output is correct |
26 |
Correct |
3 ms |
10332 KB |
Output is correct |
27 |
Correct |
3 ms |
10328 KB |
Output is correct |
28 |
Correct |
3 ms |
10332 KB |
Output is correct |
29 |
Correct |
3 ms |
10332 KB |
Output is correct |
30 |
Correct |
3 ms |
10328 KB |
Output is correct |
31 |
Correct |
4 ms |
10332 KB |
Output is correct |
32 |
Correct |
3 ms |
10332 KB |
Output is correct |
33 |
Correct |
3 ms |
10332 KB |
Output is correct |
34 |
Correct |
3 ms |
10332 KB |
Output is correct |
35 |
Correct |
3 ms |
10332 KB |
Output is correct |
36 |
Correct |
3 ms |
10332 KB |
Output is correct |
37 |
Correct |
3 ms |
10292 KB |
Output is correct |
38 |
Correct |
3 ms |
10332 KB |
Output is correct |
39 |
Correct |
93 ms |
19116 KB |
Output is correct |
40 |
Correct |
83 ms |
18860 KB |
Output is correct |
41 |
Correct |
105 ms |
19304 KB |
Output is correct |
42 |
Correct |
81 ms |
19296 KB |
Output is correct |
43 |
Correct |
69 ms |
19284 KB |
Output is correct |
44 |
Correct |
41 ms |
19288 KB |
Output is correct |
45 |
Correct |
87 ms |
22540 KB |
Output is correct |
46 |
Correct |
77 ms |
27024 KB |
Output is correct |
47 |
Correct |
140 ms |
25680 KB |
Output is correct |
48 |
Correct |
186 ms |
40020 KB |
Output is correct |
49 |
Correct |
206 ms |
38852 KB |
Output is correct |
50 |
Correct |
127 ms |
25820 KB |
Output is correct |
51 |
Correct |
116 ms |
25688 KB |
Output is correct |
52 |
Correct |
189 ms |
25680 KB |
Output is correct |
53 |
Correct |
151 ms |
24656 KB |
Output is correct |
54 |
Correct |
176 ms |
24656 KB |
Output is correct |
55 |
Correct |
9 ms |
10844 KB |
Output is correct |
56 |
Correct |
9 ms |
11100 KB |
Output is correct |
57 |
Correct |
58 ms |
19672 KB |
Output is correct |
58 |
Correct |
36 ms |
19880 KB |
Output is correct |
59 |
Correct |
76 ms |
27748 KB |
Output is correct |
60 |
Correct |
279 ms |
41932 KB |
Output is correct |
61 |
Correct |
147 ms |
27428 KB |
Output is correct |
62 |
Correct |
129 ms |
27472 KB |
Output is correct |
63 |
Correct |
188 ms |
27452 KB |
Output is correct |
64 |
Correct |
338 ms |
27316 KB |
Output is correct |
65 |
Correct |
164 ms |
28244 KB |
Output is correct |
66 |
Correct |
265 ms |
36568 KB |
Output is correct |
67 |
Correct |
73 ms |
29376 KB |
Output is correct |
68 |
Correct |
186 ms |
28240 KB |
Output is correct |
69 |
Correct |
197 ms |
26448 KB |
Output is correct |
70 |
Correct |
159 ms |
28244 KB |
Output is correct |