답안 #513859

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
513859 2022-01-17T18:55:05 Z Eyed 경주 (Race) (IOI11_race) C++17
100 / 100
498 ms 109136 KB
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll MOD = 1e9 + 7;

//IOI 2011 Race
// Problem: https://oj.uz/problem/view/IOI11_race

struct edge {
	ll b;
	ll w;
	edge(ll a = 0, ll y = 0) : b(a), w(y) {};
};

vector<edge> tree[200005];
map<ll, ll> dists[200005];
ll minK[200005];
ll addTo[200005]; //d + addTo[x] = trueDist;
ll addN[200005]; //ns + addN[x] = trueNs;

void dfs(ll x, ll p, ll k) {
	dists[x][0] = 0;
	minK[x] = 1e9;
	addTo[x] = 0;
	addN[x] = 0;
	if (k == 0) minK[x] = 0;	
	for (edge e : tree[x]) {
		ll v = e.b;
		ll c = e.w;
		if (v == p) continue;
		dfs(v, x, k);
		minK[x] = min(minK[x], minK[v]);
		if (dists[v].size() <= dists[x].size()) {
			for (auto itr = dists[v].begin(); itr != dists[v].end(); ++itr) {
				ll d = itr->first + addTo[v];
				ll ns = itr->second + addN[v];
				if (dists[x].find(k - d - c - addTo[x]) != dists[x].end()) minK[x] = min(minK[x], ns + 1 + dists[x][k - d -c - addTo[x]] + addN[x]);
			}
			for (auto itr = dists[v].begin(); itr != dists[v].end(); ++itr) {
				ll d = itr->first + addTo[v];
				ll ns = itr->second + addN[v];
				if (dists[x].find(d + c - addTo[x]) != dists[x].end()) dists[x][d + c - addTo[x]] = min(dists[x][d + c - addTo[x]], ns + 1 - addN[x]);
				else dists[x][d + c - addTo[x]] = ns + 1 - addN[x];
			}
		}
		else {
			dists[x].swap(dists[v]);
			for (auto itr = dists[v].begin(); itr != dists[v].end(); ++itr) {
				ll d = itr->first + addTo[x];
				ll ns = itr->second + addN[x];
				if (dists[x].find(k - d - c - addTo[v]) != dists[x].end()) minK[x] = min(minK[x], ns +1 + dists[x][k - d - c - addTo[v]] + addN[v]);
			}
			for (auto itr = dists[v].begin(); itr != dists[v].end(); ++itr) {
				ll d = itr->first + addTo[x] - addTo[v] - c;
				ll ns = itr->second + addN[x] - addN[v] - 1;
				if (dists[x].find(d) != dists[x].end()) dists[x][d] = min(dists[x][d], ns);
				else dists[x][d] = ns;
			}
			addTo[x] = addTo[v] + c;
			addN[x] = addN[v] + 1;
		}
	}
}

int best_path(int N, int K, int H[][2], int L[]) {
	for (ll i = 0; i < N - 1; ++i) {
		tree[H[i][0]].push_back(edge(H[i][1], L[i]));
		tree[H[i][1]].push_back(edge(H[i][0], L[i]));
	}
	dfs(0, 0, K);
	if (minK[0] != 1e9) return minK[0];
	return -1;
}

