답안 #854777

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
854777 2023-09-28T20:19:21 Z alex_2008 경주 (Race) (IOI11_race) C++17
100 / 100
603 ms 60176 KB
#include "race.h"
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
#include <unordered_map>
#include <cassert>
using namespace std;
const int MAX_N = 5e5 + 10;
bool used[MAX_N];
int subtree[MAX_N], dist[MAX_N], height[MAX_N];
int h[MAX_N][2], l[MAX_N];
int KK;
int ans = MAX_N;
vector <vector<pair<int, int>>> G;
int find_centroid(int v) {
	int p = -1;
	int u = v;
	while (1) {
		int x = -1;
		for (auto it : G[u]) {
			if (it.first == p) continue;
			if (used[it.first]) continue;
			if (2 * subtree[it.first] > subtree[v]) {
				x = it.first;
				break;
			}
		}
		if (x == -1) return u;
		p = u;
	    u = x;
	}
}
void dfs(int v, int p) {
	if (p == -1) {
		dist[v] = 0;
		height[v] = 0;
	}
	subtree[v] = 1;
	for (auto it : G[v]) {
		if (it.first == p || used[it.first]) continue;
		dist[it.first] = min(dist[v] + it.second, KK + 1);
		height[it.first] = height[v] + 1;
		dfs(it.first, v);
		subtree[v] += subtree[it.first];
	}
}
vector <int> vec;
void find_vertexes(int v, int p) {
	vec.push_back(v);
	for (auto it : G[v]) {
		if (used[it.first] || it.first == p) continue;
		find_vertexes(it.first, v);
	}
}
void solve(int v) {
	dfs(v, -1);
	v = find_centroid(v);
	dfs(v, -1);
	vector <vector<int>> u;
	for (auto it : G[v]) {
		if (used[it.first]) continue;
		vec.clear();
		find_vertexes(it.first, v);
		u.push_back(vec);
	}
	unordered_map <int, int> mp;
	for (auto it : u) {
		for (auto j : it) {
			if (mp.find(KK - dist[j]) != mp.end()) {
				ans = min(ans, height[j] + mp[KK - dist[j]]);
			}
		}
		for (auto j : it) {
			if (mp.find(dist[j]) == mp.end()) {
				mp[dist[j]] = height[j];
			}
			else mp[dist[j]] = min(mp[dist[j]], height[j]);
		}
	}
	if (mp.find(KK) != mp.end()) {
		ans = min(ans, mp[KK]);
	}
	used[v] = true;
	for (auto it : G[v]) {
		if (used[it.first]) continue;
		solve(it.first);
	}
}
int best_path(int N, int K, int H[][2], int L[]) {
	KK = K;
	G.resize(N);
	for (int i = 0; i < N - 1; i++) {
		G[H[i][0]].push_back({ H[i][1], L[i] });
		G[H[i][1]].push_back({ H[i][0], L[i] });
	}
	solve(0);
	if (ans == MAX_N) return -1;
	return ans;
}
//int main() {
//	int n, k;
//	cin >> n >> k;
//	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) << "\n";
//}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 1 ms 10688 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 1 ms 10688 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 2 ms 10688 KB Output is correct
9 Correct 1 ms 10588 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
11 Correct 1 ms 10588 KB Output is correct
12 Correct 1 ms 10684 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 1 ms 10584 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 1 ms 10588 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
18 Correct 2 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 1 ms 10688 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 1 ms 10688 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 2 ms 10688 KB Output is correct
9 Correct 1 ms 10588 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
11 Correct 1 ms 10588 KB Output is correct
12 Correct 1 ms 10684 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 1 ms 10584 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 1 ms 10588 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
18 Correct 2 ms 10588 KB Output is correct
19 Correct 2 ms 10584 KB Output is correct
20 Correct 1 ms 10584 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 3 ms 10768 KB Output is correct
23 Correct 2 ms 10588 KB Output is correct
24 Correct 2 ms 10588 KB Output is correct
25 Correct 3 ms 10844 KB Output is correct
26 Correct 2 ms 10588 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 2 ms 10668 KB Output is correct
30 Correct 2 ms 10844 KB Output is correct
31 Correct 2 ms 10840 KB Output is correct
32 Correct 2 ms 10840 KB Output is correct
33 Correct 3 ms 10844 KB Output is correct
34 Correct 2 ms 10588 KB Output is correct
35 Correct 2 ms 10588 KB Output is correct
36 Correct 2 ms 10588 KB Output is correct
37 Correct 3 ms 10844 KB Output is correct
38 Correct 2 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 1 ms 10688 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 1 ms 10688 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 2 ms 10688 KB Output is correct
9 Correct 1 ms 10588 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
11 Correct 1 ms 10588 KB Output is correct
12 Correct 1 ms 10684 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 1 ms 10584 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 1 ms 10588 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
18 Correct 2 ms 10588 KB Output is correct
19 Correct 155 ms 21352 KB Output is correct
20 Correct 160 ms 21668 KB Output is correct
21 Correct 141 ms 21392 KB Output is correct
22 Correct 122 ms 21284 KB Output is correct
23 Correct 145 ms 21432 KB Output is correct
24 Correct 76 ms 21072 KB Output is correct
25 Correct 160 ms 24852 KB Output is correct
26 Correct 116 ms 28688 KB Output is correct
27 Correct 221 ms 28896 KB Output is correct
28 Correct 452 ms 44100 KB Output is correct
29 Correct 428 ms 43244 KB Output is correct
30 Correct 195 ms 28752 KB Output is correct
31 Correct 190 ms 28684 KB Output is correct
32 Correct 236 ms 28752 KB Output is correct
33 Correct 295 ms 28860 KB Output is correct
34 Correct 300 ms 29320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 1 ms 10688 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 1 ms 10688 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 1 ms 10588 KB Output is correct
8 Correct 2 ms 10688 KB Output is correct
9 Correct 1 ms 10588 KB Output is correct
10 Correct 1 ms 10588 KB Output is correct
11 Correct 1 ms 10588 KB Output is correct
12 Correct 1 ms 10684 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 1 ms 10584 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 1 ms 10588 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
18 Correct 2 ms 10588 KB Output is correct
19 Correct 2 ms 10584 KB Output is correct
20 Correct 1 ms 10584 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 3 ms 10768 KB Output is correct
23 Correct 2 ms 10588 KB Output is correct
24 Correct 2 ms 10588 KB Output is correct
25 Correct 3 ms 10844 KB Output is correct
26 Correct 2 ms 10588 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 2 ms 10668 KB Output is correct
30 Correct 2 ms 10844 KB Output is correct
31 Correct 2 ms 10840 KB Output is correct
32 Correct 2 ms 10840 KB Output is correct
33 Correct 3 ms 10844 KB Output is correct
34 Correct 2 ms 10588 KB Output is correct
35 Correct 2 ms 10588 KB Output is correct
36 Correct 2 ms 10588 KB Output is correct
37 Correct 3 ms 10844 KB Output is correct
38 Correct 2 ms 10844 KB Output is correct
39 Correct 155 ms 21352 KB Output is correct
40 Correct 160 ms 21668 KB Output is correct
41 Correct 141 ms 21392 KB Output is correct
42 Correct 122 ms 21284 KB Output is correct
43 Correct 145 ms 21432 KB Output is correct
44 Correct 76 ms 21072 KB Output is correct
45 Correct 160 ms 24852 KB Output is correct
46 Correct 116 ms 28688 KB Output is correct
47 Correct 221 ms 28896 KB Output is correct
48 Correct 452 ms 44100 KB Output is correct
49 Correct 428 ms 43244 KB Output is correct
50 Correct 195 ms 28752 KB Output is correct
51 Correct 190 ms 28684 KB Output is correct
52 Correct 236 ms 28752 KB Output is correct
53 Correct 295 ms 28860 KB Output is correct
54 Correct 300 ms 29320 KB Output is correct
55 Correct 13 ms 11868 KB Output is correct
56 Correct 12 ms 11612 KB Output is correct
57 Correct 104 ms 21472 KB Output is correct
58 Correct 37 ms 25788 KB Output is correct
59 Correct 184 ms 34596 KB Output is correct
60 Correct 603 ms 60176 KB Output is correct
61 Correct 222 ms 29156 KB Output is correct
62 Correct 217 ms 29000 KB Output is correct
63 Correct 233 ms 29004 KB Output is correct
64 Correct 499 ms 40048 KB Output is correct
65 Correct 295 ms 30060 KB Output is correct
66 Correct 524 ms 41556 KB Output is correct
67 Correct 106 ms 40380 KB Output is correct
68 Correct 278 ms 36284 KB Output is correct
69 Correct 296 ms 36176 KB Output is correct
70 Correct 259 ms 35032 KB Output is correct