Submission #383355

# Submission time Handle Problem Language Result Execution time Memory
383355 2021-03-29T18:01:26 Z Aryan_Raina Race (IOI11_race) C++14
100 / 100
584 ms 38660 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define ld long double
#define ar array
 
const int INF = 1e9;
const int MOD = 1e9 + 7;

const int MXN = 2e5+9, MXK = 1e6+9;
vector<ar<int,2>> g[MXN], to_update;
vector<int> length(MXK, INF), indexes, sz(MXN);
vector<bool> dead(MXK, false);
int n, k, ans = INT_MAX;
int OneCentroid(int rt) {
	function<void(int,int)> get_sz = [&](int u, int pu) {
		sz[u] = 1;
		for (auto v : g[u]) if (v[1] != pu && !dead[v[1]]) {
			get_sz(v[1], u);
			sz[u] += sz[v[1]];
		}
	};
	get_sz(rt, rt);
	function<int(int,int)> cent = [&](int u, int pu) {
		for (auto v : g[u]) if (v[1] != pu && !dead[v[1]]) {
			if (sz[v[1]] > sz[rt]/2) return cent(v[1], u);
		}
		return u;
	};
	return cent(rt, rt);
}

void dfs(int u, int pu, int len, int dist) {
	if (k < dist || len > ans) return;
	if (k-dist >= 0) ans = min(ans, length[k-dist] + len);
	to_update.push_back({dist, len});
	for (auto v : g[u]) if (v[1] != pu && !dead[v[1]]) 
		dfs(v[1], u, len + 1, dist + v[0]);
}
 
