#include <vector>
#include <set>
#include <map>
#include "race.h"
using namespace std;
int const MAXN=2e5+10;
vector<pair<int,int>>nei[MAXN]={};
int reqval=0,ans=1e9+10;
bool dead[MAXN]={};
int sz[MAXN]={};
int h[MAXN]={};
void dfs(int n,int m=-1)
{
sz[n]=1;
for (auto [i,we]:nei[n])
{
if (!dead[i]&&i!=m)
{
dfs(i,n);
sz[n]+=sz[i];
}
}
}
int find_centroid(int n,int m,int root)
{
for (auto [i,we]:nei[n])
{
if (!dead[i]&&i!=m&&sz[i]*2>sz[root])
return find_centroid(i,n,root);
}
return n;
}
map<int,int>f;
vector<pair<int,int>>cur;
void dfs1(int n,int m,int val)
{
cur.push_back({val,h[n]});
if (val>reqval)
return;
if (f.find(reqval-val)!=f.end())
ans=min(ans,h[n]+f[reqval-val]);
for (auto [i,we]:nei[n])
{
if (!dead[i]&&i!=m)
{
h[i]=h[n]+1;
dfs1(i,n,val+we);
}
}
}
void sol(int n)
{
dfs(n);
if (sz[n]==1)
return;
n=find_centroid(n,-1,n);
dead[n]=1;
cur={};
f.clear();
f[0]=0;
for (auto [i,we]:nei[n])
{
if (!dead[i])
{
h[i]=1;
dfs1(i,-1,we);
for (auto [j,w]:cur)
{
if (f.find(j)==f.end())
f[j]=w;
else
f[j]=min(f[j],w);
}
cur={};
}
}
for (auto [i,we]:nei[n])
{
if (!dead[i])
sol(i);
}
}
int best_path(int n, int K, int H[][2], int L[])
{
reqval=K;
for (int i=0;i<n-1;i++)
{
int u=H[i][0],v=H[i][1];
nei[u].push_back({v,L[i]});
nei[v].push_back({u,L[i]});
}
sol(0);
ans=(ans==1e9+10?-1:ans);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9676 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9672 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 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 |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9676 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9672 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 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 |
9564 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 |
2 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 |
9784 KB |
Output is correct |
26 |
Correct |
2 ms |
9564 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
3 ms |
9564 KB |
Output is correct |
29 |
Correct |
3 ms |
9564 KB |
Output is correct |
30 |
Correct |
4 ms |
9564 KB |
Output is correct |
31 |
Correct |
3 ms |
9564 KB |
Output is correct |
32 |
Correct |
3 ms |
9644 KB |
Output is correct |
33 |
Correct |
4 ms |
9560 KB |
Output is correct |
34 |
Correct |
2 ms |
9560 KB |
Output is correct |
35 |
Correct |
2 ms |
9564 KB |
Output is correct |
36 |
Correct |
3 ms |
9756 KB |
Output is correct |
37 |
Correct |
3 ms |
9784 KB |
Output is correct |
38 |
Correct |
3 ms |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9676 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9672 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 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 |
9564 KB |
Output is correct |
19 |
Correct |
129 ms |
14168 KB |
Output is correct |
20 |
Correct |
130 ms |
13904 KB |
Output is correct |
21 |
Correct |
137 ms |
14044 KB |
Output is correct |
22 |
Correct |
120 ms |
14292 KB |
Output is correct |
23 |
Correct |
61 ms |
13968 KB |
Output is correct |
24 |
Correct |
64 ms |
13904 KB |
Output is correct |
25 |
Correct |
91 ms |
17244 KB |
Output is correct |
26 |
Correct |
79 ms |
21464 KB |
Output is correct |
27 |
Correct |
121 ms |
20120 KB |
Output is correct |
28 |
Correct |
171 ms |
34152 KB |
Output is correct |
29 |
Correct |
166 ms |
33108 KB |
Output is correct |
30 |
Correct |
128 ms |
20076 KB |
Output is correct |
31 |
Correct |
120 ms |
20048 KB |
Output is correct |
32 |
Correct |
185 ms |
20096 KB |
Output is correct |
33 |
Correct |
165 ms |
18772 KB |
Output is correct |
34 |
Correct |
113 ms |
18772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9676 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9672 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 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 |
9564 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 |
2 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 |
9784 KB |
Output is correct |
26 |
Correct |
2 ms |
9564 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
3 ms |
9564 KB |
Output is correct |
29 |
Correct |
3 ms |
9564 KB |
Output is correct |
30 |
Correct |
4 ms |
9564 KB |
Output is correct |
31 |
Correct |
3 ms |
9564 KB |
Output is correct |
32 |
Correct |
3 ms |
9644 KB |
Output is correct |
33 |
Correct |
4 ms |
9560 KB |
Output is correct |
34 |
Correct |
2 ms |
9560 KB |
Output is correct |
35 |
Correct |
2 ms |
9564 KB |
Output is correct |
36 |
Correct |
3 ms |
9756 KB |
Output is correct |
37 |
Correct |
3 ms |
9784 KB |
Output is correct |
38 |
Correct |
3 ms |
9564 KB |
Output is correct |
39 |
Correct |
129 ms |
14168 KB |
Output is correct |
40 |
Correct |
130 ms |
13904 KB |
Output is correct |
41 |
Correct |
137 ms |
14044 KB |
Output is correct |
42 |
Correct |
120 ms |
14292 KB |
Output is correct |
43 |
Correct |
61 ms |
13968 KB |
Output is correct |
44 |
Correct |
64 ms |
13904 KB |
Output is correct |
45 |
Correct |
91 ms |
17244 KB |
Output is correct |
46 |
Correct |
79 ms |
21464 KB |
Output is correct |
47 |
Correct |
121 ms |
20120 KB |
Output is correct |
48 |
Correct |
171 ms |
34152 KB |
Output is correct |
49 |
Correct |
166 ms |
33108 KB |
Output is correct |
50 |
Correct |
128 ms |
20076 KB |
Output is correct |
51 |
Correct |
120 ms |
20048 KB |
Output is correct |
52 |
Correct |
185 ms |
20096 KB |
Output is correct |
53 |
Correct |
165 ms |
18772 KB |
Output is correct |
54 |
Correct |
113 ms |
18772 KB |
Output is correct |
55 |
Correct |
15 ms |
10268 KB |
Output is correct |
56 |
Correct |
11 ms |
10076 KB |
Output is correct |
57 |
Correct |
105 ms |
14604 KB |
Output is correct |
58 |
Correct |
32 ms |
13768 KB |
Output is correct |
59 |
Correct |
222 ms |
24684 KB |
Output is correct |
60 |
Correct |
617 ms |
42948 KB |
Output is correct |
61 |
Correct |
167 ms |
20060 KB |
Output is correct |
62 |
Correct |
186 ms |
20152 KB |
Output is correct |
63 |
Correct |
234 ms |
20088 KB |
Output is correct |
64 |
Correct |
611 ms |
24940 KB |
Output is correct |
65 |
Correct |
127 ms |
20100 KB |
Output is correct |
66 |
Correct |
422 ms |
30684 KB |
Output is correct |
67 |
Correct |
78 ms |
20256 KB |
Output is correct |
68 |
Correct |
297 ms |
24376 KB |
Output is correct |
69 |
Correct |
279 ms |
24260 KB |
Output is correct |
70 |
Correct |
259 ms |
24056 KB |
Output is correct |