Submission #643513

# Submission time Handle Problem Language Result Execution time Memory
643513 2022-09-22T08:50:33 Z brobat Race (IOI11_race) C++17
100 / 100
715 ms 40148 KB
#include <bits/stdc++.h>
using namespace std;

int n, k;
vector <vector<pair<int, int>>> adj;
vector <bool> vis;
vector <int> sz;
int ans = 1E9;

void find_size(int node, int prev) {
	sz[node] = 1;
	for(auto [next, wt] : adj[node]) {
		if(next != prev && !vis[next]) {
			find_size(next, node);
			sz[node] += sz[next];
		}
	}
}

int centroid(int node, int prev, int n) {
	for(auto [next, wt] : adj[node]) {
		if(next != prev && !vis[next] && sz[next] > n/2) {
			return centroid(next, node, n);
		}
	}
	return node;
}

void dfs(int node, int prev, int weight, int depth, vector <pair<int, int>> &store) {
	store.push_back({weight, depth});
	for(auto [next, wt] : adj[node]) {
		if(next != prev && !vis[next]) {
			dfs(next, node, min(weight + wt, (int)1E9), depth + 1, store);
		}
	}
}

void process(int node) {
	vector<vector<pair<int, int>>> paths; // {sum of weights, length}.
	for(auto [next, wt] : adj[node]) {
		if(!vis[next]) {
			vector <pair<int, int>> temp;
			dfs(next, node, wt, 1, temp);
			paths.push_back(temp);
		}
	}
	// cout << node << '\n';
	// for(auto i : paths) {
		// for(auto j : i) cout << "{" << j.first << "," << j.second << "} ";
		// cout << '\n';
	// }
	if(paths.empty()) return;
	set<pair<int, int>> s;
	for(auto i : paths[0]) {
		if(i.first == k) ans = min(ans, i.second);
		else if(i.first < k) s.insert(i);
	}
	for(int i = 1; i < (int)paths.size(); i++) {
		for(auto j : paths[i]) {
			if(j.first < k) {
				auto it = s.lower_bound({k - j.first, 0});
				if(it != s.end() && (*it).first == k - j.first) {
					ans = min(ans, j.second + (*it).second);
				}
			}
		}
		for(auto j : paths[i]) {
			if(j.first == k) ans = min(ans, j.second);
			else if(j.first < k) s.insert(j);
		}
	}
}

void solve(int node, int prev) {
	find_size(node, prev);
	int c = centroid(node, prev, sz[node]);
	process(c);
	vis[c] = true;
	for(auto [next, wt] : adj[c]) {
		if(!vis[next]) solve(next, c);
	}
}

int best_path(int N, int K, int H[][2], int L[]) {
	n = N;
	k = K;
	adj.clear(); adj.resize(n);
	vis.clear(); vis.resize(n, false);
	sz.clear(); sz.resize(n);
	ans = 1E9;
	for(int i = 0; i < n - 1; i++) {
		adj[H[i][0]].push_back({H[i][1], L[i]});
		adj[H[i][1]].push_back({H[i][0], L[i]});
	}
	solve(0, 0);
	if(ans > n) return -1;
	return ans;
}

// int main() {
	// int n, k;
	// cin >> n >> k;
	// int H[n - 1][2];
	// int L[n - 1];
	// for(int i = 0; i < n - 1; i++) {
		// cin >> H[i][0] >> H[i][1];
	// }
	// for(int i = 0; i < n - 1; i++) {
		// cin >> L[i];
	// }
	// cout << best_path(n, k, H, L);
// }
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 308 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 308 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 320 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 2 ms 444 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 1 ms 320 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 308 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 200 ms 10320 KB Output is correct
20 Correct 185 ms 10496 KB Output is correct
21 Correct 220 ms 10424 KB Output is correct
22 Correct 179 ms 10172 KB Output is correct
23 Correct 102 ms 10808 KB Output is correct
24 Correct 80 ms 10392 KB Output is correct
25 Correct 221 ms 14596 KB Output is correct
26 Correct 252 ms 20184 KB Output is correct
27 Correct 233 ms 20164 KB Output is correct
28 Correct 345 ms 32528 KB Output is correct
29 Correct 346 ms 31672 KB Output is correct
30 Correct 218 ms 20260 KB Output is correct
31 Correct 219 ms 20276 KB Output is correct
32 Correct 263 ms 20340 KB Output is correct
33 Correct 309 ms 19804 KB Output is correct
34 Correct 239 ms 20848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 308 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 320 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 2 ms 444 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 1 ms 320 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 200 ms 10320 KB Output is correct
40 Correct 185 ms 10496 KB Output is correct
41 Correct 220 ms 10424 KB Output is correct
42 Correct 179 ms 10172 KB Output is correct
43 Correct 102 ms 10808 KB Output is correct
44 Correct 80 ms 10392 KB Output is correct
45 Correct 221 ms 14596 KB Output is correct
46 Correct 252 ms 20184 KB Output is correct
47 Correct 233 ms 20164 KB Output is correct
48 Correct 345 ms 32528 KB Output is correct
49 Correct 346 ms 31672 KB Output is correct
50 Correct 218 ms 20260 KB Output is correct
51 Correct 219 ms 20276 KB Output is correct
52 Correct 263 ms 20340 KB Output is correct
53 Correct 309 ms 19804 KB Output is correct
54 Correct 239 ms 20848 KB Output is correct
55 Correct 16 ms 1492 KB Output is correct
56 Correct 15 ms 1236 KB Output is correct
57 Correct 146 ms 10672 KB Output is correct
58 Correct 50 ms 14616 KB Output is correct
59 Correct 250 ms 20420 KB Output is correct
60 Correct 715 ms 40148 KB Output is correct
61 Correct 268 ms 21452 KB Output is correct
62 Correct 273 ms 22680 KB Output is correct
63 Correct 324 ms 22696 KB Output is correct
64 Correct 688 ms 29000 KB Output is correct
65 Correct 290 ms 21392 KB Output is correct
66 Correct 532 ms 29668 KB Output is correct
67 Correct 133 ms 30172 KB Output is correct
68 Correct 337 ms 25076 KB Output is correct
69 Correct 361 ms 25192 KB Output is correct
70 Correct 321 ms 23960 KB Output is correct