#include<bits/stdc++.h>
using namespace std;
int n,k;
vector<int>to [200005];
vector<int>len[200005];
long long deep[200005];
int Dep[200005];
int size[200005];
int son [200005];
int nid [200005];
int id [200005];
int cnt;
map< long long,int >dep;
int ans = 1e9;
void dfs1(int now,int last)
{
size[now] = 1;
id [now] = ++ cnt;
nid [cnt] = now;
int mx = 0;
for(int i = 0;i < to[now].size();i ++)
{
int next = to[now][i];
if( next == last )continue;
deep[next] = deep[now] + len[now][i];
Dep[next] = Dep[now] + 1;
dfs1( next,now );
size[now] += size[next];
if( size[next] > mx )
{
mx = size[next];
son[now] = next;
}
}
}
void updata(int x,int k)
{
int dx = deep[x];
if( k == -1 )
{
dep[ dx ] = 0;
}
else
{
int tmp = dep[dx];
if( tmp == 0 )tmp = 1e9;
dep[dx] = min( tmp,Dep[x] );
}
}
void dfs2(int now,int last,bool keep)
{
for(auto next : to[now])
{
if( next == son[now] or next == last )continue;
dfs2( next,now,0 );
}
if( son[now] )dfs2( son[now],now,1 );
for(auto next : to[now])
{
if( next == son[now] or next == last )continue;
for( int i = 0;i < size[next];i ++ )
{
int nxt = nid[ id[next] + i ];
int req = k + deep[now] - deep[nxt] + deep[now];
if( dep[ req ] )
{
ans = min( ans,dep[ req ] + Dep[nxt] - 2 * Dep[now] );
}
}
for( int i = 0;i < size[next];i ++)
{
updata( nid[ id[next] + i ],1 );
}
}
updata( now,1 );
if( dep[ deep[now] + k ] ) ans = min( ans,dep[ deep[now] + k ] - Dep[now] );
if( keep == 0 )
{
for( int i = 0;i < size[now];i ++ ){
updata( nid[ id[now] + i ],-1 );
}
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N, k = K;
for(int i = 0;i < n-1;i ++)
{
to[H[i][0]].push_back( H[i][1] );
to[H[i][1]].push_back( H[i][0] );
len[H[i][0]].push_back(L[i]);
len[H[i][1]].push_back(L[i]);
}
dfs1(0,0);dfs2(0,0,0);
if( ans == 1e9 )ans = -1;
return ans;
}
Compilation message
race.cpp: In function 'void dfs1(int, int)':
race.cpp:24:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i < to[now].size();i ++)
~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9848 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
10 ms |
9720 KB |
Output is correct |
4 |
Correct |
10 ms |
9720 KB |
Output is correct |
5 |
Correct |
11 ms |
9852 KB |
Output is correct |
6 |
Correct |
10 ms |
9720 KB |
Output is correct |
7 |
Correct |
10 ms |
9724 KB |
Output is correct |
8 |
Correct |
10 ms |
9720 KB |
Output is correct |
9 |
Correct |
11 ms |
9720 KB |
Output is correct |
10 |
Correct |
10 ms |
9848 KB |
Output is correct |
11 |
Correct |
10 ms |
9720 KB |
Output is correct |
12 |
Correct |
11 ms |
9720 KB |
Output is correct |
13 |
Correct |
10 ms |
9848 KB |
Output is correct |
14 |
Correct |
11 ms |
9720 KB |
Output is correct |
15 |
Correct |
11 ms |
9720 KB |
Output is correct |
16 |
Correct |
11 ms |
9720 KB |
Output is correct |
17 |
Correct |
10 ms |
9720 KB |
Output is correct |
18 |
Correct |
10 ms |
9720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9848 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
10 ms |
9720 KB |
Output is correct |
4 |
Correct |
10 ms |
9720 KB |
Output is correct |
5 |
Correct |
11 ms |
9852 KB |
Output is correct |
6 |
Correct |
10 ms |
9720 KB |
Output is correct |
7 |
Correct |
10 ms |
9724 KB |
Output is correct |
8 |
Correct |
10 ms |
9720 KB |
Output is correct |
9 |
Correct |
11 ms |
9720 KB |
Output is correct |
10 |
Correct |
10 ms |
9848 KB |
Output is correct |
11 |
Correct |
10 ms |
9720 KB |
Output is correct |
12 |
Correct |
11 ms |
9720 KB |
Output is correct |
13 |
Correct |
10 ms |
9848 KB |
Output is correct |
14 |
Correct |
11 ms |
9720 KB |
Output is correct |
15 |
Correct |
11 ms |
9720 KB |
Output is correct |
16 |
Correct |
11 ms |
9720 KB |
Output is correct |
17 |
Correct |
10 ms |
9720 KB |
Output is correct |
18 |
Correct |
10 ms |
9720 KB |
Output is correct |
19 |
Correct |
10 ms |
9720 KB |
Output is correct |
20 |
Correct |
10 ms |
9720 KB |
Output is correct |
21 |
Correct |
13 ms |
9976 KB |
Output is correct |
22 |
Correct |
14 ms |
10104 KB |
Output is correct |
23 |
Correct |
13 ms |
10104 KB |
Output is correct |
24 |
Correct |
13 ms |
10232 KB |
Output is correct |
25 |
Correct |
13 ms |
10136 KB |
Output is correct |
26 |
Correct |
13 ms |
10104 KB |
Output is correct |
27 |
Correct |
11 ms |
9720 KB |
Output is correct |
28 |
Correct |
13 ms |
10104 KB |
Output is correct |
29 |
Correct |
13 ms |
10104 KB |
Output is correct |
30 |
Correct |
13 ms |
10104 KB |
Output is correct |
31 |
Correct |
13 ms |
10104 KB |
Output is correct |
32 |
Correct |
13 ms |
10104 KB |
Output is correct |
33 |
Correct |
14 ms |
10104 KB |
Output is correct |
34 |
Correct |
12 ms |
9976 KB |
Output is correct |
35 |
Correct |
12 ms |
9976 KB |
Output is correct |
36 |
Correct |
12 ms |
9976 KB |
Output is correct |
37 |
Correct |
11 ms |
9976 KB |
Output is correct |
38 |
Correct |
12 ms |
10104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9848 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
10 ms |
9720 KB |
Output is correct |
4 |
Correct |
10 ms |
9720 KB |
Output is correct |
5 |
Correct |
11 ms |
9852 KB |
Output is correct |
6 |
Correct |
10 ms |
9720 KB |
Output is correct |
7 |
Correct |
10 ms |
9724 KB |
Output is correct |
8 |
Correct |
10 ms |
9720 KB |
Output is correct |
9 |
Correct |
11 ms |
9720 KB |
Output is correct |
10 |
Correct |
10 ms |
9848 KB |
Output is correct |
11 |
Correct |
10 ms |
9720 KB |
Output is correct |
12 |
Correct |
11 ms |
9720 KB |
Output is correct |
13 |
Correct |
10 ms |
9848 KB |
Output is correct |
14 |
Correct |
11 ms |
9720 KB |
Output is correct |
15 |
Correct |
11 ms |
9720 KB |
Output is correct |
16 |
Correct |
11 ms |
9720 KB |
Output is correct |
17 |
Correct |
10 ms |
9720 KB |
Output is correct |
18 |
Correct |
10 ms |
9720 KB |
Output is correct |
19 |
Correct |
220 ms |
20220 KB |
Output is correct |
20 |
Correct |
216 ms |
20344 KB |
Output is correct |
21 |
Correct |
215 ms |
20344 KB |
Output is correct |
22 |
Correct |
236 ms |
20472 KB |
Output is correct |
23 |
Correct |
330 ms |
20344 KB |
Output is correct |
24 |
Correct |
241 ms |
20728 KB |
Output is correct |
25 |
Correct |
141 ms |
27388 KB |
Output is correct |
26 |
Correct |
84 ms |
32760 KB |
Output is correct |
27 |
Correct |
378 ms |
31992 KB |
Output is correct |
28 |
Correct |
372 ms |
80376 KB |
Output is correct |
29 |
Correct |
389 ms |
79352 KB |
Output is correct |
30 |
Correct |
380 ms |
31992 KB |
Output is correct |
31 |
Correct |
381 ms |
31992 KB |
Output is correct |
32 |
Correct |
470 ms |
32376 KB |
Output is correct |
33 |
Correct |
410 ms |
30840 KB |
Output is correct |
34 |
Correct |
1417 ms |
87416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9848 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
10 ms |
9720 KB |
Output is correct |
4 |
Correct |
10 ms |
9720 KB |
Output is correct |
5 |
Correct |
11 ms |
9852 KB |
Output is correct |
6 |
Correct |
10 ms |
9720 KB |
Output is correct |
7 |
Correct |
10 ms |
9724 KB |
Output is correct |
8 |
Correct |
10 ms |
9720 KB |
Output is correct |
9 |
Correct |
11 ms |
9720 KB |
Output is correct |
10 |
Correct |
10 ms |
9848 KB |
Output is correct |
11 |
Correct |
10 ms |
9720 KB |
Output is correct |
12 |
Correct |
11 ms |
9720 KB |
Output is correct |
13 |
Correct |
10 ms |
9848 KB |
Output is correct |
14 |
Correct |
11 ms |
9720 KB |
Output is correct |
15 |
Correct |
11 ms |
9720 KB |
Output is correct |
16 |
Correct |
11 ms |
9720 KB |
Output is correct |
17 |
Correct |
10 ms |
9720 KB |
Output is correct |
18 |
Correct |
10 ms |
9720 KB |
Output is correct |
19 |
Correct |
10 ms |
9720 KB |
Output is correct |
20 |
Correct |
10 ms |
9720 KB |
Output is correct |
21 |
Correct |
13 ms |
9976 KB |
Output is correct |
22 |
Correct |
14 ms |
10104 KB |
Output is correct |
23 |
Correct |
13 ms |
10104 KB |
Output is correct |
24 |
Correct |
13 ms |
10232 KB |
Output is correct |
25 |
Correct |
13 ms |
10136 KB |
Output is correct |
26 |
Correct |
13 ms |
10104 KB |
Output is correct |
27 |
Correct |
11 ms |
9720 KB |
Output is correct |
28 |
Correct |
13 ms |
10104 KB |
Output is correct |
29 |
Correct |
13 ms |
10104 KB |
Output is correct |
30 |
Correct |
13 ms |
10104 KB |
Output is correct |
31 |
Correct |
13 ms |
10104 KB |
Output is correct |
32 |
Correct |
13 ms |
10104 KB |
Output is correct |
33 |
Correct |
14 ms |
10104 KB |
Output is correct |
34 |
Correct |
12 ms |
9976 KB |
Output is correct |
35 |
Correct |
12 ms |
9976 KB |
Output is correct |
36 |
Correct |
12 ms |
9976 KB |
Output is correct |
37 |
Correct |
11 ms |
9976 KB |
Output is correct |
38 |
Correct |
12 ms |
10104 KB |
Output is correct |
39 |
Correct |
220 ms |
20220 KB |
Output is correct |
40 |
Correct |
216 ms |
20344 KB |
Output is correct |
41 |
Correct |
215 ms |
20344 KB |
Output is correct |
42 |
Correct |
236 ms |
20472 KB |
Output is correct |
43 |
Correct |
330 ms |
20344 KB |
Output is correct |
44 |
Correct |
241 ms |
20728 KB |
Output is correct |
45 |
Correct |
141 ms |
27388 KB |
Output is correct |
46 |
Correct |
84 ms |
32760 KB |
Output is correct |
47 |
Correct |
378 ms |
31992 KB |
Output is correct |
48 |
Correct |
372 ms |
80376 KB |
Output is correct |
49 |
Correct |
389 ms |
79352 KB |
Output is correct |
50 |
Correct |
380 ms |
31992 KB |
Output is correct |
51 |
Correct |
381 ms |
31992 KB |
Output is correct |
52 |
Correct |
470 ms |
32376 KB |
Output is correct |
53 |
Correct |
410 ms |
30840 KB |
Output is correct |
54 |
Correct |
1417 ms |
87416 KB |
Output is correct |
55 |
Correct |
37 ms |
11768 KB |
Output is correct |
56 |
Correct |
23 ms |
10872 KB |
Output is correct |
57 |
Correct |
150 ms |
20048 KB |
Output is correct |
58 |
Correct |
84 ms |
20580 KB |
Output is correct |
59 |
Correct |
162 ms |
45048 KB |
Output is correct |
60 |
Correct |
483 ms |
79224 KB |
Output is correct |
61 |
Correct |
499 ms |
35448 KB |
Output is correct |
62 |
Correct |
388 ms |
32120 KB |
Output is correct |
63 |
Correct |
454 ms |
32248 KB |
Output is correct |
64 |
Correct |
1990 ms |
58600 KB |
Output is correct |
65 |
Correct |
2632 ms |
103672 KB |
Output is correct |
66 |
Correct |
467 ms |
77176 KB |
Output is correct |
67 |
Correct |
326 ms |
32096 KB |
Output is correct |
68 |
Correct |
1250 ms |
67960 KB |
Output is correct |
69 |
Correct |
1265 ms |
68600 KB |
Output is correct |
70 |
Correct |
1194 ms |
65528 KB |
Output is correct |