#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> piipi;
typedef pair<pii, pii> piipii;
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define eb emplace_back
vector<pii> g[200005];
bool vis[200005];
int sub[200005];
int nn = 0;
int ans = 1e9;
void dfs1(int u, int p){
sub[u] = 1;
nn++;
for(int i=0;i<g[u].size();i++){
int v = g[u][i].fi;
if(v == p || vis[v]) continue;
dfs1(v, u);
sub[u] += sub[v];
}
}
int dfs2(int u, int p){
for(int i=0;i<g[u].size();i++){
int v = g[u][i].fi;
if(v == p || vis[v]) continue;
if(sub[v] > nn/2) return dfs2(v, u);
}
return u;
}
int best[1000005];
vector<pii> val;
void dfs3(int u, int p, int d, int c, int k){
if(c > k) return;
if(best[k-c] != -1) ans = min(ans, best[k-c] + d);
val.eb(mp(c, d));
for(int i=0;i<sz(g[u]);i++){
int v = g[u][i].fi, w = g[u][i].se;
if(v == p || vis[v]) continue;
dfs3(v, u, d+1, c+w, k);
}
}
void decompose(int u, int p, int k){
nn = 0;
dfs1(u, 0);
int centroid = dfs2(u, 0);
vis[centroid] = 1;
vector<int> dist;
best[0] = 0;
dist.eb(0);
for(int i=0;i<sz(g[centroid]);i++){
int v = g[centroid][i].fi, w = g[centroid][i].se;
if(v == p || vis[v]) continue;
val.clear();
dfs3(v, centroid, 1, w, k);
for(int j=0;j<sz(val);j++){
if(best[val[j].fi] == -1 || best[val[j].fi] > val[j].se) best[val[j].fi] = val[j].se;
dist.eb(val[j].fi);
}
}
for(int i=0;i<sz(dist);i++){
best[dist[i]] = -1;
}
for(int i=0;i<g[centroid].size();i++){
int v = g[centroid][i].fi;
if(v == p || vis[v]) continue;
decompose(v, centroid, k);
}
}
int best_path(int n, int k, int h[][2], int l[]){
for(int i=0;i<n-1;i++){
g[h[i][0]].eb(mp(h[i][1], l[i]));
g[h[i][1]].eb(mp(h[i][0], l[i]));
}
memset(best, -1, sizeof(best));
decompose(0, -1, k);
if(ans == 1e9) ans = -1;
return ans;
}
Compilation message
race.cpp: In function 'void dfs1(int, int)':
race.cpp:26:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[u].size();i++){
~^~~~~~~~~~~~
race.cpp: In function 'int dfs2(int, int)':
race.cpp:35:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[u].size();i++){
~^~~~~~~~~~~~
race.cpp: In function 'void decompose(int, int, int)':
race.cpp:79:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[centroid].size();i++){
~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8960 KB |
Output is correct |
2 |
Correct |
8 ms |
8960 KB |
Output is correct |
3 |
Correct |
7 ms |
8960 KB |
Output is correct |
4 |
Correct |
6 ms |
8960 KB |
Output is correct |
5 |
Correct |
6 ms |
8960 KB |
Output is correct |
6 |
Correct |
7 ms |
8960 KB |
Output is correct |
7 |
Correct |
7 ms |
8960 KB |
Output is correct |
8 |
Correct |
6 ms |
8960 KB |
Output is correct |
9 |
Correct |
8 ms |
8960 KB |
Output is correct |
10 |
Correct |
7 ms |
8960 KB |
Output is correct |
11 |
Correct |
6 ms |
8960 KB |
Output is correct |
12 |
Correct |
6 ms |
8960 KB |
Output is correct |
13 |
Correct |
9 ms |
8960 KB |
Output is correct |
14 |
Correct |
7 ms |
8960 KB |
Output is correct |
15 |
Correct |
6 ms |
8960 KB |
Output is correct |
16 |
Correct |
7 ms |
8960 KB |
Output is correct |
17 |
Correct |
6 ms |
9088 KB |
Output is correct |
18 |
Correct |
8 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8960 KB |
Output is correct |
2 |
Correct |
8 ms |
8960 KB |
Output is correct |
3 |
Correct |
7 ms |
8960 KB |
Output is correct |
4 |
Correct |
6 ms |
8960 KB |
Output is correct |
5 |
Correct |
6 ms |
8960 KB |
Output is correct |
6 |
Correct |
7 ms |
8960 KB |
Output is correct |
7 |
Correct |
7 ms |
8960 KB |
Output is correct |
8 |
Correct |
6 ms |
8960 KB |
Output is correct |
9 |
Correct |
8 ms |
8960 KB |
Output is correct |
10 |
Correct |
7 ms |
8960 KB |
Output is correct |
11 |
Correct |
6 ms |
8960 KB |
Output is correct |
12 |
Correct |
6 ms |
8960 KB |
Output is correct |
13 |
Correct |
9 ms |
8960 KB |
Output is correct |
14 |
Correct |
7 ms |
8960 KB |
Output is correct |
15 |
Correct |
6 ms |
8960 KB |
Output is correct |
16 |
Correct |
7 ms |
8960 KB |
Output is correct |
17 |
Correct |
6 ms |
9088 KB |
Output is correct |
18 |
Correct |
8 ms |
8960 KB |
Output is correct |
19 |
Correct |
7 ms |
8960 KB |
Output is correct |
20 |
Correct |
7 ms |
8960 KB |
Output is correct |
21 |
Correct |
7 ms |
8960 KB |
Output is correct |
22 |
Correct |
7 ms |
8960 KB |
Output is correct |
23 |
Correct |
7 ms |
8960 KB |
Output is correct |
24 |
Correct |
7 ms |
8960 KB |
Output is correct |
25 |
Correct |
7 ms |
8960 KB |
Output is correct |
26 |
Correct |
6 ms |
8960 KB |
Output is correct |
27 |
Correct |
7 ms |
8960 KB |
Output is correct |
28 |
Correct |
8 ms |
9088 KB |
Output is correct |
29 |
Correct |
8 ms |
9088 KB |
Output is correct |
30 |
Correct |
8 ms |
8960 KB |
Output is correct |
31 |
Correct |
8 ms |
8960 KB |
Output is correct |
32 |
Correct |
8 ms |
9088 KB |
Output is correct |
33 |
Correct |
10 ms |
9088 KB |
Output is correct |
34 |
Correct |
6 ms |
8960 KB |
Output is correct |
35 |
Correct |
6 ms |
8960 KB |
Output is correct |
36 |
Correct |
7 ms |
8960 KB |
Output is correct |
37 |
Correct |
7 ms |
8960 KB |
Output is correct |
38 |
Correct |
8 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8960 KB |
Output is correct |
2 |
Correct |
8 ms |
8960 KB |
Output is correct |
3 |
Correct |
7 ms |
8960 KB |
Output is correct |
4 |
Correct |
6 ms |
8960 KB |
Output is correct |
5 |
Correct |
6 ms |
8960 KB |
Output is correct |
6 |
Correct |
7 ms |
8960 KB |
Output is correct |
7 |
Correct |
7 ms |
8960 KB |
Output is correct |
8 |
Correct |
6 ms |
8960 KB |
Output is correct |
9 |
Correct |
8 ms |
8960 KB |
Output is correct |
10 |
Correct |
7 ms |
8960 KB |
Output is correct |
11 |
Correct |
6 ms |
8960 KB |
Output is correct |
12 |
Correct |
6 ms |
8960 KB |
Output is correct |
13 |
Correct |
9 ms |
8960 KB |
Output is correct |
14 |
Correct |
7 ms |
8960 KB |
Output is correct |
15 |
Correct |
6 ms |
8960 KB |
Output is correct |
16 |
Correct |
7 ms |
8960 KB |
Output is correct |
17 |
Correct |
6 ms |
9088 KB |
Output is correct |
18 |
Correct |
8 ms |
8960 KB |
Output is correct |
19 |
Correct |
173 ms |
15916 KB |
Output is correct |
20 |
Correct |
168 ms |
15812 KB |
Output is correct |
21 |
Correct |
184 ms |
16280 KB |
Output is correct |
22 |
Correct |
173 ms |
16752 KB |
Output is correct |
23 |
Correct |
111 ms |
16084 KB |
Output is correct |
24 |
Correct |
94 ms |
15864 KB |
Output is correct |
25 |
Correct |
164 ms |
18424 KB |
Output is correct |
26 |
Correct |
127 ms |
22564 KB |
Output is correct |
27 |
Correct |
261 ms |
22776 KB |
Output is correct |
28 |
Correct |
297 ms |
34088 KB |
Output is correct |
29 |
Correct |
360 ms |
33276 KB |
Output is correct |
30 |
Correct |
284 ms |
22776 KB |
Output is correct |
31 |
Correct |
229 ms |
22816 KB |
Output is correct |
32 |
Correct |
345 ms |
22904 KB |
Output is correct |
33 |
Correct |
351 ms |
21880 KB |
Output is correct |
34 |
Correct |
288 ms |
22524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8960 KB |
Output is correct |
2 |
Correct |
8 ms |
8960 KB |
Output is correct |
3 |
Correct |
7 ms |
8960 KB |
Output is correct |
4 |
Correct |
6 ms |
8960 KB |
Output is correct |
5 |
Correct |
6 ms |
8960 KB |
Output is correct |
6 |
Correct |
7 ms |
8960 KB |
Output is correct |
7 |
Correct |
7 ms |
8960 KB |
Output is correct |
8 |
Correct |
6 ms |
8960 KB |
Output is correct |
9 |
Correct |
8 ms |
8960 KB |
Output is correct |
10 |
Correct |
7 ms |
8960 KB |
Output is correct |
11 |
Correct |
6 ms |
8960 KB |
Output is correct |
12 |
Correct |
6 ms |
8960 KB |
Output is correct |
13 |
Correct |
9 ms |
8960 KB |
Output is correct |
14 |
Correct |
7 ms |
8960 KB |
Output is correct |
15 |
Correct |
6 ms |
8960 KB |
Output is correct |
16 |
Correct |
7 ms |
8960 KB |
Output is correct |
17 |
Correct |
6 ms |
9088 KB |
Output is correct |
18 |
Correct |
8 ms |
8960 KB |
Output is correct |
19 |
Correct |
7 ms |
8960 KB |
Output is correct |
20 |
Correct |
7 ms |
8960 KB |
Output is correct |
21 |
Correct |
7 ms |
8960 KB |
Output is correct |
22 |
Correct |
7 ms |
8960 KB |
Output is correct |
23 |
Correct |
7 ms |
8960 KB |
Output is correct |
24 |
Correct |
7 ms |
8960 KB |
Output is correct |
25 |
Correct |
7 ms |
8960 KB |
Output is correct |
26 |
Correct |
6 ms |
8960 KB |
Output is correct |
27 |
Correct |
7 ms |
8960 KB |
Output is correct |
28 |
Correct |
8 ms |
9088 KB |
Output is correct |
29 |
Correct |
8 ms |
9088 KB |
Output is correct |
30 |
Correct |
8 ms |
8960 KB |
Output is correct |
31 |
Correct |
8 ms |
8960 KB |
Output is correct |
32 |
Correct |
8 ms |
9088 KB |
Output is correct |
33 |
Correct |
10 ms |
9088 KB |
Output is correct |
34 |
Correct |
6 ms |
8960 KB |
Output is correct |
35 |
Correct |
6 ms |
8960 KB |
Output is correct |
36 |
Correct |
7 ms |
8960 KB |
Output is correct |
37 |
Correct |
7 ms |
8960 KB |
Output is correct |
38 |
Correct |
8 ms |
8960 KB |
Output is correct |
39 |
Correct |
173 ms |
15916 KB |
Output is correct |
40 |
Correct |
168 ms |
15812 KB |
Output is correct |
41 |
Correct |
184 ms |
16280 KB |
Output is correct |
42 |
Correct |
173 ms |
16752 KB |
Output is correct |
43 |
Correct |
111 ms |
16084 KB |
Output is correct |
44 |
Correct |
94 ms |
15864 KB |
Output is correct |
45 |
Correct |
164 ms |
18424 KB |
Output is correct |
46 |
Correct |
127 ms |
22564 KB |
Output is correct |
47 |
Correct |
261 ms |
22776 KB |
Output is correct |
48 |
Correct |
297 ms |
34088 KB |
Output is correct |
49 |
Correct |
360 ms |
33276 KB |
Output is correct |
50 |
Correct |
284 ms |
22776 KB |
Output is correct |
51 |
Correct |
229 ms |
22816 KB |
Output is correct |
52 |
Correct |
345 ms |
22904 KB |
Output is correct |
53 |
Correct |
351 ms |
21880 KB |
Output is correct |
54 |
Correct |
288 ms |
22524 KB |
Output is correct |
55 |
Correct |
17 ms |
9772 KB |
Output is correct |
56 |
Correct |
20 ms |
9728 KB |
Output is correct |
57 |
Correct |
122 ms |
16884 KB |
Output is correct |
58 |
Correct |
55 ms |
16620 KB |
Output is correct |
59 |
Correct |
152 ms |
22572 KB |
Output is correct |
60 |
Correct |
569 ms |
35940 KB |
Output is correct |
61 |
Correct |
266 ms |
23164 KB |
Output is correct |
62 |
Correct |
297 ms |
23932 KB |
Output is correct |
63 |
Correct |
377 ms |
23804 KB |
Output is correct |
64 |
Correct |
679 ms |
25740 KB |
Output is correct |
65 |
Correct |
352 ms |
23800 KB |
Output is correct |
66 |
Correct |
555 ms |
31480 KB |
Output is correct |
67 |
Correct |
176 ms |
25316 KB |
Output is correct |
68 |
Correct |
422 ms |
24768 KB |
Output is correct |
69 |
Correct |
380 ms |
24972 KB |
Output is correct |
70 |
Correct |
373 ms |
24048 KB |
Output is correct |