Submission #58781

# Submission time Handle Problem Language Result Execution time Memory
58781 2018-07-19T10:40:10 Z Sherazin Race (IOI11_race) C++14
100 / 100
1212 ms 62144 KB
//#include "race.h"
#include <bits/stdc++.h>

#define pii pair<int, int>
#define x first
#define y second

using namespace std;

const int N = 2e5 + 5;
const int M = 1e6 + 5;

int n, k;
int ans = -1, centroid, mxnode, cnt = 0;
int dep[N], path[M], dp[M];
vector<vector<pii> > g(N);
bitset<N> chk;

int subtree(int u, int p) {
	dep[u] = 1;
	for(pii v : g[u]) if(!chk[v.x] && v.x != p) {
		dep[u] += subtree(v.x, u);
	}
	return dep[u];
}

void centdecomp(int u, int p, int all) {
	int ret = all - dep[u];
	for(pii v : g[u]) if(!chk[v.x] && v.x != p) {
		centdecomp(v.x, u, all);
		ret = max(ret, dep[v.x]);
	}
	if(ret < mxnode) centroid = u, mxnode = ret;
}

void dfs(int u, int p, int cost, int edge, bool fill) {
	if(cost > k) return;
	if(!fill) {
		if(dp[k - cost] == cnt && (edge + path[k - cost] < ans || ans == -1)) ans = edge + path[k - cost];
		if(cost == k && (edge < ans || ans == -1)) ans = edge;
	} else if(dp[cost] < cnt || edge < path[cost]) dp[cost] = cnt, path[cost] = edge;

	for(pii v : g[u]) if(!chk[v.x] && v.x != p) {
		dfs(v.x, u, cost + v.y, edge + 1, fill);
	}
}

void proc(int u) {
	cnt++;
	if(subtree(u, -1) <= 1) return;
	mxnode = dep[u] + 1;
	centdecomp(u, -1, dep[u]);
	for(pii v : g[centroid]) if(!chk[v.x]) {
		dfs(v.x, centroid, v.y, 1, false);
		dfs(v.x, centroid, v.y, 1, true);
	}
	chk[centroid] = 1;
	for(pii v : g[centroid]) if(!chk[v.x]) {
		proc(v.x);
	}
}