//ll main() {
//	ios::sync_with_stdio(0);
//	cin.tie(0);
//
//	ll H[10][2] = { {0,1}, {0,2}, {2,3}, {3,4}, {4,5}, {0,6}, {6,7}, {6,8}, {8,9}, {8,10} };
//	ll L[10] = { 3,4,5,4,6,3,2,5,6,7};
//	cout << best_path(11, 12, H, L) << endl;
//
//
//	return 0;
//}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 8 ms 14340 KB Output is correct
5 Correct 7 ms 14412 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 8 ms 14412 KB Output is correct
8 Correct 12 ms 14356 KB Output is correct
9 Correct 8 ms 14412 KB Output is correct
10 Correct 8 ms 14448 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 10 ms 14456 KB Output is correct
13 Correct 7 ms 14412 KB Output is correct
14 Correct 7 ms 14412 KB Output is correct
15 Correct 7 ms 14452 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
17 Correct 8 ms 14412 KB Output is correct
18 Correct 7 ms 14412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 8 ms 14340 KB Output is correct
5 Correct 7 ms 14412 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 8 ms 14412 KB Output is correct
8 Correct 12 ms 14356 KB Output is correct
9 Correct 8 ms 14412 KB Output is correct
10 Correct 8 ms 14448 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 10 ms 14456 KB Output is correct
13 Correct 7 ms 14412 KB Output is correct
14 Correct 7 ms 14412 KB Output is correct
15 Correct 7 ms 14452 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
17 Correct 8 ms 14412 KB Output is correct
18 Correct 7 ms 14412 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 7 ms 14412 KB Output is correct
21 Correct 8 ms 14668 KB Output is correct
22 Correct 10 ms 14644 KB Output is correct
23 Correct 13 ms 14772 KB Output is correct
24 Correct 9 ms 14656 KB Output is correct
25 Correct 11 ms 14668 KB Output is correct
26 Correct 8 ms 14668 KB Output is correct
27 Correct 8 ms 14528 KB Output is correct
28 Correct 8 ms 14656 KB Output is correct
29 Correct 8 ms 14668 KB Output is correct
30 Correct 9 ms 14660 KB Output is correct
31 Correct 9 ms 14668 KB Output is correct
32 Correct 8 ms 14656 KB Output is correct
33 Correct 9 ms 14792 KB Output is correct
34 Correct 8 ms 14668 KB Output is correct
35 Correct 8 ms 14652 KB Output is correct
36 Correct 8 ms 14668 KB Output is correct
37 Correct 8 ms 14668 KB Output is correct
38 Correct 9 ms 14708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 8 ms 14340 KB Output is correct
5 Correct 7 ms 14412 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 8 ms 14412 KB Output is correct
8 Correct 12 ms 14356 KB Output is correct
9 Correct 8 ms 14412 KB Output is correct
10 Correct 8 ms 14448 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 10 ms 14456 KB Output is correct
13 Correct 7 ms 14412 KB Output is correct
14 Correct 7 ms 14412 KB Output is correct
15 Correct 7 ms 14452 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
17 Correct 8 ms 14412 KB Output is correct
18 Correct 7 ms 14412 KB Output is correct
19 Correct 144 ms 38772 KB Output is correct
20 Correct 130 ms 38632 KB Output is correct
21 Correct 143 ms 38240 KB Output is correct
22 Correct 134 ms 37808 KB Output is correct
23 Correct 187 ms 50828 KB Output is correct
24 Correct 160 ms 40716 KB Output is correct
25 Correct 113 ms 39440 KB Output is correct
26 Correct 61 ms 47660 KB Output is correct
27 Correct 238 ms 51200 KB Output is correct
28 Correct 306 ms 93320 KB Output is correct
29 Correct 322 ms 91464 KB Output is correct
30 Correct 209 ms 51272 KB Output is correct
31 Correct 198 ms 51176 KB Output is correct
32 Correct 286 ms 51284 KB Output is correct
33 Correct 292 ms 56180 KB Output is correct
34 Correct 345 ms 88040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14412 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 8 ms 14412 KB Output is correct
4 Correct 8 ms 14340 KB Output is correct
5 Correct 7 ms 14412 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 8 ms 14412 KB Output is correct
8 Correct 12 ms 14356 KB Output is correct
9 Correct 8 ms 14412 KB Output is correct
10 Correct 8 ms 14448 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 10 ms 14456 KB Output is correct
13 Correct 7 ms 14412 KB Output is correct
14 Correct 7 ms 14412 KB Output is correct
15 Correct 7 ms 14452 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
17 Correct 8 ms 14412 KB Output is correct
18 Correct 7 ms 14412 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 7 ms 14412 KB Output is correct
21 Correct 8 ms 14668 KB Output is correct
22 Correct 10 ms 14644 KB Output is correct
23 Correct 13 ms 14772 KB Output is correct
24 Correct 9 ms 14656 KB Output is correct
25 Correct 11 ms 14668 KB Output is correct
26 Correct 8 ms 14668 KB Output is correct
27 Correct 8 ms 14528 KB Output is correct
28 Correct 8 ms 14656 KB Output is correct
29 Correct 8 ms 14668 KB Output is correct
30 Correct 9 ms 14660 KB Output is correct
31 Correct 9 ms 14668 KB Output is correct
32 Correct 8 ms 14656 KB Output is correct
33 Correct 9 ms 14792 KB Output is correct
34 Correct 8 ms 14668 KB Output is correct
35 Correct 8 ms 14652 KB Output is correct
36 Correct 8 ms 14668 KB Output is correct
37 Correct 8 ms 14668 KB Output is correct
38 Correct 9 ms 14708 KB Output is correct
39 Correct 144 ms 38772 KB Output is correct
40 Correct 130 ms 38632 KB Output is correct
41 Correct 143 ms 38240 KB Output is correct
42 Correct 134 ms 37808 KB Output is correct
43 Correct 187 ms 50828 KB Output is correct
44 Correct 160 ms 40716 KB Output is correct
45 Correct 113 ms 39440 KB Output is correct
46 Correct 61 ms 47660 KB Output is correct
47 Correct 238 ms 51200 KB Output is correct
48 Correct 306 ms 93320 KB Output is correct
49 Correct 322 ms 91464 KB Output is correct
50 Correct 209 ms 51272 KB Output is correct
51 Correct 198 ms 51176 KB Output is correct
52 Correct 286 ms 51284 KB Output is correct
53 Correct 292 ms 56180 KB Output is correct
54 Correct 345 ms 88040 KB Output is correct
55 Correct 25 ms 17848 KB Output is correct
56 Correct 16 ms 16572 KB Output is correct
57 Correct 82 ms 37480 KB Output is correct
58 Correct 54 ms 29888 KB Output is correct
59 Correct 110 ms 55272 KB Output is correct
60 Correct 301 ms 94936 KB Output is correct
61 Correct 269 ms 57696 KB Output is correct
62 Correct 202 ms 54320 KB Output is correct
63 Correct 284 ms 54304 KB Output is correct
64 Correct 498 ms 109136 KB Output is correct
65 Correct 485 ms 107228 KB Output is correct
66 Correct 345 ms 90552 KB Output is correct
67 Correct 194 ms 47048 KB Output is correct
68 Correct 401 ms 70484 KB Output is correct
69 Correct 414 ms 75328 KB Output is correct
70 Correct 350 ms 68016 KB Output is correct