#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define MAXN 200010
int n,k;
vector<pair<int,int>> adj[MAXN];
int siz[MAXN];
bool uklonjeno[MAXN];
map<int,int> mapa;
int ans=INT_MAX;
int subtree_size(int node,int pret)
{
siz[node]=1;
int s=adj[node].size();
for (int i=0;i<s;i++)
{
int sled=adj[node][i].first;
if (sled==pret) continue;
if (uklonjeno[sled]==true) continue;
siz[node]+=subtree_size(sled,node);
}
return siz[node];
}
int get_centroid(int node,int pret,int velicina)
{
int s=adj[node].size();
for (int i=0;i<s;i++)
{
int sled=adj[node][i].first;
if (sled==pret) continue;
if (uklonjeno[sled]==true) continue;
if (siz[sled]*2>velicina) return get_centroid(sled,node,velicina);
}
return node;
}
void racunamo(int node,int pret,bool stanje,int dubina,int depth)
{
if (dubina>k) return;
if (stanje==false)
{
int val=k-dubina;
if (mapa.find(val)!=mapa.end()) ans=min(ans,depth+mapa[val]);
}
else
{
if (mapa.find(dubina)==mapa.end()) mapa[dubina]=depth;
else mapa[dubina]=min(mapa[dubina],depth);
}
int s=adj[node].size();
for (int i=0;i<s;i++)
{
int sled=adj[node][i].first;
int tezina=adj[node][i].second;
if (sled==pret) continue;
if (uklonjeno[sled]==true) continue;
racunamo(sled,node,stanje,dubina+tezina,depth+1);
}
}
void centroid_decomposition(int node)
{
subtree_size(node,-1);
int centroida=get_centroid(node,-1,siz[node]);
uklonjeno[centroida]=true;
int s=adj[centroida].size();
for (int i=0;i<s;i++)
{
int sled=adj[centroida][i].first;
int tezina=adj[centroida][i].second;
if (uklonjeno[sled]==true) continue;
racunamo(sled,centroida,false,tezina,1);
racunamo(sled,centroida,true,tezina,1);
}
if (mapa.find(k)!=mapa.end()) ans=min(ans,mapa[k]);
mapa.clear();
for (int i=0;i<s;i++)
{
int sled=adj[centroida][i].first;
if (uklonjeno[sled]==true) continue;
centroid_decomposition(sled);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N,k=K;
for (int i=0;i<n-1;i++)
{
int a=H[i][0],b=H[i][1];
int len=L[i];
adj[a].push_back({b,len});
adj[b].push_back({a,len});
}
centroid_decomposition(0);
if (ans==INT_MAX) return -1;
else return ans;
};
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
1 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9560 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
1 ms |
9564 KB |
Output is correct |
7 |
Correct |
1 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9560 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9584 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9600 KB |
Output is correct |
15 |
Correct |
2 ms |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
1 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9560 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
1 ms |
9564 KB |
Output is correct |
7 |
Correct |
1 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9560 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9584 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9600 KB |
Output is correct |
15 |
Correct |
2 ms |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9560 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
3 ms |
9564 KB |
Output is correct |
22 |
Correct |
3 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9564 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
3 ms |
9820 KB |
Output is correct |
26 |
Correct |
3 ms |
9564 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
3 ms |
9820 KB |
Output is correct |
29 |
Correct |
3 ms |
9820 KB |
Output is correct |
30 |
Correct |
2 ms |
9820 KB |
Output is correct |
31 |
Correct |
3 ms |
9820 KB |
Output is correct |
32 |
Correct |
3 ms |
9820 KB |
Output is correct |
33 |
Correct |
4 ms |
9820 KB |
Output is correct |
34 |
Correct |
2 ms |
9564 KB |
Output is correct |
35 |
Correct |
2 ms |
9820 KB |
Output is correct |
36 |
Correct |
2 ms |
9820 KB |
Output is correct |
37 |
Correct |
2 ms |
9764 KB |
Output is correct |
38 |
Correct |
3 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
1 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9560 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
1 ms |
9564 KB |
Output is correct |
7 |
Correct |
1 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9560 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9584 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9600 KB |
Output is correct |
15 |
Correct |
2 ms |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9560 KB |
Output is correct |
19 |
Correct |
121 ms |
15696 KB |
Output is correct |
20 |
Correct |
116 ms |
16212 KB |
Output is correct |
21 |
Correct |
124 ms |
15960 KB |
Output is correct |
22 |
Correct |
123 ms |
16728 KB |
Output is correct |
23 |
Correct |
50 ms |
16732 KB |
Output is correct |
24 |
Correct |
43 ms |
16732 KB |
Output is correct |
25 |
Correct |
93 ms |
19796 KB |
Output is correct |
26 |
Correct |
69 ms |
23388 KB |
Output is correct |
27 |
Correct |
110 ms |
20448 KB |
Output is correct |
28 |
Correct |
143 ms |
34988 KB |
Output is correct |
29 |
Correct |
143 ms |
33888 KB |
Output is correct |
30 |
Correct |
106 ms |
20816 KB |
Output is correct |
31 |
Correct |
106 ms |
20820 KB |
Output is correct |
32 |
Correct |
111 ms |
20668 KB |
Output is correct |
33 |
Correct |
142 ms |
19792 KB |
Output is correct |
34 |
Correct |
97 ms |
19796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
1 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9560 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
1 ms |
9564 KB |
Output is correct |
7 |
Correct |
1 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9560 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9584 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9600 KB |
Output is correct |
15 |
Correct |
2 ms |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9560 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
3 ms |
9564 KB |
Output is correct |
22 |
Correct |
3 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9564 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
3 ms |
9820 KB |
Output is correct |
26 |
Correct |
3 ms |
9564 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
3 ms |
9820 KB |
Output is correct |
29 |
Correct |
3 ms |
9820 KB |
Output is correct |
30 |
Correct |
2 ms |
9820 KB |
Output is correct |
31 |
Correct |
3 ms |
9820 KB |
Output is correct |
32 |
Correct |
3 ms |
9820 KB |
Output is correct |
33 |
Correct |
4 ms |
9820 KB |
Output is correct |
34 |
Correct |
2 ms |
9564 KB |
Output is correct |
35 |
Correct |
2 ms |
9820 KB |
Output is correct |
36 |
Correct |
2 ms |
9820 KB |
Output is correct |
37 |
Correct |
2 ms |
9764 KB |
Output is correct |
38 |
Correct |
3 ms |
9820 KB |
Output is correct |
39 |
Correct |
121 ms |
15696 KB |
Output is correct |
40 |
Correct |
116 ms |
16212 KB |
Output is correct |
41 |
Correct |
124 ms |
15960 KB |
Output is correct |
42 |
Correct |
123 ms |
16728 KB |
Output is correct |
43 |
Correct |
50 ms |
16732 KB |
Output is correct |
44 |
Correct |
43 ms |
16732 KB |
Output is correct |
45 |
Correct |
93 ms |
19796 KB |
Output is correct |
46 |
Correct |
69 ms |
23388 KB |
Output is correct |
47 |
Correct |
110 ms |
20448 KB |
Output is correct |
48 |
Correct |
143 ms |
34988 KB |
Output is correct |
49 |
Correct |
143 ms |
33888 KB |
Output is correct |
50 |
Correct |
106 ms |
20816 KB |
Output is correct |
51 |
Correct |
106 ms |
20820 KB |
Output is correct |
52 |
Correct |
111 ms |
20668 KB |
Output is correct |
53 |
Correct |
142 ms |
19792 KB |
Output is correct |
54 |
Correct |
97 ms |
19796 KB |
Output is correct |
55 |
Correct |
13 ms |
10332 KB |
Output is correct |
56 |
Correct |
10 ms |
10076 KB |
Output is correct |
57 |
Correct |
85 ms |
16068 KB |
Output is correct |
58 |
Correct |
35 ms |
16452 KB |
Output is correct |
59 |
Correct |
200 ms |
26964 KB |
Output is correct |
60 |
Correct |
494 ms |
42928 KB |
Output is correct |
61 |
Correct |
157 ms |
20532 KB |
Output is correct |
62 |
Correct |
178 ms |
20820 KB |
Output is correct |
63 |
Correct |
191 ms |
20804 KB |
Output is correct |
64 |
Correct |
539 ms |
24404 KB |
Output is correct |
65 |
Correct |
92 ms |
21076 KB |
Output is correct |
66 |
Correct |
339 ms |
30284 KB |
Output is correct |
67 |
Correct |
65 ms |
21696 KB |
Output is correct |
68 |
Correct |
219 ms |
24916 KB |
Output is correct |
69 |
Correct |
234 ms |
23636 KB |
Output is correct |
70 |
Correct |
216 ms |
24400 KB |
Output is correct |