답안 #914149

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
914149 2024-01-21T09:42:18 Z lamter 경주 (Race) (IOI11_race) C++17
100 / 100
739 ms 48736 KB
#include "race.h"
#include <bits/stdc++.h>

template <class Fun> class y_combinator_result {
	Fun fun_;

  public:
	template <class T>
	explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}

	template <class... Args> decltype(auto) operator()(Args &&...args) {
		return fun_(std::ref(*this), std::forward<Args>(args)...);
	}
};

template <class Fun> decltype(auto) y_combinator(Fun &&fun) {
	return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

struct Edge {
	int u, v, w;

	Edge(int _u = 0, int _v = 0, int _w = 0) : u(_u), v(_v), w(_w) {}
	int other(int x) const {
		//khong mot ai ngoai em
		return x ^ u ^ v;
	}
};

int best_path(int n, int k, int H[][2], int L[]) {
	std::vector <Edge> edges; edges.reserve(n - 1);
	std::vector <std::vector <int>> g(n);
	for (int i = 0; i < n - 1; i += 1) {
		int u = H[i][0], v = H[i][1], w = L[i];
		g[u].push_back(edges.size());
		g[v].push_back(edges.size());
		edges.emplace_back(u, v, w);
	}

	int ans = n + 1;
	std::vector <int> was(n), siz(n), lvl(n), dist(n);
	auto decompose = y_combinator([&] (auto decompose, int u) -> void {
		int sz = y_combinator([&] (auto dfs, int u, int p) -> int {
			int sz = 1;
			for (int id : g[u]) {
				int v = edges[id].other(u);
				if (v == p or was[v])
					continue;
				sz += dfs(v, u);
			}
			return siz[u] = sz;
		}) (u, u);
		auto centroid = y_combinator([&] (auto centroid, int u, int p) -> int {
			for (int id : g[u]) {
				int v = edges[id].other(u);
				if (v == p or was[v])
					continue;
				if (siz[v] > sz / 2)
					return centroid(v, u);
			}
			return u;
		});
		std::unordered_map <int, int> maps;

		auto chmin = [&] (int p, int x) -> void {
			auto it = maps.emplace(p, x);
			if (not it.second) {
				it.first->second = std::min(it.first->second, x);
			}
		};

		auto query = [&] (int p) -> int {
			auto it = maps.find(p);
			if (it == maps.end())
				return n + 1;
			return it->second;
		};

		int c = centroid(u, u);
		lvl[c] = 0;
		dist[c] = 0;
		auto upd = y_combinator([&] (auto dfs, int u, int p) -> void {
			for (int id : g[u]) {
				int v = edges[id].other(u);
				if (v == p or was[v])
					continue;
				if (dist[v] <= k) {
					dfs(v, u);
					chmin(dist[v], lvl[v]);
				}
			}
		});

		auto get = y_combinator([&] (auto dfs, int u, int p) -> void {
			for (int id : g[u]) {
				int v = edges[id].other(u);
				if (v == p or was[v])
					continue;
				lvl[v] = lvl[u] + 1;
				dist[v] = dist[u] + edges[id].w;
				if (dist[v] <= k) {
					dfs(v, u);
					ans = std::min(ans, lvl[v] + query(k - dist[v]));
				}
			}
		});

		for (int id : g[c]) {
			int x = edges[id].other(c);
			if (was[x])
				continue;
			lvl[x] = 1;
			dist[x] = edges[id].w;
			ans = std::min(ans, query(k - dist[x]) + 1);
			get(x, c);
			upd(x, c);
			chmin(dist[x], lvl[x]);
		}

		was[c] = 1;

		ans = std::min(ans, query(k));

		for (int id : g[c]) {
			int x = edges[id].other(c);
			if (was[x])
				continue;
			decompose(x);
		}
	});

	decompose(0);

	return ans == n + 1 ? -1 : ans;
}

int h[10000][2];
int l[10000];

#ifdef ILOVELAMTER
	int main(void) {
		h[0][0] = 0; h[0][1] = 1;
		h[1][0] = 1; h[1][1] = 2;
		h[2][0] = 1; h[2][1] = 3;
		l[0] = 1;
		l[1] = 2;
		l[2] = 4;
		std::cout << best_path(4, 3, h, l);
		return 0;
	}
#endif // ILOVELAMTER
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 1 ms 2392 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 2 ms 2652 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2500 KB Output is correct
28 Correct 2 ms 2396 KB Output is correct
29 Correct 2 ms 2648 KB Output is correct
30 Correct 2 ms 2652 KB Output is correct
31 Correct 2 ms 2652 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 2 ms 2648 KB Output is correct
34 Correct 2 ms 2396 KB Output is correct
35 Correct 1 ms 2396 KB Output is correct
36 Correct 2 ms 2396 KB Output is correct
37 Correct 2 ms 2652 KB Output is correct
38 Correct 2 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 154 ms 14368 KB Output is correct
20 Correct 166 ms 14300 KB Output is correct
21 Correct 162 ms 14384 KB Output is correct
22 Correct 147 ms 14416 KB Output is correct
23 Correct 71 ms 14164 KB Output is correct
24 Correct 59 ms 14604 KB Output is correct
25 Correct 139 ms 16460 KB Output is correct
26 Correct 127 ms 18352 KB Output is correct
27 Correct 198 ms 24652 KB Output is correct
28 Correct 231 ms 32352 KB Output is correct
29 Correct 267 ms 31564 KB Output is correct
30 Correct 220 ms 24796 KB Output is correct
31 Correct 169 ms 24680 KB Output is correct
32 Correct 238 ms 24656 KB Output is correct
33 Correct 272 ms 24336 KB Output is correct
34 Correct 159 ms 24796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 1 ms 2392 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 2 ms 2652 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2500 KB Output is correct
28 Correct 2 ms 2396 KB Output is correct
29 Correct 2 ms 2648 KB Output is correct
30 Correct 2 ms 2652 KB Output is correct
31 Correct 2 ms 2652 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 2 ms 2648 KB Output is correct
34 Correct 2 ms 2396 KB Output is correct
35 Correct 1 ms 2396 KB Output is correct
36 Correct 2 ms 2396 KB Output is correct
37 Correct 2 ms 2652 KB Output is correct
38 Correct 2 ms 2652 KB Output is correct
39 Correct 154 ms 14368 KB Output is correct
40 Correct 166 ms 14300 KB Output is correct
41 Correct 162 ms 14384 KB Output is correct
42 Correct 147 ms 14416 KB Output is correct
43 Correct 71 ms 14164 KB Output is correct
44 Correct 59 ms 14604 KB Output is correct
45 Correct 139 ms 16460 KB Output is correct
46 Correct 127 ms 18352 KB Output is correct
47 Correct 198 ms 24652 KB Output is correct
48 Correct 231 ms 32352 KB Output is correct
49 Correct 267 ms 31564 KB Output is correct
50 Correct 220 ms 24796 KB Output is correct
51 Correct 169 ms 24680 KB Output is correct
52 Correct 238 ms 24656 KB Output is correct
53 Correct 272 ms 24336 KB Output is correct
54 Correct 159 ms 24796 KB Output is correct
55 Correct 13 ms 3672 KB Output is correct
56 Correct 12 ms 3420 KB Output is correct
57 Correct 110 ms 14272 KB Output is correct
58 Correct 33 ms 14284 KB Output is correct
59 Correct 187 ms 24276 KB Output is correct
60 Correct 739 ms 48736 KB Output is correct
61 Correct 232 ms 25056 KB Output is correct
62 Correct 262 ms 24960 KB Output is correct
63 Correct 345 ms 24912 KB Output is correct
64 Correct 735 ms 34788 KB Output is correct
65 Correct 209 ms 25176 KB Output is correct
66 Correct 485 ms 31824 KB Output is correct
67 Correct 92 ms 25288 KB Output is correct
68 Correct 446 ms 31396 KB Output is correct
69 Correct 348 ms 31332 KB Output is correct
70 Correct 384 ms 30396 KB Output is correct