답안 #456362

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
456362 2021-08-06T14:24:05 Z TruaShamu 경주 (Race) (IOI11_race) C++17
100 / 100
634 ms 34000 KB
#include <bits/stdc++.h>
using namespace std;

#define MAXN 200005
#define ii pair<int, int>
#define ll long long

int N, K, ans, cnt[1000100], subSize[MAXN], root[MAXN];
vector<ii> adj[MAXN];
inline void minimize(int& a, int b) {
	if (a == -1 || a > b) {
		a = b;
	}
}


void get_cnt(int sum, bool filling, int cur, int par = -1, int depth = 1) {
	if (sum > K) {
		return;
	}
	if (filling) {
		minimize(cnt[sum], depth);
	}
	else {
		if (cnt[K - sum] != -1) {
			minimize(ans, depth + cnt[K - sum]);
		}
	}
	for (auto& oPair : adj[cur]) {
		int next = oPair.first;
		int cost = oPair.second;
		if (next != par && !root[next]) {
			get_cnt(sum + cost, filling, next, cur, depth + 1);
		}
	}
}

void del(int sum, int cur, int par = -1) {
	if (sum > K) return;
	cnt[sum] = -1;
	for (auto& oPair : adj[cur]) {
		int next = oPair.first;
		int cost = oPair.second;
		if (next != par && !root[next]) {
			del(sum + cost, next, cur);
		}
	}
}


// Find subtree size under each node.
int dfs(int cur, int par = -1) {
	subSize[cur] = 1;
	for (auto& oPair : adj[cur]) {
		int next = oPair.first;
		int cost = oPair.second;
		if (next != par && !root[next]) {
			subSize[cur] += dfs(next, cur);
		}
	}
	return subSize[cur];
}


// Get the centroid within the subtree.
int get_centroid(int size, int cur, int par = -1) {
	for (auto& oPair : adj[cur]) {
		int next = oPair.first;
		if (next != par && !root[next] && 2 * subSize[next] > size) {
			return get_centroid(size, next, cur);
		}
	}
	return cur;
}


void centroid(int cur = 0) {
	cur = get_centroid(dfs(cur), cur);
	root[cur] = 1;
	cnt[0] = 0;
	for (auto& oPair : adj[cur]) {
		int next = oPair.first;
		int cost = oPair.second;
		if (!root[next]) {
			get_cnt(cost, false, next);
			get_cnt(cost, true, next);
		}
	}

	for (auto& oPair : adj[cur]) {
		int next = oPair.first;
		int cost = oPair.second;
		if (!root[next]) {
			del(cost, next);
		}
	}
	for (auto& oPair : adj[cur]) {
		int next = oPair.first;
		if (!root[next]) {
			centroid(next);
		}
	}
}


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

	}
	ans = -1;
	memset(cnt, -1, sizeof(cnt));
	centroid();
	return ans;
}

Compilation message

race.cpp: In function 'int dfs(int, int)':
race.cpp:56:7: warning: unused variable 'cost' [-Wunused-variable]
   56 |   int cost = oPair.second;
      |       ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8844 KB Output is correct
