답안 #507490

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
507490 2022-01-12T14:41:11 Z haxorman 경주 (Race) (IOI11_race) C++14
100 / 100
437 ms 35844 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

const int mxN = 2e5 + 7;
const int mxK = 1e6 + 7;

int n, k;
vector<int> cur_seg;
vector<pair<int,int>> sums, graph[mxN];
int dp[mxN], p[mxN], len[mxK];
bool vis[mxN];

void dfs(int u, int par) {
	dp[u] = 1;
	p[u] = par;

	for (auto pr : graph[u]) {
		int v = pr.first;
		if (!vis[v] && v != par) {
			dfs(v, u);
			dp[u] += dp[v];
		}
	}

	cur_seg.push_back(u);
}

int find_centroid(int x) {
	cur_seg.clear();
	dfs(x, -1);

	for (int u : cur_seg) {
		int max_subtree = cur_seg.size() - dp[u];

		for (auto pr : graph[u]) {
			int v = pr.first;
			if (v != p[u] && !vis[v]) {
				max_subtree = max(max_subtree, dp[v]);
			}
		}

		if (max_subtree <= cur_seg.size() / 2) {
			return u;
		}
	}

	return x;
}

int ans = INT_MAX;

void find_min_path(int u, int par, int cnt, int sum) {
	if (sum < k && len[k - sum]) {
		ans = min(ans, cnt + len[k - sum]);
	}
	else if (sum == k) {
		ans = min(ans, cnt);
	}
	
	if (sum > k) {
		return;
	}

	for (auto pr : graph[u]) {
		int v = pr.first, w = pr.second;
		if (!vis[v] && v != par) {
			find_min_path(v, u, cnt + 1, sum + w);
		}
	}

	sums.push_back({sum, cnt});
}

void decompose(int x) {
	int c = find_centroid(x);
	vis[c] = true;
	
	vector<int> reset;

	for (auto pr : graph[c]) {
		int v = pr.first, w = pr.second;
		if (!vis[v]) {
			sums.clear();
			find_min_path(v, c, 1, w);

			for (auto prr : sums) {
				int sum = prr.first, cnt = prr.second;
				if (!len[sum]) {
					len[sum] = cnt;
				}
				else {
					len[sum] = min(len[sum], cnt);
				}
				reset.push_back(sum);
			}
		}
	}

	for (auto sum : reset) {
		len[sum] = 0;
	}

	for (auto pr : graph[c]) {
		int v = pr.first;
		if (!vis[v]) {
			decompose(v);
		}
	}
}

