# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
461182 |
2021-08-09T13:32:24 Z |
KazamaHoang |
Race (IOI11_race) |
C++14 |
|
638 ms |
34036 KB |
#include <bits/stdc++.h>
#define F first
#define S second
#define eb emplace_back
#define bit(x, i) (((x) >> (i)) & 1)
using namespace std;
using ll = long long;
const int inf = 1061109567;
const ll INF = 4557430888798830399;
const int MOD = 998244353;
int n, lenRace;
vector <pair <int, int>> adj[200005];
int subtree_size[200005];
int minDepth[1000005];
bool vis[200005];
int res = inf;
int dfs(int u, int par) {
int &ret = subtree_size[u];
ret = 1;
for (auto pii : adj[u]) {
int v = pii.F;
if (v != par && !vis[v])
ret += dfs(v, u);
}
return ret;
}
int find_centroid(int u, int par, int sz) {
for (auto pii : adj[u]) {
int v = pii.F;
if (v != par && !vis[v] && subtree_size[v] * 2 > sz)
return find_centroid(v, u, sz);
}
return u;
}
void calc(int u, int par, int sign, int depth, int len) {
if (len > lenRace)
return;
if (sign == 0)
res = min(res, depth + minDepth[lenRace-len]);
else if (sign == 1)
minDepth[len] = min(minDepth[len], depth);
else
minDepth[len] = inf;
for (auto pii : adj[u]) {
int v = pii.F;
int w = pii.S;
if (!vis[v] && v != par)
calc(v, u, sign, depth + 1, len + w);
}
}
void process(int u) {
int centroid = find_centroid(u, -1, dfs(u, -1));
vis[centroid] = true;
for (auto pii : adj[centroid]) {
int v = pii.F, w = pii.S;
if (vis[v])
continue;
calc(v, centroid, 0, 1, w);
calc(v, centroid, 1, 1, w);
}
for (auto pii : adj[centroid]) {
int v = pii.F;
int w = pii.S;
if (vis[v])
continue;
calc(v, centroid, -1, 1, w);
}
for (auto pii : adj[centroid]) {
int v = pii.F;
if (vis[v])
continue;
process(v);
}
}
int best_path(int N, int K, int h[][2], int L[]) {
n = N;
lenRace = K;
for (int i = 0; i < n - 1; ++ i) {
int u = h[i][0] + 1;
int v = h[i][1] + 1;
adj[u].eb(v, L[i]);
adj[v].eb(u, L[i]);
}
for (int i = 1; i <= 1e6; ++ i)
minDepth[i] = inf;
process(1);
if (res == inf)
res = -1;
return res;
}
//int32_t main(void) {
// cin.tie(0)->sync_with_stdio(0);
// if (fopen(".in", "r")) {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
// }
// cin >> n >> lenRace;
// for (int i = 1; i < n; ++ i) {
// int u, v, w; cin >> u >> v >> w;
// ++ u, ++ v;
// adj[u].eb(v, w);
// adj[v].eb(u, w);
// }
// for (int i = 1; i <= 1e6; ++ i)
// minDepth[i] = inf;
// process(1);
// if (res == inf)
// res = -1;
// cout << res;
// return 0;
//}
// Written by Kazama Hoang ^^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
6 ms |
8908 KB |
Output is correct |
3 |
Correct |
6 ms |
8840 KB |
Output is correct |
4 |
Correct |
5 ms |
8836 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
5 ms |
8840 KB |
Output is correct |
8 |
Correct |
5 ms |
8840 KB |
Output is correct |
9 |
Correct |
5 ms |
8908 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8844 KB |
Output is correct |
17 |
Correct |
5 ms |
8836 KB |
Output is correct |
18 |
Correct |
5 ms |
8908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
6 ms |
8908 KB |
Output is correct |
3 |
Correct |
6 ms |
8840 KB |
Output is correct |
4 |
Correct |
5 ms |
8836 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
5 ms |
8840 KB |
Output is correct |
8 |
Correct |
5 ms |
8840 KB |
Output is correct |
9 |
Correct |
5 ms |
8908 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8844 KB |
Output is correct |
17 |
Correct |
5 ms |
8836 KB |
Output is correct |
18 |
Correct |
5 ms |
8908 KB |
Output is correct |
19 |
Correct |
6 ms |
8908 KB |
Output is correct |
20 |
Correct |
5 ms |
8908 KB |
Output is correct |
21 |
Correct |
5 ms |
8908 KB |
Output is correct |
22 |
Correct |
6 ms |
8908 KB |
Output is correct |
23 |
Correct |
6 ms |
8908 KB |
Output is correct |
24 |
Correct |
6 ms |
8960 KB |
Output is correct |
25 |
Correct |
6 ms |
8908 KB |
Output is correct |
26 |
Correct |
7 ms |
8980 KB |
Output is correct |
27 |
Correct |
6 ms |
8908 KB |
Output is correct |
28 |
Correct |
6 ms |
8908 KB |
Output is correct |
29 |
Correct |
6 ms |
8908 KB |
Output is correct |
30 |
Correct |
7 ms |
8908 KB |
Output is correct |
31 |
Correct |
6 ms |
8908 KB |
Output is correct |
32 |
Correct |
6 ms |
8984 KB |
Output is correct |
33 |
Correct |
7 ms |
8908 KB |
Output is correct |
34 |
Correct |
7 ms |
8976 KB |
Output is correct |
35 |
Correct |
6 ms |
8908 KB |
Output is correct |
36 |
Correct |
6 ms |
8908 KB |
Output is correct |
37 |
Correct |
6 ms |
8972 KB |
Output is correct |
38 |
Correct |
6 ms |
8908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
6 ms |
8908 KB |
Output is correct |
3 |
Correct |
6 ms |
8840 KB |
Output is correct |
4 |
Correct |
5 ms |
8836 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
5 ms |
8840 KB |
Output is correct |
8 |
Correct |
5 ms |
8840 KB |
Output is correct |
9 |
Correct |
5 ms |
8908 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8844 KB |
Output is correct |
17 |
Correct |
5 ms |
8836 KB |
Output is correct |
18 |
Correct |
5 ms |
8908 KB |
Output is correct |
19 |
Correct |
147 ms |
15492 KB |
Output is correct |
20 |
Correct |
147 ms |
15572 KB |
Output is correct |
21 |
Correct |
195 ms |
15612 KB |
Output is correct |
22 |
Correct |
156 ms |
15752 KB |
Output is correct |
23 |
Correct |
92 ms |
15884 KB |
Output is correct |
24 |
Correct |
65 ms |
15760 KB |
Output is correct |
25 |
Correct |
147 ms |
18244 KB |
Output is correct |
26 |
Correct |
113 ms |
21340 KB |
Output is correct |
27 |
Correct |
188 ms |
22568 KB |
Output is correct |
28 |
Correct |
272 ms |
34036 KB |
Output is correct |
29 |
Correct |
302 ms |
33084 KB |
Output is correct |
30 |
Correct |
204 ms |
22680 KB |
Output is correct |
31 |
Correct |
215 ms |
22680 KB |
Output is correct |
32 |
Correct |
238 ms |
22784 KB |
Output is correct |
33 |
Correct |
235 ms |
21616 KB |
Output is correct |
34 |
Correct |
196 ms |
22384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
6 ms |
8908 KB |
Output is correct |
3 |
Correct |
6 ms |
8840 KB |
Output is correct |
4 |
Correct |
5 ms |
8836 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
5 ms |
8840 KB |
Output is correct |
8 |
Correct |
5 ms |
8840 KB |
Output is correct |
9 |
Correct |
5 ms |
8908 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8844 KB |
Output is correct |
17 |
Correct |
5 ms |
8836 KB |
Output is correct |
18 |
Correct |
5 ms |
8908 KB |
Output is correct |
19 |
Correct |
6 ms |
8908 KB |
Output is correct |
20 |
Correct |
5 ms |
8908 KB |
Output is correct |
21 |
Correct |
5 ms |
8908 KB |
Output is correct |
22 |
Correct |
6 ms |
8908 KB |
Output is correct |
23 |
Correct |
6 ms |
8908 KB |
Output is correct |
24 |
Correct |
6 ms |
8960 KB |
Output is correct |
25 |
Correct |
6 ms |
8908 KB |
Output is correct |
26 |
Correct |
7 ms |
8980 KB |
Output is correct |
27 |
Correct |
6 ms |
8908 KB |
Output is correct |
28 |
Correct |
6 ms |
8908 KB |
Output is correct |
29 |
Correct |
6 ms |
8908 KB |
Output is correct |
30 |
Correct |
7 ms |
8908 KB |
Output is correct |
31 |
Correct |
6 ms |
8908 KB |
Output is correct |
32 |
Correct |
6 ms |
8984 KB |
Output is correct |
33 |
Correct |
7 ms |
8908 KB |
Output is correct |
34 |
Correct |
7 ms |
8976 KB |
Output is correct |
35 |
Correct |
6 ms |
8908 KB |
Output is correct |
36 |
Correct |
6 ms |
8908 KB |
Output is correct |
37 |
Correct |
6 ms |
8972 KB |
Output is correct |
38 |
Correct |
6 ms |
8908 KB |
Output is correct |
39 |
Correct |
147 ms |
15492 KB |
Output is correct |
40 |
Correct |
147 ms |
15572 KB |
Output is correct |
41 |
Correct |
195 ms |
15612 KB |
Output is correct |
42 |
Correct |
156 ms |
15752 KB |
Output is correct |
43 |
Correct |
92 ms |
15884 KB |
Output is correct |
44 |
Correct |
65 ms |
15760 KB |
Output is correct |
45 |
Correct |
147 ms |
18244 KB |
Output is correct |
46 |
Correct |
113 ms |
21340 KB |
Output is correct |
47 |
Correct |
188 ms |
22568 KB |
Output is correct |
48 |
Correct |
272 ms |
34036 KB |
Output is correct |
49 |
Correct |
302 ms |
33084 KB |
Output is correct |
50 |
Correct |
204 ms |
22680 KB |
Output is correct |
51 |
Correct |
215 ms |
22680 KB |
Output is correct |
52 |
Correct |
238 ms |
22784 KB |
Output is correct |
53 |
Correct |
235 ms |
21616 KB |
Output is correct |
54 |
Correct |
196 ms |
22384 KB |
Output is correct |
55 |
Correct |
14 ms |
9548 KB |
Output is correct |
56 |
Correct |
16 ms |
9580 KB |
Output is correct |
57 |
Correct |
100 ms |
15852 KB |
Output is correct |
58 |
Correct |
41 ms |
15520 KB |
Output is correct |
59 |
Correct |
132 ms |
21296 KB |
Output is correct |
60 |
Correct |
537 ms |
33220 KB |
Output is correct |
61 |
Correct |
233 ms |
22980 KB |
Output is correct |
62 |
Correct |
225 ms |
22724 KB |
Output is correct |
63 |
Correct |
297 ms |
22720 KB |
Output is correct |
64 |
Correct |
638 ms |
23108 KB |
Output is correct |
65 |
Correct |
246 ms |
23496 KB |
Output is correct |
66 |
Correct |
440 ms |
31224 KB |
Output is correct |
67 |
Correct |
129 ms |
23348 KB |
Output is correct |
68 |
Correct |
311 ms |
23536 KB |
Output is correct |
69 |
Correct |
305 ms |
23712 KB |
Output is correct |
70 |
Correct |
293 ms |
22920 KB |
Output is correct |