#include<iostream>
#include "race.h"
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
vector< pair<long long,long long> > v[200005];
long long numv[200005],nex,rs,k;
long long ans=10000000000000;
bool used[200005];
map<long long,long long> mp;
pair<long long,long long> nw[200005];
long long numvr(long long vr,long long par)
{
if(used[vr]) return numv[vr]=0;
long long b=0;
for(long long i=0;i<v[vr].size();i++)
{
if(v[vr][i].first==par) continue;
b+=numvr(v[vr][i].first,vr);
}
return numv[vr]=b+1;
}
long long findcen(long long vr,long long par,long long ncom)
{
if(used[vr]) return -1;
for(long long i=0;i<v[vr].size();i++)
{
if(v[vr][i].first==par) continue;
if(numv[v[vr][i].first]>(ncom/2)) return findcen(v[vr][i].first,vr,ncom);
}
return vr;
}
void finp(long long vr,long long par,long long d,long long br)
{
if(used[vr]) return;
nw[nex]={d,br};
nex++;
for(long long i=0;i<v[vr].size();i++)
{
if(v[vr][i].first!=par)
finp(v[vr][i].first,vr,v[vr][i].second+d,br+1);
}
}
void calc()
{
for(int i=0;i<nex;i++)
{
if(mp.count(k-nw[i].first))
ans=min(ans,(mp[k-nw[i].first]+nw[i].second));
}
for(int i=0;i<nex;i++)
{
if(mp.count(nw[i].first))
mp[nw[i].first]=min(mp[nw[i].first],nw[i].second);
else
mp[nw[i].first]=nw[i].second;
}
}
void solve(long long cen)
{
if(used[cen]) return;
mp.clear();
mp[0]=0;
nex=0;
used[cen]=1;
for(long long i=0;i<v[cen].size();i++)
{
if(used[v[cen][i].first]) continue;
nex=0;
finp(v[cen][i].first,cen,v[cen][i].second,1);
calc();
}
for(long long i=0;i<v[cen].size();i++)
{
if(used[v[cen][i].first]) continue;
long long tn=numvr(v[cen][i].first,cen);
solve(findcen(v[cen][i].first,cen,tn));
}
}
int best_path (int N, int K, int H[][2], int L[])
{
k=K;
for(long long i=0;i<N-1;i++)
{
v[H[i][0]].push_back({H[i][1],L[i]});
v[H[i][1]].push_back({H[i][0],L[i]});
}
long long tn=numvr(H[0][0],-1);
solve(findcen(H[0][0],-1,tn));
if(ans==10000000000000) return -1;
return ans;
}
/*
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
2
*/
Compilation message
race.cpp: In function 'long long int numvr(long long int, long long int)':
race.cpp:17:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(long long i=0;i<v[vr].size();i++)
| ~^~~~~~~~~~~~~
race.cpp: In function 'long long int findcen(long long int, long long int, long long int)':
race.cpp:27:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(long long i=0;i<v[vr].size();i++)
| ~^~~~~~~~~~~~~
race.cpp: In function 'void finp(long long int, long long int, long long int, long long int)':
race.cpp:40:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(long long i=0;i<v[vr].size();i++)
| ~^~~~~~~~~~~~~
race.cpp: In function 'void solve(long long int)':
race.cpp:71:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(long long i=0;i<v[cen].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp:80:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(long long i=0;i<v[cen].size();i++)
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5016 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5016 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4968 KB |
Output is correct |
21 |
Correct |
4 ms |
5024 KB |
Output is correct |
22 |
Correct |
5 ms |
5204 KB |
Output is correct |
23 |
Correct |
6 ms |
5172 KB |
Output is correct |
24 |
Correct |
4 ms |
5160 KB |
Output is correct |
25 |
Correct |
4 ms |
5152 KB |
Output is correct |
26 |
Correct |
5 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5156 KB |
Output is correct |
30 |
Correct |
5 ms |
5076 KB |
Output is correct |
31 |
Correct |
4 ms |
5156 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5152 KB |
Output is correct |
35 |
Correct |
4 ms |
5144 KB |
Output is correct |
36 |
Correct |
4 ms |
5156 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5016 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
229 ms |
13388 KB |
Output is correct |
20 |
Correct |
243 ms |
13484 KB |
Output is correct |
21 |
Correct |
212 ms |
13444 KB |
Output is correct |
22 |
Correct |
192 ms |
13292 KB |
Output is correct |
23 |
Correct |
270 ms |
13772 KB |
Output is correct |
24 |
Correct |
122 ms |
12608 KB |
Output is correct |
25 |
Correct |
184 ms |
19656 KB |
Output is correct |
26 |
Correct |
95 ms |
19308 KB |
Output is correct |
27 |
Correct |
290 ms |
20584 KB |
Output is correct |
28 |
Correct |
848 ms |
45496 KB |
Output is correct |
29 |
Correct |
822 ms |
45500 KB |
Output is correct |
30 |
Correct |
304 ms |
20600 KB |
Output is correct |
31 |
Correct |
271 ms |
20772 KB |
Output is correct |
32 |
Correct |
334 ms |
20428 KB |
Output is correct |
33 |
Correct |
440 ms |
20800 KB |
Output is correct |
34 |
Correct |
794 ms |
32844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5016 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4968 KB |
Output is correct |
21 |
Correct |
4 ms |
5024 KB |
Output is correct |
22 |
Correct |
5 ms |
5204 KB |
Output is correct |
23 |
Correct |
6 ms |
5172 KB |
Output is correct |
24 |
Correct |
4 ms |
5160 KB |
Output is correct |
25 |
Correct |
4 ms |
5152 KB |
Output is correct |
26 |
Correct |
5 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5156 KB |
Output is correct |
30 |
Correct |
5 ms |
5076 KB |
Output is correct |
31 |
Correct |
4 ms |
5156 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5152 KB |
Output is correct |
35 |
Correct |
4 ms |
5144 KB |
Output is correct |
36 |
Correct |
4 ms |
5156 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
229 ms |
13388 KB |
Output is correct |
40 |
Correct |
243 ms |
13484 KB |
Output is correct |
41 |
Correct |
212 ms |
13444 KB |
Output is correct |
42 |
Correct |
192 ms |
13292 KB |
Output is correct |
43 |
Correct |
270 ms |
13772 KB |
Output is correct |
44 |
Correct |
122 ms |
12608 KB |
Output is correct |
45 |
Correct |
184 ms |
19656 KB |
Output is correct |
46 |
Correct |
95 ms |
19308 KB |
Output is correct |
47 |
Correct |
290 ms |
20584 KB |
Output is correct |
48 |
Correct |
848 ms |
45496 KB |
Output is correct |
49 |
Correct |
822 ms |
45500 KB |
Output is correct |
50 |
Correct |
304 ms |
20600 KB |
Output is correct |
51 |
Correct |
271 ms |
20772 KB |
Output is correct |
52 |
Correct |
334 ms |
20428 KB |
Output is correct |
53 |
Correct |
440 ms |
20800 KB |
Output is correct |
54 |
Correct |
794 ms |
32844 KB |
Output is correct |
55 |
Correct |
22 ms |
6228 KB |
Output is correct |
56 |
Correct |
15 ms |
5844 KB |
Output is correct |
57 |
Correct |
126 ms |
14376 KB |
Output is correct |
58 |
Correct |
52 ms |
12732 KB |
Output is correct |
59 |
Correct |
297 ms |
24796 KB |
Output is correct |
60 |
Correct |
815 ms |
47008 KB |
Output is correct |
61 |
Correct |
355 ms |
24796 KB |
Output is correct |
62 |
Correct |
279 ms |
23048 KB |
Output is correct |
63 |
Correct |
354 ms |
23044 KB |
Output is correct |
64 |
Correct |
888 ms |
30444 KB |
Output is correct |
65 |
Correct |
767 ms |
37088 KB |
Output is correct |
66 |
Correct |
803 ms |
48344 KB |
Output is correct |
67 |
Correct |
178 ms |
22064 KB |
Output is correct |
68 |
Correct |
417 ms |
35060 KB |
Output is correct |
69 |
Correct |
446 ms |
35068 KB |
Output is correct |
70 |
Correct |
421 ms |
33624 KB |
Output is correct |