#include "race.h"
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
ll n,k,has,b[202020],besar[202020],calon,me[1010101],i;
vector<ll> v[202020];
vector<ll> w[202020];
void dfs(ll aa,ll bb,ll cc)
{
ll ii,ma=0;
besar[aa]=1;
for(ii=0;ii<v[aa].size();ii++)
if((v[aa][ii]!=bb)&&(!b[v[aa][ii]]))
{
dfs(v[aa][ii],aa,cc);
besar[aa]+=besar[v[aa][ii]];
ma=max(ma,besar[v[aa][ii]]);
}
//cout<<aa<<" "<<ma<<" "<<(cc-besar[aa])<<"\n";
if((ma<=(cc/2))&&((cc-besar[aa])<=(cc/2)))calon=aa;
}
void dfs2(ll aa,ll bb)
{
ll ii,ma=0;
besar[aa]=1;
for(ii=0;ii<v[aa].size();ii++)
if((v[aa][ii]!=bb)&&(!b[v[aa][ii]]))
{
dfs2(v[aa][ii],aa);
besar[aa]+=besar[v[aa][ii]];
}
// cout<<aa<<" "<<ma<<" "<<(cc-besar[aa])<<"\n";
// if((ma<=(cc/2))&&((cc-besar[aa])<=(cc/2)))calon=aa;
}
vector<pair<ll,ll> > isi;
void cari(ll aa,ll bb,ll cc,ll dd)
{
if(dd>k)return ;
//cout<<aa<<" "<<bb<<" "<<cc<<" "<<dd<<" "<<me[k-dd]<<"\n";
if(me[k-dd]!=0)has=min(has,cc+me[k-dd]);
isi.pb(mp(cc,dd));
ll ii;
for(ii=0;ii<v[aa].size();ii++)
if((v[aa][ii]!=bb)&&(!b[v[aa][ii]]))
cari(v[aa][ii],aa,cc+1,dd+w[aa][ii]);
}
void cari(ll aa,ll bb)
{
// cout<<aa<<" "<<bb<<"\n";
calon=-1;
dfs(aa,aa,bb);
ll C0=calon;
// cout<<calon<<"\n";
ll ii,jj;
vector<ll> ini;
vector<ll> BS;
for(ii=0;ii<v[calon].size();ii++)
{
if(b[v[calon][ii]])continue;
//cout<<calon<<" "<<v[calon][ii]<<" x\n";
isi.clear();
cari(v[calon][ii],calon,1,w[calon][ii]);
for(jj=0;jj<isi.size();jj++)
{
//cout<<jj<<"->"<<k-isi[jj].se<<"\n";
if((me[isi[jj].se]==0)||(me[isi[jj].se]>isi[jj].fi))
me[isi[jj].se]=isi[jj].fi;
ini.pb(isi[jj].se);
}
if(me[k]!=0)has=min(has,me[k]);
}
for(ii=0;ii<ini.size();ii++)
me[ini[ii]]=0;
b[calon]=1;
dfs2(calon,calon);
ll te=0;
for(ii=0;ii<v[C0].size();ii++)
{
if(b[v[C0][ii]])continue;
//cout<<calon<<" "<<v[calon][ii]<<" z\n";
cari(v[C0][ii],besar[v[C0][ii]]);
//te++;
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N;
k=K;
has=1e18;
for(i=0;i<(n-1);i++)
{
v[H[i][0]].pb(H[i][1]);
w[H[i][0]].pb(L[i]);
v[H[i][1]].pb(H[i][0]);
w[H[i][1]].pb(L[i]);
}
cari(0,N);
if(has==1e18)has=-1;
return has;
}
Compilation message
race.cpp: In function 'void dfs(long long int, long long int, long long int)':
race.cpp:16:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ii=0;ii<v[aa].size();ii++)
~~^~~~~~~~~~~~~
race.cpp: In function 'void dfs2(long long int, long long int)':
race.cpp:30:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ii=0;ii<v[aa].size();ii++)
~~^~~~~~~~~~~~~
race.cpp:28:11: warning: unused variable 'ma' [-Wunused-variable]
ll ii,ma=0;
^~
race.cpp: In function 'void cari(long long int, long long int, long long int, long long int)':
race.cpp:47:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ii=0;ii<v[aa].size();ii++)
~~^~~~~~~~~~~~~
race.cpp: In function 'void cari(long long int, long long int)':
race.cpp:61:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ii=0;ii<v[calon].size();ii++)
~~^~~~~~~~~~~~~~~~
race.cpp:67:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(jj=0;jj<isi.size();jj++)
~~^~~~~~~~~~~
race.cpp:76:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ii=0;ii<ini.size();ii++)
~~^~~~~~~~~~~
race.cpp:81:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ii=0;ii<v[C0].size();ii++)
~~^~~~~~~~~~~~~
race.cpp:80:8: warning: unused variable 'te' [-Wunused-variable]
ll te=0;
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9976 KB |
Output is correct |
2 |
Correct |
11 ms |
10080 KB |
Output is correct |
3 |
Correct |
10 ms |
10152 KB |
Output is correct |
4 |
Correct |
10 ms |
10280 KB |
Output is correct |
5 |
Correct |
10 ms |
10280 KB |
Output is correct |
6 |
Correct |
10 ms |
10284 KB |
Output is correct |
7 |
Correct |
8 ms |
10288 KB |
Output is correct |
8 |
Correct |
8 ms |
10296 KB |
Output is correct |
9 |
Correct |
11 ms |
10364 KB |
Output is correct |
10 |
Correct |
10 ms |
10364 KB |
Output is correct |
11 |
Correct |
10 ms |
10364 KB |
Output is correct |
12 |
Correct |
10 ms |
10364 KB |
Output is correct |
13 |
Correct |
10 ms |
10372 KB |
Output is correct |
14 |
Correct |
10 ms |
10376 KB |
Output is correct |
15 |
Correct |
10 ms |
10380 KB |
Output is correct |
16 |
Correct |
10 ms |
10380 KB |
Output is correct |
17 |
Correct |
10 ms |
10388 KB |
Output is correct |
18 |
Correct |
11 ms |
10520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9976 KB |
Output is correct |
2 |
Correct |
11 ms |
10080 KB |
Output is correct |
3 |
Correct |
10 ms |
10152 KB |
Output is correct |
4 |
Correct |
10 ms |
10280 KB |
Output is correct |
5 |
Correct |
10 ms |
10280 KB |
Output is correct |
6 |
Correct |
10 ms |
10284 KB |
Output is correct |
7 |
Correct |
8 ms |
10288 KB |
Output is correct |
8 |
Correct |
8 ms |
10296 KB |
Output is correct |
9 |
Correct |
11 ms |
10364 KB |
Output is correct |
10 |
Correct |
10 ms |
10364 KB |
Output is correct |
11 |
Correct |
10 ms |
10364 KB |
Output is correct |
12 |
Correct |
10 ms |
10364 KB |
Output is correct |
13 |
Correct |
10 ms |
10372 KB |
Output is correct |
14 |
Correct |
10 ms |
10376 KB |
Output is correct |
15 |
Correct |
10 ms |
10380 KB |
Output is correct |
16 |
Correct |
10 ms |
10380 KB |
Output is correct |
17 |
Correct |
10 ms |
10388 KB |
Output is correct |
18 |
Correct |
11 ms |
10520 KB |
Output is correct |
19 |
Correct |
8 ms |
10520 KB |
Output is correct |
20 |
Correct |
9 ms |
10520 KB |
Output is correct |
21 |
Correct |
11 ms |
10536 KB |
Output is correct |
22 |
Correct |
16 ms |
13876 KB |
Output is correct |
23 |
Correct |
15 ms |
13876 KB |
Output is correct |
24 |
Correct |
16 ms |
14708 KB |
Output is correct |
25 |
Correct |
15 ms |
14708 KB |
Output is correct |
26 |
Correct |
14 ms |
14708 KB |
Output is correct |
27 |
Correct |
11 ms |
14708 KB |
Output is correct |
28 |
Correct |
13 ms |
14708 KB |
Output is correct |
29 |
Correct |
14 ms |
14708 KB |
Output is correct |
30 |
Correct |
14 ms |
14708 KB |
Output is correct |
31 |
Correct |
15 ms |
14708 KB |
Output is correct |
32 |
Correct |
15 ms |
14708 KB |
Output is correct |
33 |
Correct |
17 ms |
15524 KB |
Output is correct |
34 |
Correct |
16 ms |
15524 KB |
Output is correct |
35 |
Correct |
17 ms |
15812 KB |
Output is correct |
36 |
Correct |
18 ms |
16084 KB |
Output is correct |
37 |
Correct |
16 ms |
16084 KB |
Output is correct |
38 |
Correct |
14 ms |
16084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9976 KB |
Output is correct |
2 |
Correct |
11 ms |
10080 KB |
Output is correct |
3 |
Correct |
10 ms |
10152 KB |
Output is correct |
4 |
Correct |
10 ms |
10280 KB |
Output is correct |
5 |
Correct |
10 ms |
10280 KB |
Output is correct |
6 |
Correct |
10 ms |
10284 KB |
Output is correct |
7 |
Correct |
8 ms |
10288 KB |
Output is correct |
8 |
Correct |
8 ms |
10296 KB |
Output is correct |
9 |
Correct |
11 ms |
10364 KB |
Output is correct |
10 |
Correct |
10 ms |
10364 KB |
Output is correct |
11 |
Correct |
10 ms |
10364 KB |
Output is correct |
12 |
Correct |
10 ms |
10364 KB |
Output is correct |
13 |
Correct |
10 ms |
10372 KB |
Output is correct |
14 |
Correct |
10 ms |
10376 KB |
Output is correct |
15 |
Correct |
10 ms |
10380 KB |
Output is correct |
16 |
Correct |
10 ms |
10380 KB |
Output is correct |
17 |
Correct |
10 ms |
10388 KB |
Output is correct |
18 |
Correct |
11 ms |
10520 KB |
Output is correct |
19 |
Correct |
283 ms |
22644 KB |
Output is correct |
20 |
Correct |
215 ms |
23868 KB |
Output is correct |
21 |
Correct |
252 ms |
26080 KB |
Output is correct |
22 |
Correct |
271 ms |
28356 KB |
Output is correct |
23 |
Correct |
220 ms |
28356 KB |
Output is correct |
24 |
Correct |
149 ms |
29520 KB |
Output is correct |
25 |
Correct |
333 ms |
33772 KB |
Output is correct |
26 |
Correct |
164 ms |
41040 KB |
Output is correct |
27 |
Correct |
341 ms |
45440 KB |
Output is correct |
28 |
Correct |
798 ms |
61396 KB |
Output is correct |
29 |
Correct |
899 ms |
63488 KB |
Output is correct |
30 |
Correct |
377 ms |
63488 KB |
Output is correct |
31 |
Correct |
393 ms |
63488 KB |
Output is correct |
32 |
Correct |
505 ms |
63488 KB |
Output is correct |
33 |
Correct |
700 ms |
63488 KB |
Output is correct |
34 |
Correct |
616 ms |
64680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9976 KB |
Output is correct |
2 |
Correct |
11 ms |
10080 KB |
Output is correct |
3 |
Correct |
10 ms |
10152 KB |
Output is correct |
4 |
Correct |
10 ms |
10280 KB |
Output is correct |
5 |
Correct |
10 ms |
10280 KB |
Output is correct |
6 |
Correct |
10 ms |
10284 KB |
Output is correct |
7 |
Correct |
8 ms |
10288 KB |
Output is correct |
8 |
Correct |
8 ms |
10296 KB |
Output is correct |
9 |
Correct |
11 ms |
10364 KB |
Output is correct |
10 |
Correct |
10 ms |
10364 KB |
Output is correct |
11 |
Correct |
10 ms |
10364 KB |
Output is correct |
12 |
Correct |
10 ms |
10364 KB |
Output is correct |
13 |
Correct |
10 ms |
10372 KB |
Output is correct |
14 |
Correct |
10 ms |
10376 KB |
Output is correct |
15 |
Correct |
10 ms |
10380 KB |
Output is correct |
16 |
Correct |
10 ms |
10380 KB |
Output is correct |
17 |
Correct |
10 ms |
10388 KB |
Output is correct |
18 |
Correct |
11 ms |
10520 KB |
Output is correct |
19 |
Correct |
8 ms |
10520 KB |
Output is correct |
20 |
Correct |
9 ms |
10520 KB |
Output is correct |
21 |
Correct |
11 ms |
10536 KB |
Output is correct |
22 |
Correct |
16 ms |
13876 KB |
Output is correct |
23 |
Correct |
15 ms |
13876 KB |
Output is correct |
24 |
Correct |
16 ms |
14708 KB |
Output is correct |
25 |
Correct |
15 ms |
14708 KB |
Output is correct |
26 |
Correct |
14 ms |
14708 KB |
Output is correct |
27 |
Correct |
11 ms |
14708 KB |
Output is correct |
28 |
Correct |
13 ms |
14708 KB |
Output is correct |
29 |
Correct |
14 ms |
14708 KB |
Output is correct |
30 |
Correct |
14 ms |
14708 KB |
Output is correct |
31 |
Correct |
15 ms |
14708 KB |
Output is correct |
32 |
Correct |
15 ms |
14708 KB |
Output is correct |
33 |
Correct |
17 ms |
15524 KB |
Output is correct |
34 |
Correct |
16 ms |
15524 KB |
Output is correct |
35 |
Correct |
17 ms |
15812 KB |
Output is correct |
36 |
Correct |
18 ms |
16084 KB |
Output is correct |
37 |
Correct |
16 ms |
16084 KB |
Output is correct |
38 |
Correct |
14 ms |
16084 KB |
Output is correct |
39 |
Correct |
283 ms |
22644 KB |
Output is correct |
40 |
Correct |
215 ms |
23868 KB |
Output is correct |
41 |
Correct |
252 ms |
26080 KB |
Output is correct |
42 |
Correct |
271 ms |
28356 KB |
Output is correct |
43 |
Correct |
220 ms |
28356 KB |
Output is correct |
44 |
Correct |
149 ms |
29520 KB |
Output is correct |
45 |
Correct |
333 ms |
33772 KB |
Output is correct |
46 |
Correct |
164 ms |
41040 KB |
Output is correct |
47 |
Correct |
341 ms |
45440 KB |
Output is correct |
48 |
Correct |
798 ms |
61396 KB |
Output is correct |
49 |
Correct |
899 ms |
63488 KB |
Output is correct |
50 |
Correct |
377 ms |
63488 KB |
Output is correct |
51 |
Correct |
393 ms |
63488 KB |
Output is correct |
52 |
Correct |
505 ms |
63488 KB |
Output is correct |
53 |
Correct |
700 ms |
63488 KB |
Output is correct |
54 |
Correct |
616 ms |
64680 KB |
Output is correct |
55 |
Correct |
22 ms |
64680 KB |
Output is correct |
56 |
Correct |
24 ms |
64680 KB |
Output is correct |
57 |
Correct |
127 ms |
64680 KB |
Output is correct |
58 |
Correct |
52 ms |
64680 KB |
Output is correct |
59 |
Correct |
156 ms |
70592 KB |
Output is correct |
60 |
Correct |
1071 ms |
95964 KB |
Output is correct |
61 |
Correct |
437 ms |
95964 KB |
Output is correct |
62 |
Correct |
454 ms |
95964 KB |
Output is correct |
63 |
Correct |
426 ms |
95964 KB |
Output is correct |
64 |
Correct |
813 ms |
95964 KB |
Output is correct |
65 |
Correct |
465 ms |
95964 KB |
Output is correct |
66 |
Correct |
850 ms |
111208 KB |
Output is correct |
67 |
Correct |
297 ms |
111208 KB |
Output is correct |
68 |
Correct |
485 ms |
111208 KB |
Output is correct |
69 |
Correct |
573 ms |
112444 KB |
Output is correct |
70 |
Correct |
542 ms |
114648 KB |
Output is correct |