#include <bits/stdc++.h>
//#include "race.h"
using namespace std;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
struct cell
{
long long int a,b,c;
void read()
{
cin>>a>>b>>c;
}
};
long long int n,k,used[200005],dp[200005];
cell a[200005];
vector<cell>v[200005];
map<long long int,long long int>m,ne;
long long int otg;
void make_dp(long long int beg,int par)
{
long long int w;
dp[beg]=1;
for(long long int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(!used[w]&&w!=par)
{
make_dp(w,beg);
dp[beg]+=dp[w];
}
}
}
void cl(long long int beg,long long int par)
{
long long int w;
used[beg]=0;
for(long long int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(w!=par&&used[w]!=2)cl(w,beg);
}
}
void cdp(int beg,int par)
{
int w;
dp[beg]=0;
for(int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(!used[w]&&w!=par)cdp(w,beg);
}
}
void dfs(long long int beg,long long int st,long long int lead,long long int br)
{
if(st>k)return;
used[beg]=1;
cell w;
if(!ne[st])
{
ne[st]=br;
}
else ne[st]=min(ne[st],br);
for(long long int i=0;i<v[beg].size();i++)
{
w=v[beg][i];
if(!used[w.b])
{
dfs(w.b,st+w.c,lead,br+1);
}
if(beg==lead)
{
for(auto j=ne.begin();j!=ne.end();j++)
{
long long int h=j->first;
if(k==h)otg=min(otg,j->second);
if(m[k-h])
{
otg=min(otg,m[k-h]+j->second);
}
}
for(auto j=ne.begin();j!=ne.end();j++)
{
long long int h=j->first;
if(m[h])m[h]=min(m[h],ne[h]);
else m[h]=ne[h];
}
ne.clear();
}
}
}
void find_centroid(long long int beg,long long int par,long long int br)
{
long long int w,l=0;
used[beg]=1;
for(long long int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(!used[w]&&dp[w]>=(br+1)/2)
{
find_centroid(w,beg,br);
l=1;
}
}
if(!l)
{
cl(beg,beg);
dfs(beg,0,beg,0);
m.clear();
cl(beg,beg);
cdp(beg,beg);
used[beg]=2;
for(long long int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(!used[w])
{
make_dp(w,beg);
cl(w,w);
find_centroid(w,w,dp[w]);
}
}
}
}
int best_path(int N,int K,int H[][2],int L[])
//void read()
{
//cin>>n>>k;
n=N;
k=K;
for(long long int i=1;i<n;i++)
{
//cin>>a[i].a>>a[i].b;
a[i].a=H[i-1][0];
a[i].b=H[i-1][1];
}
for(long long int i=1;i<n;i++)
{
//cin>> a[i].c;
a[i].c=L[i-1];
v[a[i].a].push_back(a[i]);
swap(a[i].a,a[i].b);
v[a[i].a].push_back(a[i]);
}
//cout<<endl;
otg=1000000000;
make_dp(0,0);
find_centroid(0,0,dp[0]);
if(otg==1000000000)
{
//cout<<-1<<endl;
return -1;
}
return otg;
//cout<<otg<<endl;
}
/*int main()
{
speed();
read();
return 0;
}
*/
Compilation message
race.cpp: In function 'void make_dp(long long int, int)':
race.cpp:27:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void cl(long long int, long long int)':
race.cpp:41:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void cdp(int, int)':
race.cpp:51:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void dfs(long long int, long long int, long long int, long long int)':
race.cpp:67:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void find_centroid(long long int, long long int, long long int)':
race.cpp:99:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp:116:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5144 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
5028 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
4952 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5144 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
5028 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
4952 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4952 KB |
Output is correct |
21 |
Correct |
4 ms |
5164 KB |
Output is correct |
22 |
Correct |
3 ms |
5208 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
6748 KB |
Output is correct |
25 |
Correct |
5 ms |
5192 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
3 ms |
5212 KB |
Output is correct |
28 |
Correct |
4 ms |
5212 KB |
Output is correct |
29 |
Correct |
4 ms |
5212 KB |
Output is correct |
30 |
Correct |
6 ms |
5212 KB |
Output is correct |
31 |
Correct |
5 ms |
5212 KB |
Output is correct |
32 |
Correct |
5 ms |
5212 KB |
Output is correct |
33 |
Correct |
5 ms |
5212 KB |
Output is correct |
34 |
Correct |
4 ms |
5212 KB |
Output is correct |
35 |
Correct |
4 ms |
5160 KB |
Output is correct |
36 |
Correct |
3 ms |
5212 KB |
Output is correct |
37 |
Correct |
4 ms |
5212 KB |
Output is correct |
38 |
Correct |
4 ms |
5416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5144 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
5028 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
4952 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
291 ms |
17940 KB |
Output is correct |
20 |
Correct |
289 ms |
19724 KB |
Output is correct |
21 |
Correct |
295 ms |
19540 KB |
Output is correct |
22 |
Correct |
251 ms |
19540 KB |
Output is correct |
23 |
Correct |
151 ms |
20308 KB |
Output is correct |
24 |
Correct |
96 ms |
18772 KB |
Output is correct |
25 |
Correct |
209 ms |
23380 KB |
Output is correct |
26 |
Correct |
119 ms |
30288 KB |
Output is correct |
27 |
Correct |
251 ms |
33108 KB |
Output is correct |
28 |
Correct |
466 ms |
47952 KB |
Output is correct |
29 |
Correct |
455 ms |
46912 KB |
Output is correct |
30 |
Correct |
250 ms |
33104 KB |
Output is correct |
31 |
Correct |
260 ms |
33108 KB |
Output is correct |
32 |
Correct |
276 ms |
33104 KB |
Output is correct |
33 |
Correct |
507 ms |
32596 KB |
Output is correct |
34 |
Correct |
310 ms |
33260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5144 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
5028 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
4952 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4952 KB |
Output is correct |
21 |
Correct |
4 ms |
5164 KB |
Output is correct |
22 |
Correct |
3 ms |
5208 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
6748 KB |
Output is correct |
25 |
Correct |
5 ms |
5192 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
3 ms |
5212 KB |
Output is correct |
28 |
Correct |
4 ms |
5212 KB |
Output is correct |
29 |
Correct |
4 ms |
5212 KB |
Output is correct |
30 |
Correct |
6 ms |
5212 KB |
Output is correct |
31 |
Correct |
5 ms |
5212 KB |
Output is correct |
32 |
Correct |
5 ms |
5212 KB |
Output is correct |
33 |
Correct |
5 ms |
5212 KB |
Output is correct |
34 |
Correct |
4 ms |
5212 KB |
Output is correct |
35 |
Correct |
4 ms |
5160 KB |
Output is correct |
36 |
Correct |
3 ms |
5212 KB |
Output is correct |
37 |
Correct |
4 ms |
5212 KB |
Output is correct |
38 |
Correct |
4 ms |
5416 KB |
Output is correct |
39 |
Correct |
291 ms |
17940 KB |
Output is correct |
40 |
Correct |
289 ms |
19724 KB |
Output is correct |
41 |
Correct |
295 ms |
19540 KB |
Output is correct |
42 |
Correct |
251 ms |
19540 KB |
Output is correct |
43 |
Correct |
151 ms |
20308 KB |
Output is correct |
44 |
Correct |
96 ms |
18772 KB |
Output is correct |
45 |
Correct |
209 ms |
23380 KB |
Output is correct |
46 |
Correct |
119 ms |
30288 KB |
Output is correct |
47 |
Correct |
251 ms |
33108 KB |
Output is correct |
48 |
Correct |
466 ms |
47952 KB |
Output is correct |
49 |
Correct |
455 ms |
46912 KB |
Output is correct |
50 |
Correct |
250 ms |
33104 KB |
Output is correct |
51 |
Correct |
260 ms |
33108 KB |
Output is correct |
52 |
Correct |
276 ms |
33104 KB |
Output is correct |
53 |
Correct |
507 ms |
32596 KB |
Output is correct |
54 |
Correct |
310 ms |
33260 KB |
Output is correct |
55 |
Correct |
31 ms |
8284 KB |
Output is correct |
56 |
Correct |
20 ms |
8024 KB |
Output is correct |
57 |
Correct |
155 ms |
18524 KB |
Output is correct |
58 |
Correct |
44 ms |
16644 KB |
Output is correct |
59 |
Correct |
597 ms |
40676 KB |
Output is correct |
60 |
Correct |
1690 ms |
81488 KB |
Output is correct |
61 |
Correct |
384 ms |
33624 KB |
Output is correct |
62 |
Correct |
443 ms |
33760 KB |
Output is correct |
63 |
Correct |
477 ms |
33612 KB |
Output is correct |
64 |
Correct |
1436 ms |
48464 KB |
Output is correct |
65 |
Correct |
295 ms |
33648 KB |
Output is correct |
66 |
Correct |
1085 ms |
45064 KB |
Output is correct |
67 |
Correct |
112 ms |
31468 KB |
Output is correct |
68 |
Correct |
564 ms |
42324 KB |
Output is correct |
69 |
Correct |
562 ms |
42068 KB |
Output is correct |
70 |
Correct |
561 ms |
40752 KB |
Output is correct |