#include "race.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> v[200001],w[200001];
int ans=200000;
void add(int i,int j,int k)
{
v[i].push_back(j);
v[j].push_back(i);
w[i].push_back(k);
w[j].push_back(k);
}
int n,k;
int all;
int sz[200001];
int mark[200001];
void prec(int i,int p)
{
sz[i]=1;
for(int j=0;j<v[i].size();j++)
{
int nb=v[i][j];
if(nb==p||mark[nb])continue;
prec(nb,i);
sz[i]+=sz[nb];
}
}
int centroid(int i,int p)
{
for(int j=0;j<v[i].size();j++)
{
int nb=v[i][j];
if(nb==p||mark[nb])continue;
if(sz[nb]>all/2)return centroid(nb,i);
}
return i;
}
int mp[1000001];
vector<int> val;
vector<pair<int,int> > ext;
void dfs(int i,int p,int cnt,int wg)
{
if(wg>k)return;
if(wg==k)ans=min(ans,cnt);
else
{
int wg2=k-wg;
//cout<<wg2<<" + "<<mp[wg2]<<endl;
ans=min(ans,cnt+mp[wg2]);
}
//cout<<i<<" "<<wg<<endl;
ext.push_back({wg,cnt});
for(int j=0;j<v[i].size();j++)
{
int nb=v[i][j];
if(nb==p||mark[nb])continue;
dfs(nb,i,cnt+1,wg+w[i][j]);
}
}
void solve(int beg)
{
prec(beg,-1);
all=sz[beg];
//cout<<all<<endl;
int c=centroid(beg,-1);
//cout<<"! "<<c<<endl;
for(int i=0;i<val.size();i++)
mp[val[i]]=n;
val.clear();
mark[c]=1;
for(int i=0;i<v[c].size();i++)
{
int nb=v[c][i];
if(mark[nb])continue;
//cout<<c<<" "<<nb<<endl;
dfs(nb,-1,1,w[c][i]);
for(int j=0;j<ext.size();j++)
{
//cout<<"= "<<ext[j].first<<endl;
if(mp[ext[j].first]==n)val.push_back(ext[j].first);
mp[ext[j].first]=min(mp[ext[j].first],ext[j].second);
}
ext.clear();
}
for(int i=0;i<v[c].size();i++)
{
int nb=v[c][i];
if(!mark[nb])solve(nb);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N;
k=K;
for(int i=1;i<=k;i++)
mp[i]=n;
for(int i=0;i<n-1;i++)
add(H[i][0],H[i][1],L[i]);
solve(0);
if(ans>=n)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 'void prec(int, int)':
race.cpp:25:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int j=0;j<v[i].size();j++)
| ~^~~~~~~~~~~~
race.cpp: In function 'int centroid(int, int)':
race.cpp:37:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int j=0;j<v[i].size();j++)
| ~^~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int, int, int)':
race.cpp:68:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int j=0;j<v[i].size();j++)
| ~^~~~~~~~~~~~
race.cpp: In function 'void solve(int)':
race.cpp:85:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int i=0;i<val.size();i++)
| ~^~~~~~~~~~~
race.cpp:90:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for(int i=0;i<v[c].size();i++)
| ~^~~~~~~~~~~~
race.cpp:98:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for(int j=0;j<ext.size();j++)
| ~^~~~~~~~~~~
race.cpp:107:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int i=0;i<v[c].size();i++)
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
5 ms |
9756 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
6 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
5 ms |
9820 KB |
Output is correct |
8 |
Correct |
5 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
6 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
5 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
5 ms |
9844 KB |
Output is correct |
17 |
Correct |
5 ms |
9816 KB |
Output is correct |
18 |
Correct |
5 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
5 ms |
9756 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
6 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
5 ms |
9820 KB |
Output is correct |
8 |
Correct |
5 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
6 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
5 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
5 ms |
9844 KB |
Output is correct |
17 |
Correct |
5 ms |
9816 KB |
Output is correct |
18 |
Correct |
5 ms |
9820 KB |
Output is correct |
19 |
Correct |
6 ms |
9720 KB |
Output is correct |
20 |
Correct |
4 ms |
9820 KB |
Output is correct |
21 |
Correct |
5 ms |
9820 KB |
Output is correct |
22 |
Correct |
7 ms |
13440 KB |
Output is correct |
23 |
Correct |
6 ms |
12892 KB |
Output is correct |
24 |
Correct |
6 ms |
13148 KB |
Output is correct |
25 |
Correct |
7 ms |
13144 KB |
Output is correct |
26 |
Correct |
6 ms |
11100 KB |
Output is correct |
27 |
Correct |
7 ms |
12892 KB |
Output is correct |
28 |
Correct |
5 ms |
10736 KB |
Output is correct |
29 |
Correct |
5 ms |
11356 KB |
Output is correct |
30 |
Correct |
6 ms |
11356 KB |
Output is correct |
31 |
Correct |
6 ms |
12380 KB |
Output is correct |
32 |
Correct |
6 ms |
12636 KB |
Output is correct |
33 |
Correct |
7 ms |
12920 KB |
Output is correct |
34 |
Correct |
7 ms |
12124 KB |
Output is correct |
35 |
Correct |
6 ms |
13064 KB |
Output is correct |
36 |
Correct |
7 ms |
13404 KB |
Output is correct |
37 |
Correct |
7 ms |
12892 KB |
Output is correct |
38 |
Correct |
6 ms |
11868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
5 ms |
9756 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
6 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
5 ms |
9820 KB |
Output is correct |
8 |
Correct |
5 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
6 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
5 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
5 ms |
9844 KB |
Output is correct |
17 |
Correct |
5 ms |
9816 KB |
Output is correct |
18 |
Correct |
5 ms |
9820 KB |
Output is correct |
19 |
Correct |
93 ms |
19540 KB |
Output is correct |
20 |
Correct |
114 ms |
19424 KB |
Output is correct |
21 |
Correct |
96 ms |
19712 KB |
Output is correct |
22 |
Correct |
86 ms |
19920 KB |
Output is correct |
23 |
Correct |
59 ms |
19284 KB |
Output is correct |
24 |
Correct |
44 ms |
19792 KB |
Output is correct |
25 |
Correct |
92 ms |
23376 KB |
Output is correct |
26 |
Correct |
69 ms |
27592 KB |
Output is correct |
27 |
Correct |
149 ms |
30232 KB |
Output is correct |
28 |
Correct |
210 ms |
44884 KB |
Output is correct |
29 |
Correct |
200 ms |
43604 KB |
Output is correct |
30 |
Correct |
148 ms |
30616 KB |
Output is correct |
31 |
Correct |
140 ms |
30208 KB |
Output is correct |
32 |
Correct |
171 ms |
30288 KB |
Output is correct |
33 |
Correct |
178 ms |
29268 KB |
Output is correct |
34 |
Correct |
169 ms |
30032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
5 ms |
9756 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
6 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
5 ms |
9820 KB |
Output is correct |
8 |
Correct |
5 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
6 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
5 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
5 ms |
9844 KB |
Output is correct |
17 |
Correct |
5 ms |
9816 KB |
Output is correct |
18 |
Correct |
5 ms |
9820 KB |
Output is correct |
19 |
Correct |
6 ms |
9720 KB |
Output is correct |
20 |
Correct |
4 ms |
9820 KB |
Output is correct |
21 |
Correct |
5 ms |
9820 KB |
Output is correct |
22 |
Correct |
7 ms |
13440 KB |
Output is correct |
23 |
Correct |
6 ms |
12892 KB |
Output is correct |
24 |
Correct |
6 ms |
13148 KB |
Output is correct |
25 |
Correct |
7 ms |
13144 KB |
Output is correct |
26 |
Correct |
6 ms |
11100 KB |
Output is correct |
27 |
Correct |
7 ms |
12892 KB |
Output is correct |
28 |
Correct |
5 ms |
10736 KB |
Output is correct |
29 |
Correct |
5 ms |
11356 KB |
Output is correct |
30 |
Correct |
6 ms |
11356 KB |
Output is correct |
31 |
Correct |
6 ms |
12380 KB |
Output is correct |
32 |
Correct |
6 ms |
12636 KB |
Output is correct |
33 |
Correct |
7 ms |
12920 KB |
Output is correct |
34 |
Correct |
7 ms |
12124 KB |
Output is correct |
35 |
Correct |
6 ms |
13064 KB |
Output is correct |
36 |
Correct |
7 ms |
13404 KB |
Output is correct |
37 |
Correct |
7 ms |
12892 KB |
Output is correct |
38 |
Correct |
6 ms |
11868 KB |
Output is correct |
39 |
Correct |
93 ms |
19540 KB |
Output is correct |
40 |
Correct |
114 ms |
19424 KB |
Output is correct |
41 |
Correct |
96 ms |
19712 KB |
Output is correct |
42 |
Correct |
86 ms |
19920 KB |
Output is correct |
43 |
Correct |
59 ms |
19284 KB |
Output is correct |
44 |
Correct |
44 ms |
19792 KB |
Output is correct |
45 |
Correct |
92 ms |
23376 KB |
Output is correct |
46 |
Correct |
69 ms |
27592 KB |
Output is correct |
47 |
Correct |
149 ms |
30232 KB |
Output is correct |
48 |
Correct |
210 ms |
44884 KB |
Output is correct |
49 |
Correct |
200 ms |
43604 KB |
Output is correct |
50 |
Correct |
148 ms |
30616 KB |
Output is correct |
51 |
Correct |
140 ms |
30208 KB |
Output is correct |
52 |
Correct |
171 ms |
30288 KB |
Output is correct |
53 |
Correct |
178 ms |
29268 KB |
Output is correct |
54 |
Correct |
169 ms |
30032 KB |
Output is correct |
55 |
Correct |
11 ms |
10840 KB |
Output is correct |
56 |
Correct |
12 ms |
10844 KB |
Output is correct |
57 |
Correct |
69 ms |
19612 KB |
Output is correct |
58 |
Correct |
31 ms |
19788 KB |
Output is correct |
59 |
Correct |
80 ms |
28656 KB |
Output is correct |
60 |
Correct |
306 ms |
49480 KB |
Output is correct |
61 |
Correct |
151 ms |
30636 KB |
Output is correct |
62 |
Correct |
163 ms |
34248 KB |
Output is correct |
63 |
Correct |
195 ms |
34456 KB |
Output is correct |
64 |
Correct |
331 ms |
33360 KB |
Output is correct |
65 |
Correct |
202 ms |
30820 KB |
Output is correct |
66 |
Correct |
298 ms |
45140 KB |
Output is correct |
67 |
Correct |
89 ms |
35128 KB |
Output is correct |
68 |
Correct |
211 ms |
35556 KB |
Output is correct |
69 |
Correct |
203 ms |
35556 KB |
Output is correct |
70 |
Correct |
225 ms |
34580 KB |
Output is correct |