int best_path(int N, int K, int H[][2], int L[])
{
	n = N, k = K;
	for(int i = 0; i < N - 1; i++) {
		g[H[i][0]].emplace_back(H[i][1], L[i]);
		g[H[i][1]].emplace_back(H[i][0], L[i]);
	}

	proc(0);

	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 7 ms 5224 KB Output is correct
3 Correct 8 ms 5224 KB Output is correct
4 Correct 8 ms 5236 KB Output is correct
5 Correct 7 ms 5280 KB Output is correct
6 Correct 8 ms 5284 KB Output is correct
7 Correct 8 ms 5284 KB Output is correct
8 Correct 7 ms 5284 KB Output is correct
9 Correct 7 ms 5284 KB Output is correct
10 Correct 8 ms 5284 KB Output is correct
11 Correct 8 ms 5284 KB Output is correct
12 Correct 8 ms 5284 KB Output is correct
13 Correct 7 ms 5284 KB Output is correct
14 Correct 7 ms 5340 KB Output is correct
15 Correct 6 ms 5340 KB Output is correct
16 Correct 8 ms 5340 KB Output is correct
17 Correct 7 ms 5340 KB Output is correct
18 Correct 8 ms 5444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 7 ms 5224 KB Output is correct
3 Correct 8 ms 5224 KB Output is correct
4 Correct 8 ms 5236 KB Output is correct
5 Correct 7 ms 5280 KB Output is correct
6 Correct 8 ms 5284 KB Output is correct
7 Correct 8 ms 5284 KB Output is correct
8 Correct 7 ms 5284 KB Output is correct
9 Correct 7 ms 5284 KB Output is correct
10 Correct 8 ms 5284 KB Output is correct
11 Correct 8 ms 5284 KB Output is correct
12 Correct 8 ms 5284 KB Output is correct
13 Correct 7 ms 5284 KB Output is correct
14 Correct 7 ms 5340 KB Output is correct
15 Correct 6 ms 5340 KB Output is correct
16 Correct 8 ms 5340 KB Output is correct
17 Correct 7 ms 5340 KB Output is correct
18 Correct 8 ms 5444 KB Output is correct
19 Correct 7 ms 5444 KB Output is correct
20 Correct 7 ms 5444 KB Output is correct
21 Correct 9 ms 5444 KB Output is correct
22 Correct 14 ms 10432 KB Output is correct
23 Correct 12 ms 10432 KB Output is correct
24 Correct 16 ms 11180 KB Output is correct
25 Correct 13 ms 11180 KB Output is correct
26 Correct 12 ms 11180 KB Output is correct
27 Correct 10 ms 11180 KB Output is correct
28 Correct 11 ms 11180 KB Output is correct
29 Correct 12 ms 11180 KB Output is correct
30 Correct 14 ms 11180 KB Output is correct
31 Correct 13 ms 11180 KB Output is correct
32 Correct 14 ms 11180 KB Output is correct
33 Correct 13 ms 11180 KB Output is correct
34 Correct 12 ms 11180 KB Output is correct
35 Correct 16 ms 11580 KB Output is correct
36 Correct 14 ms 12184 KB Output is correct
37 Correct 14 ms 12184 KB Output is correct
38 Correct 10 ms 12184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 7 ms 5224 KB Output is correct
3 Correct 8 ms 5224 KB Output is correct
4 Correct 8 ms 5236 KB Output is correct
5 Correct 7 ms 5280 KB Output is correct
6 Correct 8 ms 5284 KB Output is correct
7 Correct 8 ms 5284 KB Output is correct
8 Correct 7 ms 5284 KB Output is correct
9 Correct 7 ms 5284 KB Output is correct
10 Correct 8 ms 5284 KB Output is correct
11 Correct 8 ms 5284 KB Output is correct
12 Correct 8 ms 5284 KB Output is correct
13 Correct 7 ms 5284 KB Output is correct
14 Correct 7 ms 5340 KB Output is correct
15 Correct 6 ms 5340 KB Output is correct
16 Correct 8 ms 5340 KB Output is correct
17 Correct 7 ms 5340 KB Output is correct
18 Correct 8 ms 5444 KB Output is correct
19 Correct 265 ms 12184 KB Output is correct
20 Correct 387 ms 12184 KB Output is correct
21 Correct 257 ms 12184 KB Output is correct
22 Correct 237 ms 12184 KB Output is correct
23 Correct 151 ms 12184 KB Output is correct
24 Correct 165 ms 12184 KB Output is correct
25 Correct 367 ms 14568 KB Output is correct
26 Correct 204 ms 18328 KB Output is correct
27 Correct 381 ms 18328 KB Output is correct
28 Correct 745 ms 30880 KB Output is correct
29 Correct 821 ms 30880 KB Output is correct
30 Correct 374 ms 30880 KB Output is correct
31 Correct 358 ms 30880 KB Output is correct
32 Correct 377 ms 30880 KB Output is correct
33 Correct 760 ms 30880 KB Output is correct
34 Correct 474 ms 30880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 7 ms 5224 KB Output is correct
3 Correct 8 ms 5224 KB Output is correct
4 Correct 8 ms 5236 KB Output is correct
5 Correct 7 ms 5280 KB Output is correct
6 Correct 8 ms 5284 KB Output is correct
7 Correct 8 ms 5284 KB Output is correct
8 Correct 7 ms 5284 KB Output is correct
9 Correct 7 ms 5284 KB Output is correct
10 Correct 8 ms 5284 KB Output is correct
11 Correct 8 ms 5284 KB Output is correct
12 Correct 8 ms 5284 KB Output is correct
13 Correct 7 ms 5284 KB Output is correct
14 Correct 7 ms 5340 KB Output is correct
15 Correct 6 ms 5340 KB Output is correct
16 Correct 8 ms 5340 KB Output is correct
17 Correct 7 ms 5340 KB Output is correct
18 Correct 8 ms 5444 KB Output is correct
19 Correct 7 ms 5444 KB Output is correct
20 Correct 7 ms 5444 KB Output is correct
21 Correct 9 ms 5444 KB Output is correct
22 Correct 14 ms 10432 KB Output is correct
23 Correct 12 ms 10432 KB Output is correct
24 Correct 16 ms 11180 KB Output is correct
25 Correct 13 ms 11180 KB Output is correct
26 Correct 12 ms 11180 KB Output is correct
27 Correct 10 ms 11180 KB Output is correct
28 Correct 11 ms 11180 KB Output is correct
29 Correct 12 ms 11180 KB Output is correct
30 Correct 14 ms 11180 KB Output is correct
31 Correct 13 ms 11180 KB Output is correct
32 Correct 14 ms 11180 KB Output is correct
33 Correct 13 ms 11180 KB Output is correct
34 Correct 12 ms 11180 KB Output is correct
35 Correct 16 ms 11580 KB Output is correct
36 Correct 14 ms 12184 KB Output is correct
37 Correct 14 ms 12184 KB Output is correct
38 Correct 10 ms 12184 KB Output is correct
39 Correct 265 ms 12184 KB Output is correct
40 Correct 387 ms 12184 KB Output is correct
41 Correct 257 ms 12184 KB Output is correct
42 Correct 237 ms 12184 KB Output is correct
43 Correct 151 ms 12184 KB Output is correct
44 Correct 165 ms 12184 KB Output is correct
45 Correct 367 ms 14568 KB Output is correct
46 Correct 204 ms 18328 KB Output is correct
47 Correct 381 ms 18328 KB Output is correct
48 Correct 745 ms 30880 KB Output is correct
49 Correct 821 ms 30880 KB Output is correct
50 Correct 374 ms 30880 KB Output is correct
51 Correct 358 ms 30880 KB Output is correct
52 Correct 377 ms 30880 KB Output is correct
53 Correct 760 ms 30880 KB Output is correct
54 Correct 474 ms 30880 KB Output is correct
55 Correct 22 ms 30880 KB Output is correct
56 Correct 24 ms 30880 KB Output is correct
57 Correct 149 ms 30880 KB Output is correct
58 Correct 56 ms 30880 KB Output is correct
59 Correct 154 ms 30880 KB Output is correct
60 Correct 914 ms 41768 KB Output is correct
61 Correct 397 ms 41768 KB Output is correct
62 Correct 385 ms 41768 KB Output is correct
63 Correct 501 ms 41768 KB Output is correct
64 Correct 1117 ms 41768 KB Output is correct
65 Correct 494 ms 41768 KB Output is correct
66 Correct 1212 ms 61656 KB Output is correct
67 Correct 282 ms 61656 KB Output is correct
68 Correct 482 ms 61656 KB Output is correct
69 Correct 499 ms 61656 KB Output is correct
70 Correct 441 ms 62144 KB Output is correct