int best_path(int N, int K, int H[][2], int L[]) {
	n = N, k = K;
	for (int i = 0; i < n - 1; ++i) {
		int u = H[i][0], v = H[i][1], w = L[i];

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

	decompose(0);
	return (ans == INT_MAX ? -1 : ans);
}

Compilation message

race.cpp: In function 'int find_centroid(int)':
race.cpp:43:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |   if (max_subtree <= cur_seg.size() / 2) {
      |       ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 5040 KB Output is correct
15 Correct 4 ms 4940 KB Output is correct
16 Correct 2 ms 4940 KB Output is correct
17 Correct 2 ms 4940 KB Output is correct
18 Correct 2 ms 4940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 5040 KB Output is correct
15 Correct 4 ms 4940 KB Output is correct
16 Correct 2 ms 4940 KB Output is correct
17 Correct 2 ms 4940 KB Output is correct
18 Correct 2 ms 4940 KB Output is correct
19 Correct 2 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 3 ms 5068 KB Output is correct
22 Correct 7 ms 7500 KB Output is correct
23 Correct 4 ms 7116 KB Output is correct
24 Correct 6 ms 7884 KB Output is correct
25 Correct 5 ms 6820 KB Output is correct
26 Correct 4 ms 6348 KB Output is correct
27 Correct 3 ms 5092 KB Output is correct
28 Correct 4 ms 5836 KB Output is correct
29 Correct 4 ms 6348 KB Output is correct
30 Correct 4 ms 6476 KB Output is correct
31 Correct 4 ms 6860 KB Output is correct
32 Correct 4 ms 6956 KB Output is correct
33 Correct 5 ms 7640 KB Output is correct
34 Correct 5 ms 7244 KB Output is correct
35 Correct 5 ms 8020 KB Output is correct
36 Correct 5 ms 8268 KB Output is correct
37 Correct 4 ms 6988 KB Output is correct
38 Correct 4 ms 6220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 5040 KB Output is correct
15 Correct 4 ms 4940 KB Output is correct
16 Correct 2 ms 4940 KB Output is correct
17 Correct 2 ms 4940 KB Output is correct
18 Correct 2 ms 4940 KB Output is correct
19 Correct 126 ms 11344 KB Output is correct
20 Correct 131 ms 11204 KB Output is correct
21 Correct 146 ms 11784 KB Output is correct
22 Correct 118 ms 12200 KB Output is correct
23 Correct 85 ms 11460 KB Output is correct
24 Correct 60 ms 11304 KB Output is correct
25 Correct 142 ms 14872 KB Output is correct
26 Correct 99 ms 19592 KB Output is correct
27 Correct 160 ms 17568 KB Output is correct
28 Correct 245 ms 31820 KB Output is correct
29 Correct 293 ms 30780 KB Output is correct
30 Correct 170 ms 17488 KB Output is correct
31 Correct 199 ms 17476 KB Output is correct
32 Correct 209 ms 17612 KB Output is correct
33 Correct 227 ms 16472 KB Output is correct
34 Correct 198 ms 16440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 5040 KB Output is correct
15 Correct 4 ms 4940 KB Output is correct
16 Correct 2 ms 4940 KB Output is correct
17 Correct 2 ms 4940 KB Output is correct
18 Correct 2 ms 4940 KB Output is correct
19 Correct 2 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 3 ms 5068 KB Output is correct
22 Correct 7 ms 7500 KB Output is correct
23 Correct 4 ms 7116 KB Output is correct
24 Correct 6 ms 7884 KB Output is correct
25 Correct 5 ms 6820 KB Output is correct
26 Correct 4 ms 6348 KB Output is correct
27 Correct 3 ms 5092 KB Output is correct
28 Correct 4 ms 5836 KB Output is correct
29 Correct 4 ms 6348 KB Output is correct
30 Correct 4 ms 6476 KB Output is correct
31 Correct 4 ms 6860 KB Output is correct
32 Correct 4 ms 6956 KB Output is correct
33 Correct 5 ms 7640 KB Output is correct
34 Correct 5 ms 7244 KB Output is correct
35 Correct 5 ms 8020 KB Output is correct
36 Correct 5 ms 8268 KB Output is correct
37 Correct 4 ms 6988 KB Output is correct
38 Correct 4 ms 6220 KB Output is correct
39 Correct 126 ms 11344 KB Output is correct
40 Correct 131 ms 11204 KB Output is correct
41 Correct 146 ms 11784 KB Output is correct
42 Correct 118 ms 12200 KB Output is correct
43 Correct 85 ms 11460 KB Output is correct
44 Correct 60 ms 11304 KB Output is correct
45 Correct 142 ms 14872 KB Output is correct
46 Correct 99 ms 19592 KB Output is correct
47 Correct 160 ms 17568 KB Output is correct
48 Correct 245 ms 31820 KB Output is correct
49 Correct 293 ms 30780 KB Output is correct
50 Correct 170 ms 17488 KB Output is correct
51 Correct 199 ms 17476 KB Output is correct
52 Correct 209 ms 17612 KB Output is correct
53 Correct 227 ms 16472 KB Output is correct
54 Correct 198 ms 16440 KB Output is correct
55 Correct 12 ms 5708 KB Output is correct
56 Correct 12 ms 5708 KB Output is correct
57 Correct 103 ms 12336 KB Output is correct
58 Correct 35 ms 12476 KB Output is correct
59 Correct 115 ms 20024 KB Output is correct
60 Correct 416 ms 35844 KB Output is correct
61 Correct 236 ms 20892 KB Output is correct
62 Correct 191 ms 21704 KB Output is correct
63 Correct 345 ms 21772 KB Output is correct
64 Correct 437 ms 24116 KB Output is correct
65 Correct 239 ms 21304 KB Output is correct
66 Correct 362 ms 35256 KB Output is correct
67 Correct 135 ms 23436 KB Output is correct
68 Correct 244 ms 25144 KB Output is correct
69 Correct 254 ms 25460 KB Output is correct
70 Correct 224 ms 24204 KB Output is correct