void decompose(int start) {
	int cen = OneCentroid(start);
	length[0] = 0; indexes.push_back(0);
	// get length, distance to each node in subtree of centroid from centroid
	// ans = min(ans, length) if distance corresponding to it is k
	for (auto v : g[cen]) if (!dead[v[1]]) {
		dfs(v[1], cen, 1, v[0]);
		for (auto x : to_update) if (x[0] <= k) {
			if (length[x[0]] >= INF/2) indexes.push_back(x[0]);
			length[x[0]] = min(length[x[0]], x[1]);
		}
		to_update.clear();
	}
	for (int i : indexes) length[i] = INF;
	indexes.clear();

	dead[cen] = true;
	for (auto v : g[cen]) if (!dead[v[1]]) decompose(v[1]);
	dead[cen] = false;
}
 
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]].push_back({L[i], H[i][1]});
		g[H[i][1]].push_back({L[i], H[i][0]});
	}
  	decompose(0);
	return (ans < INF/2) ? ans : -1;
}
 
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 8 ms 9836 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 8 ms 9836 KB Output is correct
11 Correct 7 ms 9836 KB Output is correct
12 Correct 7 ms 9836 KB Output is correct
13 Correct 7 ms 9836 KB Output is correct
14 Correct 7 ms 9836 KB Output is correct
15 Correct 7 ms 9836 KB Output is correct
16 Correct 7 ms 9836 KB Output is correct
17 Correct 7 ms 9836 KB Output is correct
18 Correct 8 ms 9836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 8 ms 9836 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 8 ms 9836 KB Output is correct
11 Correct 7 ms 9836 KB Output is correct
12 Correct 7 ms 9836 KB Output is correct
13 Correct 7 ms 9836 KB Output is correct
14 Correct 7 ms 9836 KB Output is correct
15 Correct 7 ms 9836 KB Output is correct
16 Correct 7 ms 9836 KB Output is correct
17 Correct 7 ms 9836 KB Output is correct
18 Correct 8 ms 9836 KB Output is correct
19 Correct 7 ms 9836 KB Output is correct
20 Correct 7 ms 9836 KB Output is correct
21 Correct 9 ms 9964 KB Output is correct
22 Correct 8 ms 9836 KB Output is correct
23 Correct 8 ms 9836 KB Output is correct
24 Correct 10 ms 9836 KB Output is correct
25 Correct 8 ms 9964 KB Output is correct
26 Correct 8 ms 9836 KB Output is correct
27 Correct 7 ms 9836 KB Output is correct
28 Correct 8 ms 9964 KB Output is correct
29 Correct 8 ms 9964 KB Output is correct
30 Correct 8 ms 9980 KB Output is correct
31 Correct 8 ms 9964 KB Output is correct
32 Correct 8 ms 9964 KB Output is correct
33 Correct 8 ms 9964 KB Output is correct
34 Correct 8 ms 9964 KB Output is correct
35 Correct 9 ms 9964 KB Output is correct
36 Correct 8 ms 9964 KB Output is correct
37 Correct 8 ms 9964 KB Output is correct
38 Correct 8 ms 9964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 8 ms 9836 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 8 ms 9836 KB Output is correct
11 Correct 7 ms 9836 KB Output is correct
12 Correct 7 ms 9836 KB Output is correct
13 Correct 7 ms 9836 KB Output is correct
14 Correct 7 ms 9836 KB Output is correct
15 Correct 7 ms 9836 KB Output is correct
16 Correct 7 ms 9836 KB Output is correct
17 Correct 7 ms 9836 KB Output is correct
18 Correct 8 ms 9836 KB Output is correct
19 Correct 138 ms 14700 KB Output is correct
20 Correct 152 ms 14828 KB Output is correct
21 Correct 137 ms 14956 KB Output is correct
22 Correct 125 ms 14956 KB Output is correct
23 Correct 91 ms 14956 KB Output is correct
24 Correct 70 ms 14828 KB Output is correct
25 Correct 147 ms 19052 KB Output is correct
26 Correct 126 ms 24168 KB Output is correct
27 Correct 227 ms 19844 KB Output is correct
28 Correct 388 ms 37100 KB Output is correct
29 Correct 348 ms 35856 KB Output is correct
30 Correct 204 ms 19820 KB Output is correct
31 Correct 218 ms 19820 KB Output is correct
32 Correct 271 ms 19896 KB Output is correct
33 Correct 283 ms 18672 KB Output is correct
34 Correct 251 ms 18800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 8 ms 9836 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 8 ms 9836 KB Output is correct
11 Correct 7 ms 9836 KB Output is correct
12 Correct 7 ms 9836 KB Output is correct
13 Correct 7 ms 9836 KB Output is correct
14 Correct 7 ms 9836 KB Output is correct
15 Correct 7 ms 9836 KB Output is correct
16 Correct 7 ms 9836 KB Output is correct
17 Correct 7 ms 9836 KB Output is correct
18 Correct 8 ms 9836 KB Output is correct
19 Correct 7 ms 9836 KB Output is correct
20 Correct 7 ms 9836 KB Output is correct
21 Correct 9 ms 9964 KB Output is correct
22 Correct 8 ms 9836 KB Output is correct
23 Correct 8 ms 9836 KB Output is correct
24 Correct 10 ms 9836 KB Output is correct
25 Correct 8 ms 9964 KB Output is correct
26 Correct 8 ms 9836 KB Output is correct
27 Correct 7 ms 9836 KB Output is correct
28 Correct 8 ms 9964 KB Output is correct
29 Correct 8 ms 9964 KB Output is correct
30 Correct 8 ms 9980 KB Output is correct
31 Correct 8 ms 9964 KB Output is correct
32 Correct 8 ms 9964 KB Output is correct
33 Correct 8 ms 9964 KB Output is correct
34 Correct 8 ms 9964 KB Output is correct
35 Correct 9 ms 9964 KB Output is correct
36 Correct 8 ms 9964 KB Output is correct
37 Correct 8 ms 9964 KB Output is correct
38 Correct 8 ms 9964 KB Output is correct
39 Correct 138 ms 14700 KB Output is correct
40 Correct 152 ms 14828 KB Output is correct
41 Correct 137 ms 14956 KB Output is correct
42 Correct 125 ms 14956 KB Output is correct
43 Correct 91 ms 14956 KB Output is correct
44 Correct 70 ms 14828 KB Output is correct
45 Correct 147 ms 19052 KB Output is correct
46 Correct 126 ms 24168 KB Output is correct
47 Correct 227 ms 19844 KB Output is correct
48 Correct 388 ms 37100 KB Output is correct
49 Correct 348 ms 35856 KB Output is correct
50 Correct 204 ms 19820 KB Output is correct
51 Correct 218 ms 19820 KB Output is correct
52 Correct 271 ms 19896 KB Output is correct
53 Correct 283 ms 18672 KB Output is correct
54 Correct 251 ms 18800 KB Output is correct
55 Correct 16 ms 10348 KB Output is correct
56 Correct 16 ms 10476 KB Output is correct
57 Correct 105 ms 15720 KB Output is correct
58 Correct 57 ms 14944 KB Output is correct
59 Correct 129 ms 24676 KB Output is correct
60 Correct 491 ms 38660 KB Output is correct
61 Correct 227 ms 19780 KB Output is correct
62 Correct 234 ms 19820 KB Output is correct
63 Correct 309 ms 19804 KB Output is correct
64 Correct 584 ms 21212 KB Output is correct
65 Correct 299 ms 19692 KB Output is correct
66 Correct 509 ms 32880 KB Output is correct
67 Correct 138 ms 20060 KB Output is correct
68 Correct 283 ms 19948 KB Output is correct
69 Correct 256 ms 20076 KB Output is correct
70 Correct 251 ms 19692 KB Output is correct