//Nguyen Huu Hoang Minh
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
#include "race.h"
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define reset(x) memset(x, 0,sizeof(x))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define MAXN 200005
#define remain(x) if (x > MOD) x -= MOD
#define ii pair<int, int>
#define iiii pair< ii , ii >
#define viiii vector< iiii >
#define vi vector<int>
#define vii vector< ii >
#define bit(x, i) (((x) >> (i)) & 1)
#define Task "test"
using namespace std;
typedef long double ld;
const int inf = 1e10;
const int minf = -1e10;
int n, ans, k;
vector<ii> g[MAXN];
vector<int> brute;
bool rm[MAXN];
int s[MAXN], a[1000005], sub[MAXN], d[MAXN], dep[MAXN];
int dfs(int u, int par=-1){
s[u] = 1;
for(auto i : g[u]){
int v = i.fi; if (v==par || rm[v]) continue;
s[u] += dfs(v,u);
}
return s[u];
}
int get_centroid(int u, int ms, int par=-1){
for(auto i : g[u]){
int v = i.fi; if (v==par || rm[v]) continue;
if (s[v]*2 > ms) return get_centroid(v,ms,u);
}
return u;
}
int DFS(int u, int par){
sub[u] = 1;
brute.pb(u);
for(auto i : g[u]){
int v = i.fi; int uv = i.se;
if (v==par || rm[v]) continue;
d[v] = d[u] + uv;
dep[v] = dep[u] + 1;
sub[u] += DFS(v,u);
}
return sub[u];
}
void centroid(int n = 0){
int C = get_centroid(n,dfs(n));
rm[C] = 1;
if (s[n]==1) return;
queue<int> q;
for(auto i : g[C]){
int v = i.fi; int uv = i.se; if (rm[v]) continue;
d[C] = 0; d[v] = d[C] + uv; dep[C] = 0; dep[v] = dep[C] + 1; brute.clear();
DFS(v,C);
for(auto x : brute){
if (d[x] > k) continue;
if (d[x]== k) ans = min(ans,dep[x]);
else if (a[k-d[x]]) ans = min(ans,dep[x] + a[k-d[x]]);
}
for(auto x : brute){
if (d[x] <= k && (a[d[x]]==0 || a[d[x]] > dep[x])) a[d[x]] = dep[x], q.push(d[x]);
}
}
while (q.size()){
a[q.front()] = 0;
q.pop();
}
for(auto i : g[C]){
int v = i.fi; if (!rm[v]) centroid(v);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
int i;
n = N, k = K;
for (i = 0; i < n - 1; i++){
g[H[i][0]].push_back(ii(H[i][1],L[i]));
g[H[i][1]].push_back(ii(H[i][0],L[i]));
}
ans = n + 1;
centroid();
return ans == n + 1 ? -1 : ans;
}
Compilation message
race.cpp:27:17: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+10' to '2147483647' [-Woverflow]
27 | const int inf = 1e10;
| ^~~~
race.cpp:28:18: warning: overflow in conversion from 'double' to 'int' changes value from '-1.0e+10' to '-2147483648' [-Woverflow]
28 | const int minf = -1e10;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4996 KB |
Output is correct |
2 |
Correct |
2 ms |
4996 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
2 ms |
4944 KB |
Output is correct |
5 |
Correct |
2 ms |
4944 KB |
Output is correct |
6 |
Correct |
2 ms |
4996 KB |
Output is correct |
7 |
Correct |
3 ms |
4944 KB |
Output is correct |
8 |
Correct |
3 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4996 KB |
Output is correct |
12 |
Correct |
3 ms |
4996 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
3 ms |
4944 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
4988 KB |
Output is correct |
17 |
Correct |
3 ms |
4944 KB |
Output is correct |
18 |
Correct |
3 ms |
4976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4996 KB |
Output is correct |
2 |
Correct |
2 ms |
4996 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
2 ms |
4944 KB |
Output is correct |
5 |
Correct |
2 ms |
4944 KB |
Output is correct |
6 |
Correct |
2 ms |
4996 KB |
Output is correct |
7 |
Correct |
3 ms |
4944 KB |
Output is correct |
8 |
Correct |
3 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4996 KB |
Output is correct |
12 |
Correct |
3 ms |
4996 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
3 ms |
4944 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
4988 KB |
Output is correct |
17 |
Correct |
3 ms |
4944 KB |
Output is correct |
18 |
Correct |
3 ms |
4976 KB |
Output is correct |
19 |
Correct |
3 ms |
4972 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5072 KB |
Output is correct |
22 |
Correct |
5 ms |
7504 KB |
Output is correct |
23 |
Correct |
5 ms |
7192 KB |
Output is correct |
24 |
Correct |
7 ms |
8060 KB |
Output is correct |
25 |
Correct |
4 ms |
6868 KB |
Output is correct |
26 |
Correct |
4 ms |
6396 KB |
Output is correct |
27 |
Correct |
3 ms |
5016 KB |
Output is correct |
28 |
Correct |
4 ms |
5840 KB |
Output is correct |
29 |
Correct |
4 ms |
6352 KB |
Output is correct |
30 |
Correct |
4 ms |
6404 KB |
Output is correct |
31 |
Correct |
4 ms |
6864 KB |
Output is correct |
32 |
Correct |
4 ms |
6864 KB |
Output is correct |
33 |
Correct |
5 ms |
7696 KB |
Output is correct |
34 |
Correct |
4 ms |
7320 KB |
Output is correct |
35 |
Correct |
5 ms |
7928 KB |
Output is correct |
36 |
Correct |
5 ms |
8328 KB |
Output is correct |
37 |
Correct |
4 ms |
6996 KB |
Output is correct |
38 |
Correct |
4 ms |
6228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4996 KB |
Output is correct |
2 |
Correct |
2 ms |
4996 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
2 ms |
4944 KB |
Output is correct |
5 |
Correct |
2 ms |
4944 KB |
Output is correct |
6 |
Correct |
2 ms |
4996 KB |
Output is correct |
7 |
Correct |
3 ms |
4944 KB |
Output is correct |
8 |
Correct |
3 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4996 KB |
Output is correct |
12 |
Correct |
3 ms |
4996 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
3 ms |
4944 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
4988 KB |
Output is correct |
17 |
Correct |
3 ms |
4944 KB |
Output is correct |
18 |
Correct |
3 ms |
4976 KB |
Output is correct |
19 |
Correct |
124 ms |
13060 KB |
Output is correct |
20 |
Correct |
125 ms |
13128 KB |
Output is correct |
21 |
Correct |
124 ms |
13196 KB |
Output is correct |
22 |
Correct |
119 ms |
13280 KB |
Output is correct |
23 |
Correct |
99 ms |
13444 KB |
Output is correct |
24 |
Correct |
58 ms |
13180 KB |
Output is correct |
25 |
Correct |
137 ms |
16452 KB |
Output is correct |
26 |
Correct |
84 ms |
19004 KB |
Output is correct |
27 |
Correct |
165 ms |
21052 KB |
Output is correct |
28 |
Correct |
374 ms |
32984 KB |
Output is correct |
29 |
Correct |
414 ms |
32140 KB |
Output is correct |
30 |
Correct |
166 ms |
21064 KB |
Output is correct |
31 |
Correct |
179 ms |
21132 KB |
Output is correct |
32 |
Correct |
222 ms |
21188 KB |
Output is correct |
33 |
Correct |
279 ms |
20392 KB |
Output is correct |
34 |
Correct |
266 ms |
21268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4996 KB |
Output is correct |
2 |
Correct |
2 ms |
4996 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
2 ms |
4944 KB |
Output is correct |
5 |
Correct |
2 ms |
4944 KB |
Output is correct |
6 |
Correct |
2 ms |
4996 KB |
Output is correct |
7 |
Correct |
3 ms |
4944 KB |
Output is correct |
8 |
Correct |
3 ms |
4944 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
3 ms |
4996 KB |
Output is correct |
12 |
Correct |
3 ms |
4996 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
3 ms |
4944 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
4988 KB |
Output is correct |
17 |
Correct |
3 ms |
4944 KB |
Output is correct |
18 |
Correct |
3 ms |
4976 KB |
Output is correct |
19 |
Correct |
3 ms |
4972 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5072 KB |
Output is correct |
22 |
Correct |
5 ms |
7504 KB |
Output is correct |
23 |
Correct |
5 ms |
7192 KB |
Output is correct |
24 |
Correct |
7 ms |
8060 KB |
Output is correct |
25 |
Correct |
4 ms |
6868 KB |
Output is correct |
26 |
Correct |
4 ms |
6396 KB |
Output is correct |
27 |
Correct |
3 ms |
5016 KB |
Output is correct |
28 |
Correct |
4 ms |
5840 KB |
Output is correct |
29 |
Correct |
4 ms |
6352 KB |
Output is correct |
30 |
Correct |
4 ms |
6404 KB |
Output is correct |
31 |
Correct |
4 ms |
6864 KB |
Output is correct |
32 |
Correct |
4 ms |
6864 KB |
Output is correct |
33 |
Correct |
5 ms |
7696 KB |
Output is correct |
34 |
Correct |
4 ms |
7320 KB |
Output is correct |
35 |
Correct |
5 ms |
7928 KB |
Output is correct |
36 |
Correct |
5 ms |
8328 KB |
Output is correct |
37 |
Correct |
4 ms |
6996 KB |
Output is correct |
38 |
Correct |
4 ms |
6228 KB |
Output is correct |
39 |
Correct |
124 ms |
13060 KB |
Output is correct |
40 |
Correct |
125 ms |
13128 KB |
Output is correct |
41 |
Correct |
124 ms |
13196 KB |
Output is correct |
42 |
Correct |
119 ms |
13280 KB |
Output is correct |
43 |
Correct |
99 ms |
13444 KB |
Output is correct |
44 |
Correct |
58 ms |
13180 KB |
Output is correct |
45 |
Correct |
137 ms |
16452 KB |
Output is correct |
46 |
Correct |
84 ms |
19004 KB |
Output is correct |
47 |
Correct |
165 ms |
21052 KB |
Output is correct |
48 |
Correct |
374 ms |
32984 KB |
Output is correct |
49 |
Correct |
414 ms |
32140 KB |
Output is correct |
50 |
Correct |
166 ms |
21064 KB |
Output is correct |
51 |
Correct |
179 ms |
21132 KB |
Output is correct |
52 |
Correct |
222 ms |
21188 KB |
Output is correct |
53 |
Correct |
279 ms |
20392 KB |
Output is correct |
54 |
Correct |
266 ms |
21268 KB |
Output is correct |
55 |
Correct |
10 ms |
5816 KB |
Output is correct |
56 |
Correct |
10 ms |
5776 KB |
Output is correct |
57 |
Correct |
81 ms |
13440 KB |
Output is correct |
58 |
Correct |
33 ms |
12784 KB |
Output is correct |
59 |
Correct |
96 ms |
19500 KB |
Output is correct |
60 |
Correct |
434 ms |
35200 KB |
Output is correct |
61 |
Correct |
176 ms |
21320 KB |
Output is correct |
62 |
Correct |
165 ms |
21332 KB |
Output is correct |
63 |
Correct |
232 ms |
21304 KB |
Output is correct |
64 |
Correct |
478 ms |
23448 KB |
Output is correct |
65 |
Correct |
292 ms |
22596 KB |
Output is correct |
66 |
Correct |
426 ms |
34048 KB |
Output is correct |
67 |
Correct |
105 ms |
21816 KB |
Output is correct |
68 |
Correct |
236 ms |
25276 KB |
Output is correct |
69 |
Correct |
245 ms |
25600 KB |
Output is correct |
70 |
Correct |
222 ms |
24476 KB |
Output is correct |