답안 #379835

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
379835 2021-03-19T13:12:09 Z huynhtrankhanh 경주 (Race) (IOI11_race) C++17
100 / 100
563 ms 48236 KB
#include <bits/stdc++.h>
using namespace std;

int best_path(int n, int k, int h[][2], int l[])
{
	const int maxn = 200001;
	const int inf = 0x3f3f3f3f;

	vector<pair<int, int>> graph[maxn];

	for (int i = 0; i < n - 1; i++)
	{
		int u = h[i][0];
		int v = h[i][1];
		int w = l[i];

		graph[u].emplace_back(v, w);
		graph[v].emplace_back(u, w);
	}

	int size[maxn];
	bool ignored[maxn];
	memset(ignored, 0, sizeof(ignored));

	auto get_centroid = [&](int root) {
		{
			auto dfs = [&](auto dfs, int vertex, int parent) -> void {
				size[vertex] = 0;
				for (auto [adjacent, _]: graph[vertex])
					if (adjacent != parent) if (!ignored[adjacent])
					{
						dfs(dfs, adjacent, vertex);
						size[vertex] += size[adjacent];
					}

				size[vertex]++;
			};

			dfs(dfs, root, -1);
		}

		auto dfs = [&](auto dfs, int vertex, int parent) -> int {
			for (auto [adjacent, _]: graph[vertex])
				if (adjacent != parent) if (!ignored[adjacent])
				if (size[adjacent] > size[root] / 2)
					return dfs(dfs, adjacent, vertex);

			return vertex;
		};

		return dfs(dfs, root, -1);
	};

	const int maxk = 1000001;

	int depth[maxk];

	int counters[maxk];
	memset(counters, 0, sizeof(counters));
	int counter = 0;

	auto get = [&](int index) {
		if (index >= maxk) return inf;
		if (counters[index] == counter) return depth[index];
		return inf;
	};

	auto store = [&](int index, int value) {
		if (index >= maxk) return;
		counters[index] = counter;
		depth[index] = value;
	};

	auto clear = [&]() {
		counter++;
	};

	int result = inf;

	struct distance_t
	{
		int depth, distance;
	};

	distance_t distances[maxn];

	auto dfs = [&](auto dfs, int vertex) -> void {
		int centroid = get_centroid(vertex);
		ignored[centroid] = true;

		clear();

		store(0, 0);

		for (auto [adjacent, initial_edge]: graph[centroid])
		{
			if (ignored[adjacent]) continue;

			int counter = 0;
			auto dfs = [&](auto dfs, int vertex, int parent, int depth, int distance) -> void {
				auto clamp = [&](int x) {
					return min(inf, x);
				};

				distances[counter++] = { depth + 1, clamp(distance + initial_edge) };

				for (auto [adjacent, weight]: graph[vertex])
					if (adjacent != parent) if (!ignored[adjacent])
						dfs(dfs, adjacent, vertex, depth + 1, clamp(distance + weight));
			};


			dfs(dfs, adjacent, centroid, 0, 0);

			for (int i = 0; i < counter; i++)
			{
				auto &distance = distances[i];

				int companion = k - distance.distance;
				if (companion < 0) continue;

				result = min(result, get(companion) + distance.depth);
			}

			for (int i = 0; i < counter; i++)
			{
				auto &distance = distances[i];
				store(distance.distance, min(get(distance.distance), distance.depth));
			}
		}

		for (auto [adjacent, _]: graph[centroid])
			if (!ignored[adjacent])
				dfs(dfs, adjacent);
	};

	dfs(dfs, 0);

	return result == inf ? -1 : result;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9196 KB Output is correct
2 Correct 6 ms 9196 KB Output is correct
3 Correct 6 ms 9196 KB Output is correct
4 Correct 6 ms 9196 KB Output is correct
5 Correct 6 ms 9196 KB Output is correct
6 Correct 6 ms 9196 KB Output is correct
7 Correct 7 ms 9196 KB Output is correct
8 Correct 6 ms 9196 KB Output is correct
9 Correct 7 ms 9196 KB Output is correct
10 Correct 7 ms 9196 KB Output is correct
11 Correct 6 ms 9196 KB Output is correct
12 Correct 6 ms 9196 KB Output is correct
13 Correct 7 ms 9196 KB Output is correct
14 Correct 6 ms 9196 KB Output is correct
15 Correct 6 ms 9196 KB Output is correct
16 Correct 7 ms 9196 KB Output is correct
17 Correct 8 ms 9196 KB Output is correct
18 Correct 6 ms 9196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9196 KB Output is correct
2 Correct 6 ms 9196 KB Output is correct
3 Correct 6 ms 9196 KB Output is correct
4 Correct 6 ms 9196 KB Output is correct
5 Correct 6 ms 9196 KB Output is correct
6 Correct 6 ms 9196 KB Output is correct
7 Correct 7 ms 9196 KB Output is correct
8 Correct 6 ms 9196 KB Output is correct
9 Correct 7 ms 9196 KB Output is correct
10 Correct 7 ms 9196 KB Output is correct
11 Correct 6 ms 9196 KB Output is correct
12 Correct 6 ms 9196 KB Output is correct
13 Correct 7 ms 9196 KB Output is correct
14 Correct 6 ms 9196 KB Output is correct
15 Correct 6 ms 9196 KB Output is correct
16 Correct 7 ms 9196 KB Output is correct
17 Correct 8 ms 9196 KB Output is correct
18 Correct 6 ms 9196 KB Output is correct
19 Correct 7 ms 9196 KB Output is correct
20 Correct 8 ms 9196 KB Output is correct
21 Correct 7 ms 9196 KB Output is correct
22 Correct 9 ms 11884 KB Output is correct
23 Correct 9 ms 12140 KB Output is correct
24 Correct 8 ms 12524 KB Output is correct
25 Correct 8 ms 10988 KB Output is correct
26 Correct 9 ms 13036 KB Output is correct
27 Correct 7 ms 9196 KB Output is correct
28 Correct 8 ms 10604 KB Output is correct
29 Correct 8 ms 10988 KB Output is correct
30 Correct 8 ms 10860 KB Output is correct
31 Correct 8 ms 10988 KB Output is correct
32 Correct 8 ms 11116 KB Output is correct
33 Correct 9 ms 12012 KB Output is correct
34 Correct 9 ms 12800 KB Output is correct
35 Correct 9 ms 12652 KB Output is correct
36 Correct 9 ms 12652 KB Output is correct
37 Correct 8 ms 11116 KB Output is correct
38 Correct 8 ms 10348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9196 KB Output is correct
2 Correct 6 ms 9196 KB Output is correct
3 Correct 6 ms 9196 KB Output is correct
4 Correct 6 ms 9196 KB Output is correct
5 Correct 6 ms 9196 KB Output is correct
6 Correct 6 ms 9196 KB Output is correct
7 Correct 7 ms 9196 KB Output is correct
8 Correct 6 ms 9196 KB Output is correct
9 Correct 7 ms 9196 KB Output is correct
10 Correct 7 ms 9196 KB Output is correct
11 Correct 6 ms 9196 KB Output is correct
12 Correct 6 ms 9196 KB Output is correct
13 Correct 7 ms 9196 KB Output is correct
14 Correct 6 ms 9196 KB Output is correct
15 Correct 6 ms 9196 KB Output is correct
16 Correct 7 ms 9196 KB Output is correct
17 Correct 8 ms 9196 KB Output is correct
18 Correct 6 ms 9196 KB Output is correct
19 Correct 204 ms 14700 KB Output is correct
20 Correct 207 ms 14700 KB Output is correct
21 Correct 177 ms 14824 KB Output is correct
22 Correct 153 ms 14828 KB Output is correct
23 Correct 137 ms 14988 KB Output is correct
24 Correct 90 ms 14816 KB Output is correct
25 Correct 175 ms 20588 KB Output is correct
26 Correct 123 ms 25196 KB Output is correct
27 Correct 237 ms 20076 KB Output is correct
28 Correct 548 ms 48236 KB Output is correct
29 Correct 549 ms 46572 KB Output is correct
30 Correct 240 ms 22892 KB Output is correct
31 Correct 244 ms 22764 KB Output is correct
32 Correct 320 ms 22892 KB Output is correct
33 Correct 400 ms 22508 KB Output is correct
34 Correct 377 ms 27116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9196 KB Output is correct
2 Correct 6 ms 9196 KB Output is correct
3 Correct 6 ms 9196 KB Output is correct
4 Correct 6 ms 9196 KB Output is correct
5 Correct 6 ms 9196 KB Output is correct
6 Correct 6 ms 9196 KB Output is correct
7 Correct 7 ms 9196 KB Output is correct
8 Correct 6 ms 9196 KB Output is correct
9 Correct 7 ms 9196 KB Output is correct
10 Correct 7 ms 9196 KB Output is correct
11 Correct 6 ms 9196 KB Output is correct
12 Correct 6 ms 9196 KB Output is correct
13 Correct 7 ms 9196 KB Output is correct
14 Correct 6 ms 9196 KB Output is correct
15 Correct 6 ms 9196 KB Output is correct
16 Correct 7 ms 9196 KB Output is correct
17 Correct 8 ms 9196 KB Output is correct
18 Correct 6 ms 9196 KB Output is correct
19 Correct 7 ms 9196 KB Output is correct
20 Correct 8 ms 9196 KB Output is correct
21 Correct 7 ms 9196 KB Output is correct
22 Correct 9 ms 11884 KB Output is correct
23 Correct 9 ms 12140 KB Output is correct
24 Correct 8 ms 12524 KB Output is correct
25 Correct 8 ms 10988 KB Output is correct
26 Correct 9 ms 13036 KB Output is correct
27 Correct 7 ms 9196 KB Output is correct
28 Correct 8 ms 10604 KB Output is correct
29 Correct 8 ms 10988 KB Output is correct
30 Correct 8 ms 10860 KB Output is correct
31 Correct 8 ms 10988 KB Output is correct
32 Correct 8 ms 11116 KB Output is correct
33 Correct 9 ms 12012 KB Output is correct
34 Correct 9 ms 12800 KB Output is correct
35 Correct 9 ms 12652 KB Output is correct
36 Correct 9 ms 12652 KB Output is correct
37 Correct 8 ms 11116 KB Output is correct
38 Correct 8 ms 10348 KB Output is correct
39 Correct 204 ms 14700 KB Output is correct
40 Correct 207 ms 14700 KB Output is correct
41 Correct 177 ms 14824 KB Output is correct
42 Correct 153 ms 14828 KB Output is correct
43 Correct 137 ms 14988 KB Output is correct
44 Correct 90 ms 14816 KB Output is correct
45 Correct 175 ms 20588 KB Output is correct
46 Correct 123 ms 25196 KB Output is correct
47 Correct 237 ms 20076 KB Output is correct
48 Correct 548 ms 48236 KB Output is correct
49 Correct 549 ms 46572 KB Output is correct
50 Correct 240 ms 22892 KB Output is correct
51 Correct 244 ms 22764 KB Output is correct
52 Correct 320 ms 22892 KB Output is correct
53 Correct 400 ms 22508 KB Output is correct
54 Correct 377 ms 27116 KB Output is correct
55 Correct 16 ms 10020 KB Output is correct
56 Correct 16 ms 9836 KB Output is correct
57 Correct 104 ms 16236 KB Output is correct
58 Correct 48 ms 15712 KB Output is correct
59 Correct 129 ms 27116 KB Output is correct
60 Correct 497 ms 45548 KB Output is correct
61 Correct 258 ms 23192 KB Output is correct
62 Correct 240 ms 23148 KB Output is correct
63 Correct 347 ms 22992 KB Output is correct
64 Correct 563 ms 25068 KB Output is correct
65 Correct 447 ms 28356 KB Output is correct
66 Correct 554 ms 43116 KB Output is correct
67 Correct 148 ms 23516 KB Output is correct
68 Correct 334 ms 27100 KB Output is correct
69 Correct 371 ms 27244 KB Output is correct
70 Correct 339 ms 26348 KB Output is correct