#include <bits/stdc++.h>
#include "race.h"
using namespace std;
long long n,k,dp[200500],siz[200500],dis[200500];
long long mn=1e9;
vector<pair<long long,long long> >gr[200500];
multiset<pair<long long,long long> >v[200500];
multiset<pair<long long,long long> >::iterator it;
void cal(int x,int p)
{
dp[x]=dp[p]+1;
siz[x]++;
for(int i=0; i<gr[x].size(); i++)
{
long long u=gr[x][i].first,w=gr[x][i].second;
if(u==p)continue;
dis[u]=dis[x]+w;
cal(u,x);
siz[x]+=siz[u];
}
}
void dfs(int x,int p)
{
long long big=0,mx=-1;
for(int i=0; i<gr[x].size(); i++)
{
long long u=gr[x][i].first;
if(u==p)continue;
if(mx<siz[u])
{
mx=siz[u];
big=u;
}
}
if(mx!=-1)dfs(big,x);
swap(v[big],v[x]);
v[x].insert({dis[x],dp[x]});
pair<long long,long long>zz=*v[x].lower_bound(make_pair(k+dis[x],0ll));
if(zz.first==k+dis[x]&&v[x].lower_bound(make_pair(k+dis[x],0ll))!=v[x].end())
{
long long add=zz.second-dp[x];
mn=min(mn,add);
}
for(int i=0; i<gr[x].size(); i++)
{
long long u=gr[x][i].first;
if(u==p||u==big)continue;
dfs(u,x);
}
for(int i=0; i<gr[x].size(); i++)
{
long long u=gr[x][i].first;
if(u==p||u==big)continue;
for(it=v[u].begin(); it!=v[u].end(); it++)
{
pair<long long,long long>z=*it;
long long dd=(k-z.first)+(2*dis[x]),add=z.second-dp[x];
pair<long long,long long>zz=*v[x].lower_bound(make_pair(dd,0ll));
if(zz.first+z.first==k+(2*dis[x])&&v[x].lower_bound(make_pair(dd,0ll))!=v[x].end())
{
long long add1=zz.second-dp[x];
mn=min(mn,add1+add);
}
}
for(it=v[u].begin(); it!=v[u].end(); it++)v[x].insert(*it);
}
}
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 x=H[i][0]+1,y=H[i][1]+1;
gr[x].push_back({y,L[i]});
gr[y].push_back({x,L[i]});
}
cal(1,0);
dfs(1,0);
if(mn!=1e9)return mn;
else return -1;
}
Compilation message
race.cpp: In function 'void cal(int, int)':
race.cpp:15:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<gr[x].size(); i++)
~^~~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int)':
race.cpp:27:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<gr[x].size(); i++)
~^~~~~~~~~~~~~
race.cpp:46:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<gr[x].size(); i++)
~^~~~~~~~~~~~~
race.cpp:52:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<gr[x].size(); i++)
~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
14 ms |
14456 KB |
Output is correct |
4 |
Correct |
14 ms |
14456 KB |
Output is correct |
5 |
Correct |
14 ms |
14456 KB |
Output is correct |
6 |
Correct |
14 ms |
14456 KB |
Output is correct |
7 |
Correct |
14 ms |
14584 KB |
Output is correct |
8 |
Correct |
14 ms |
14460 KB |
Output is correct |
9 |
Correct |
15 ms |
14456 KB |
Output is correct |
10 |
Correct |
13 ms |
14456 KB |
Output is correct |
11 |
Correct |
14 ms |
14456 KB |
Output is correct |
12 |
Correct |
14 ms |
14456 KB |
Output is correct |
13 |
Correct |
14 ms |
14456 KB |
Output is correct |
14 |
Correct |
16 ms |
14456 KB |
Output is correct |
15 |
Correct |
14 ms |
14456 KB |
Output is correct |
16 |
Correct |
14 ms |
14456 KB |
Output is correct |
17 |
Correct |
14 ms |
14456 KB |
Output is correct |
18 |
Correct |
14 ms |
14456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
14 ms |
14456 KB |
Output is correct |
4 |
Correct |
14 ms |
14456 KB |
Output is correct |
5 |
Correct |
14 ms |
14456 KB |
Output is correct |
6 |
Correct |
14 ms |
14456 KB |
Output is correct |
7 |
Correct |
14 ms |
14584 KB |
Output is correct |
8 |
Correct |
14 ms |
14460 KB |
Output is correct |
9 |
Correct |
15 ms |
14456 KB |
Output is correct |
10 |
Correct |
13 ms |
14456 KB |
Output is correct |
11 |
Correct |
14 ms |
14456 KB |
Output is correct |
12 |
Correct |
14 ms |
14456 KB |
Output is correct |
13 |
Correct |
14 ms |
14456 KB |
Output is correct |
14 |
Correct |
16 ms |
14456 KB |
Output is correct |
15 |
Correct |
14 ms |
14456 KB |
Output is correct |
16 |
Correct |
14 ms |
14456 KB |
Output is correct |
17 |
Correct |
14 ms |
14456 KB |
Output is correct |
18 |
Correct |
14 ms |
14456 KB |
Output is correct |
19 |
Correct |
14 ms |
14456 KB |
Output is correct |
20 |
Correct |
14 ms |
14456 KB |
Output is correct |
21 |
Correct |
15 ms |
14712 KB |
Output is correct |
22 |
Correct |
17 ms |
14712 KB |
Output is correct |
23 |
Correct |
15 ms |
14712 KB |
Output is correct |
24 |
Correct |
15 ms |
14712 KB |
Output is correct |
25 |
Correct |
17 ms |
14712 KB |
Output is correct |
26 |
Correct |
15 ms |
14716 KB |
Output is correct |
27 |
Correct |
15 ms |
14712 KB |
Output is correct |
28 |
Correct |
15 ms |
14712 KB |
Output is correct |
29 |
Correct |
15 ms |
14712 KB |
Output is correct |
30 |
Correct |
16 ms |
14716 KB |
Output is correct |
31 |
Correct |
15 ms |
14712 KB |
Output is correct |
32 |
Correct |
17 ms |
14716 KB |
Output is correct |
33 |
Correct |
16 ms |
14712 KB |
Output is correct |
34 |
Correct |
15 ms |
14712 KB |
Output is correct |
35 |
Correct |
15 ms |
14712 KB |
Output is correct |
36 |
Correct |
16 ms |
14712 KB |
Output is correct |
37 |
Correct |
14 ms |
14712 KB |
Output is correct |
38 |
Correct |
15 ms |
14712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
14 ms |
14456 KB |
Output is correct |
4 |
Correct |
14 ms |
14456 KB |
Output is correct |
5 |
Correct |
14 ms |
14456 KB |
Output is correct |
6 |
Correct |
14 ms |
14456 KB |
Output is correct |
7 |
Correct |
14 ms |
14584 KB |
Output is correct |
8 |
Correct |
14 ms |
14460 KB |
Output is correct |
9 |
Correct |
15 ms |
14456 KB |
Output is correct |
10 |
Correct |
13 ms |
14456 KB |
Output is correct |
11 |
Correct |
14 ms |
14456 KB |
Output is correct |
12 |
Correct |
14 ms |
14456 KB |
Output is correct |
13 |
Correct |
14 ms |
14456 KB |
Output is correct |
14 |
Correct |
16 ms |
14456 KB |
Output is correct |
15 |
Correct |
14 ms |
14456 KB |
Output is correct |
16 |
Correct |
14 ms |
14456 KB |
Output is correct |
17 |
Correct |
14 ms |
14456 KB |
Output is correct |
18 |
Correct |
14 ms |
14456 KB |
Output is correct |
19 |
Correct |
282 ms |
58376 KB |
Output is correct |
20 |
Correct |
265 ms |
57624 KB |
Output is correct |
21 |
Correct |
278 ms |
59000 KB |
Output is correct |
22 |
Correct |
260 ms |
54392 KB |
Output is correct |
23 |
Correct |
282 ms |
72312 KB |
Output is correct |
24 |
Correct |
230 ms |
58616 KB |
Output is correct |
25 |
Correct |
167 ms |
40184 KB |
Output is correct |
26 |
Correct |
106 ms |
44624 KB |
Output is correct |
27 |
Correct |
374 ms |
63480 KB |
Output is correct |
28 |
Correct |
333 ms |
74616 KB |
Output is correct |
29 |
Correct |
338 ms |
73336 KB |
Output is correct |
30 |
Correct |
357 ms |
63224 KB |
Output is correct |
31 |
Correct |
381 ms |
63240 KB |
Output is correct |
32 |
Correct |
429 ms |
63224 KB |
Output is correct |
33 |
Correct |
422 ms |
77688 KB |
Output is correct |
34 |
Correct |
406 ms |
75768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
14 ms |
14456 KB |
Output is correct |
4 |
Correct |
14 ms |
14456 KB |
Output is correct |
5 |
Correct |
14 ms |
14456 KB |
Output is correct |
6 |
Correct |
14 ms |
14456 KB |
Output is correct |
7 |
Correct |
14 ms |
14584 KB |
Output is correct |
8 |
Correct |
14 ms |
14460 KB |
Output is correct |
9 |
Correct |
15 ms |
14456 KB |
Output is correct |
10 |
Correct |
13 ms |
14456 KB |
Output is correct |
11 |
Correct |
14 ms |
14456 KB |
Output is correct |
12 |
Correct |
14 ms |
14456 KB |
Output is correct |
13 |
Correct |
14 ms |
14456 KB |
Output is correct |
14 |
Correct |
16 ms |
14456 KB |
Output is correct |
15 |
Correct |
14 ms |
14456 KB |
Output is correct |
16 |
Correct |
14 ms |
14456 KB |
Output is correct |
17 |
Correct |
14 ms |
14456 KB |
Output is correct |
18 |
Correct |
14 ms |
14456 KB |
Output is correct |
19 |
Correct |
14 ms |
14456 KB |
Output is correct |
20 |
Correct |
14 ms |
14456 KB |
Output is correct |
21 |
Correct |
15 ms |
14712 KB |
Output is correct |
22 |
Correct |
17 ms |
14712 KB |
Output is correct |
23 |
Correct |
15 ms |
14712 KB |
Output is correct |
24 |
Correct |
15 ms |
14712 KB |
Output is correct |
25 |
Correct |
17 ms |
14712 KB |
Output is correct |
26 |
Correct |
15 ms |
14716 KB |
Output is correct |
27 |
Correct |
15 ms |
14712 KB |
Output is correct |
28 |
Correct |
15 ms |
14712 KB |
Output is correct |
29 |
Correct |
15 ms |
14712 KB |
Output is correct |
30 |
Correct |
16 ms |
14716 KB |
Output is correct |
31 |
Correct |
15 ms |
14712 KB |
Output is correct |
32 |
Correct |
17 ms |
14716 KB |
Output is correct |
33 |
Correct |
16 ms |
14712 KB |
Output is correct |
34 |
Correct |
15 ms |
14712 KB |
Output is correct |
35 |
Correct |
15 ms |
14712 KB |
Output is correct |
36 |
Correct |
16 ms |
14712 KB |
Output is correct |
37 |
Correct |
14 ms |
14712 KB |
Output is correct |
38 |
Correct |
15 ms |
14712 KB |
Output is correct |
39 |
Correct |
282 ms |
58376 KB |
Output is correct |
40 |
Correct |
265 ms |
57624 KB |
Output is correct |
41 |
Correct |
278 ms |
59000 KB |
Output is correct |
42 |
Correct |
260 ms |
54392 KB |
Output is correct |
43 |
Correct |
282 ms |
72312 KB |
Output is correct |
44 |
Correct |
230 ms |
58616 KB |
Output is correct |
45 |
Correct |
167 ms |
40184 KB |
Output is correct |
46 |
Correct |
106 ms |
44624 KB |
Output is correct |
47 |
Correct |
374 ms |
63480 KB |
Output is correct |
48 |
Correct |
333 ms |
74616 KB |
Output is correct |
49 |
Correct |
338 ms |
73336 KB |
Output is correct |
50 |
Correct |
357 ms |
63224 KB |
Output is correct |
51 |
Correct |
381 ms |
63240 KB |
Output is correct |
52 |
Correct |
429 ms |
63224 KB |
Output is correct |
53 |
Correct |
422 ms |
77688 KB |
Output is correct |
54 |
Correct |
406 ms |
75768 KB |
Output is correct |
55 |
Correct |
35 ms |
17784 KB |
Output is correct |
56 |
Correct |
30 ms |
18168 KB |
Output is correct |
57 |
Correct |
252 ms |
74488 KB |
Output is correct |
58 |
Correct |
123 ms |
36296 KB |
Output is correct |
59 |
Correct |
102 ms |
46028 KB |
Output is correct |
60 |
Correct |
311 ms |
76792 KB |
Output is correct |
61 |
Correct |
377 ms |
66424 KB |
Output is correct |
62 |
Correct |
353 ms |
66296 KB |
Output is correct |
63 |
Correct |
458 ms |
66332 KB |
Output is correct |
64 |
Correct |
579 ms |
106324 KB |
Output is correct |
65 |
Correct |
546 ms |
99192 KB |
Output is correct |
66 |
Correct |
339 ms |
73464 KB |
Output is correct |
67 |
Correct |
350 ms |
59380 KB |
Output is correct |
68 |
Correct |
443 ms |
67832 KB |
Output is correct |
69 |
Correct |
445 ms |
68344 KB |
Output is correct |
70 |
Correct |
419 ms |
65660 KB |
Output is correct |