2 Correct 6 ms 8844 KB Output is correct
3 Correct 5 ms 8844 KB Output is correct
4 Correct 6 ms 8908 KB Output is correct
5 Correct 5 ms 8908 KB Output is correct
6 Correct 5 ms 8852 KB Output is correct
7 Correct 6 ms 8844 KB Output is correct
8 Correct 5 ms 8896 KB Output is correct
9 Correct 5 ms 8908 KB Output is correct
10 Correct 5 ms 8908 KB Output is correct
11 Correct 5 ms 8908 KB Output is correct
12 Correct 5 ms 8908 KB Output is correct
13 Correct 6 ms 8908 KB Output is correct
14 Correct 6 ms 8908 KB Output is correct
15 Correct 5 ms 8852 KB Output is correct
16 Correct 5 ms 8908 KB Output is correct
17 Correct 5 ms 8848 KB Output is correct
18 Correct 5 ms 8908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8844 KB Output is correct
2 Correct 6 ms 8844 KB Output is correct
3 Correct 5 ms 8844 KB Output is correct
4 Correct 6 ms 8908 KB Output is correct
5 Correct 5 ms 8908 KB Output is correct
6 Correct 5 ms 8852 KB Output is correct
7 Correct 6 ms 8844 KB Output is correct
8 Correct 5 ms 8896 KB Output is correct
9 Correct 5 ms 8908 KB Output is correct
10 Correct 5 ms 8908 KB Output is correct
11 Correct 5 ms 8908 KB Output is correct
12 Correct 5 ms 8908 KB Output is correct
13 Correct 6 ms 8908 KB Output is correct
14 Correct 6 ms 8908 KB Output is correct
15 Correct 5 ms 8852 KB Output is correct
16 Correct 5 ms 8908 KB Output is correct
17 Correct 5 ms 8848 KB Output is correct
18 Correct 5 ms 8908 KB Output is correct
19 Correct 5 ms 8908 KB Output is correct
20 Correct 5 ms 8908 KB Output is correct
21 Correct 6 ms 8984 KB Output is correct
22 Correct 5 ms 8908 KB Output is correct
23 Correct 6 ms 8908 KB Output is correct
24 Correct 6 ms 8908 KB Output is correct
25 Correct 6 ms 8908 KB Output is correct
26 Correct 6 ms 8908 KB Output is correct
27 Correct 6 ms 8988 KB Output is correct
28 Correct 6 ms 8908 KB Output is correct
29 Correct 7 ms 8908 KB Output is correct
30 Correct 6 ms 8908 KB Output is correct
31 Correct 5 ms 8988 KB Output is correct
32 Correct 6 ms 8988 KB Output is correct
33 Correct 6 ms 8908 KB Output is correct
34 Correct 6 ms 8908 KB Output is correct
35 Correct 6 ms 8908 KB Output is correct
36 Correct 6 ms 8908 KB Output is correct
37 Correct 6 ms 8908 KB Output is correct
38 Correct 6 ms 8912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8844 KB Output is correct
2 Correct 6 ms 8844 KB Output is correct
3 Correct 5 ms 8844 KB Output is correct
4 Correct 6 ms 8908 KB Output is correct
5 Correct 5 ms 8908 KB Output is correct
6 Correct 5 ms 8852 KB Output is correct
7 Correct 6 ms 8844 KB Output is correct
8 Correct 5 ms 8896 KB Output is correct
9 Correct 5 ms 8908 KB Output is correct
10 Correct 5 ms 8908 KB Output is correct
11 Correct 5 ms 8908 KB Output is correct
12 Correct 5 ms 8908 KB Output is correct
13 Correct 6 ms 8908 KB Output is correct
14 Correct 6 ms 8908 KB Output is correct
15 Correct 5 ms 8852 KB Output is correct
16 Correct 5 ms 8908 KB Output is correct
17 Correct 5 ms 8848 KB Output is correct
18 Correct 5 ms 8908 KB Output is correct
19 Correct 162 ms 15880 KB Output is correct
20 Correct 156 ms 15804 KB Output is correct
21 Correct 173 ms 15896 KB Output is correct
22 Correct 159 ms 15984 KB Output is correct
23 Correct 89 ms 16196 KB Output is correct
24 Correct 68 ms 16104 KB Output is correct
25 Correct 149 ms 18644 KB Output is correct
26 Correct 125 ms 21576 KB Output is correct
27 Correct 195 ms 22860 KB Output is correct
28 Correct 279 ms 34000 KB Output is correct
29 Correct 278 ms 33184 KB Output is correct
30 Correct 190 ms 22852 KB Output is correct
31 Correct 195 ms 22800 KB Output is correct
32 Correct 238 ms 23072 KB Output is correct
33 Correct 253 ms 21696 KB Output is correct
34 Correct 210 ms 21752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8844 KB Output is correct
2 Correct 6 ms 8844 KB Output is correct
3 Correct 5 ms 8844 KB Output is correct
4 Correct 6 ms 8908 KB Output is correct
5 Correct 5 ms 8908 KB Output is correct
6 Correct 5 ms 8852 KB Output is correct
7 Correct 6 ms 8844 KB Output is correct
8 Correct 5 ms 8896 KB Output is correct
9 Correct 5 ms 8908 KB Output is correct
10 Correct 5 ms 8908 KB Output is correct
11 Correct 5 ms 8908 KB Output is correct
12 Correct 5 ms 8908 KB Output is correct
13 Correct 6 ms 8908 KB Output is correct
14 Correct 6 ms 8908 KB Output is correct
15 Correct 5 ms 8852 KB Output is correct
16 Correct 5 ms 8908 KB Output is correct
17 Correct 5 ms 8848 KB Output is correct
18 Correct 5 ms 8908 KB Output is correct
19 Correct 5 ms 8908 KB Output is correct
20 Correct 5 ms 8908 KB Output is correct
21 Correct 6 ms 8984 KB Output is correct
22 Correct 5 ms 8908 KB Output is correct
23 Correct 6 ms 8908 KB Output is correct
24 Correct 6 ms 8908 KB Output is correct
25 Correct 6 ms 8908 KB Output is correct
26 Correct 6 ms 8908 KB Output is correct
27 Correct 6 ms 8988 KB Output is correct
28 Correct 6 ms 8908 KB Output is correct
29 Correct 7 ms 8908 KB Output is correct
30 Correct 6 ms 8908 KB Output is correct
31 Correct 5 ms 8988 KB Output is correct
32 Correct 6 ms 8988 KB Output is correct
33 Correct 6 ms 8908 KB Output is correct
34 Correct 6 ms 8908 KB Output is correct
35 Correct 6 ms 8908 KB Output is correct
36 Correct 6 ms 8908 KB Output is correct
37 Correct 6 ms 8908 KB Output is correct
38 Correct 6 ms 8912 KB Output is correct
39 Correct 162 ms 15880 KB Output is correct
40 Correct 156 ms 15804 KB Output is correct
41 Correct 173 ms 15896 KB Output is correct
42 Correct 159 ms 15984 KB Output is correct
43 Correct 89 ms 16196 KB Output is correct
44 Correct 68 ms 16104 KB Output is correct
45 Correct 149 ms 18644 KB Output is correct
46 Correct 125 ms 21576 KB Output is correct
47 Correct 195 ms 22860 KB Output is correct
48 Correct 279 ms 34000 KB Output is correct
49 Correct 278 ms 33184 KB Output is correct
50 Correct 190 ms 22852 KB Output is correct
51 Correct 195 ms 22800 KB Output is correct
52 Correct 238 ms 23072 KB Output is correct
53 Correct 253 ms 21696 KB Output is correct
54 Correct 210 ms 21752 KB Output is correct
55 Correct 15 ms 9600 KB Output is correct
56 Correct 16 ms 9548 KB Output is correct
57 Correct 106 ms 16008 KB Output is correct
58 Correct 51 ms 15756 KB Output is correct
59 Correct 120 ms 21572 KB Output is correct
60 Correct 577 ms 33816 KB Output is correct
61 Correct 238 ms 23400 KB Output is correct
62 Correct 247 ms 23236 KB Output is correct
63 Correct 331 ms 23368 KB Output is correct
64 Correct 634 ms 23568 KB Output is correct
65 Correct 251 ms 24160 KB Output is correct
66 Correct 464 ms 31828 KB Output is correct
67 Correct 138 ms 23920 KB Output is correct
68 Correct 301 ms 24116 KB Output is correct
69 Correct 315 ms 24268 KB Output is correct
70 Correct 283 ms 23496 KB Output is correct