답안 #851419

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
851419 2023-09-19T17:59:51 Z mat_jur 경주 (Race) (IOI11_race) C++17
100 / 100
493 ms 86880 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#ifdef DEBUG
auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;}
auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";}
#define debug(X) cerr << "["#X"]: " << X << '\n';
#else 
#define debug(X) ;
#endif
#define ll long long
#define all(v) (v).begin(), (v).end()
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define ROF(i,r,l) for(int i=(r);i>=(l);--i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back

int best_path(int n, int k, int h[][2], int l[]) {
	vector<vector<pair<int, int>>> G(n);
	REP(i, n-1) {
		G[h[i][0]].eb(mp(h[i][1], l[i]));
		G[h[i][1]].eb(mp(h[i][0], l[i]));
	}
	const int inf = 1e9;
	int res = inf;
	vector<pair<pair<int, int>, map<int, int>>> M(n);
	auto merge = [&](pair<pair<int, int>, map<int, int>> &a, pair<pair<int, int>, map<int, int>> &b) {
		if (ssize(a.se) < ssize(b.se)) swap(a, b);
		for (auto x : b.se) {
			int key = x.fi+b.fi.fi;
			if(a.se.find((k-key)-a.fi.fi) != a.se.end()) res = min(res, a.se[(k-key)-a.fi.fi]+a.fi.se+x.se+b.fi.se);
		}
		for (auto x : b.se) {
			int key = x.fi+b.fi.fi;
			if (a.se.find(key-a.fi.fi) == a.se.end()) a.se[key-a.fi.fi] = inf;
			a.se[key-a.fi.fi] = min(a.se[key-a.fi.fi], x.se+b.fi.se-a.fi.se);
		}
		return;
	};
	function<void(int, int)> dfs = [&](int v, int p) {
		M[v].fi.fi = M[v].fi.se = 0;
		M[v].se[0] = 0;
		for (auto e : G[v]) {
			int u = e.fi, w = e.se;
			if (u == p) continue;
			dfs(u, v);
			M[u].fi.fi += w;
			M[u].fi.se++;
			merge(M[v], M[u]);
		}
	};
	dfs(0, -1);
	if (res == inf) return -1;
	return res;
}

#ifdef LOCAL
int main () {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n, k;
	cin >> n >> k;
	int h[n-1][2];
	int l[n-1];
	REP(i, n-1) {
		cin >> h[i][0] >> h[i][1];
	}	
	REP(i, n-1) {
		cin >> l[i];
	}
	cout << best_path(n, k, h, l) << '\n';
	return 0;
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2392 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2392 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 2 ms 2648 KB Output is correct
22 Correct 2 ms 2648 KB Output is correct
23 Correct 2 ms 2648 KB Output is correct
24 Correct 2 ms 2648 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 2 ms 2648 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 2 ms 2652 KB Output is correct
29 Correct 2 ms 2652 KB Output is correct
30 Correct 2 ms 2652 KB Output is correct
31 Correct 2 ms 2652 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2392 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 110 ms 27736 KB Output is correct
20 Correct 105 ms 27728 KB Output is correct
21 Correct 110 ms 27476 KB Output is correct
22 Correct 104 ms 27248 KB Output is correct
23 Correct 147 ms 36804 KB Output is correct
24 Correct 113 ms 29788 KB Output is correct
25 Correct 77 ms 29784 KB Output is correct
26 Correct 53 ms 38724 KB Output is correct
27 Correct 154 ms 45052 KB Output is correct
28 Correct 275 ms 82816 KB Output is correct
29 Correct 263 ms 80952 KB Output is correct
30 Correct 145 ms 44884 KB Output is correct
31 Correct 146 ms 45016 KB Output is correct
32 Correct 194 ms 45136 KB Output is correct
33 Correct 199 ms 48468 KB Output is correct
34 Correct 347 ms 73336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2392 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 2 ms 2648 KB Output is correct
22 Correct 2 ms 2648 KB Output is correct
23 Correct 2 ms 2648 KB Output is correct
24 Correct 2 ms 2648 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 2 ms 2648 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 2 ms 2652 KB Output is correct
29 Correct 2 ms 2652 KB Output is correct
30 Correct 2 ms 2652 KB Output is correct
31 Correct 2 ms 2652 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
39 Correct 110 ms 27736 KB Output is correct
40 Correct 105 ms 27728 KB Output is correct
41 Correct 110 ms 27476 KB Output is correct
42 Correct 104 ms 27248 KB Output is correct
43 Correct 147 ms 36804 KB Output is correct
44 Correct 113 ms 29788 KB Output is correct
45 Correct 77 ms 29784 KB Output is correct
46 Correct 53 ms 38724 KB Output is correct
47 Correct 154 ms 45052 KB Output is correct
48 Correct 275 ms 82816 KB Output is correct
49 Correct 263 ms 80952 KB Output is correct
50 Correct 145 ms 44884 KB Output is correct
51 Correct 146 ms 45016 KB Output is correct
52 Correct 194 ms 45136 KB Output is correct
53 Correct 199 ms 48468 KB Output is correct
54 Correct 347 ms 73336 KB Output is correct
55 Correct 16 ms 5772 KB Output is correct
56 Correct 7 ms 4652 KB Output is correct
57 Correct 58 ms 27224 KB Output is correct
58 Correct 42 ms 22024 KB Output is correct
59 Correct 97 ms 43452 KB Output is correct
60 Correct 261 ms 81220 KB Output is correct
61 Correct 183 ms 47544 KB Output is correct
62 Correct 146 ms 45040 KB Output is correct
63 Correct 219 ms 45012 KB Output is correct
64 Correct 493 ms 86880 KB Output is correct
65 Correct 469 ms 85328 KB Output is correct
66 Correct 306 ms 76936 KB Output is correct
67 Correct 135 ms 41156 KB Output is correct
68 Correct 317 ms 57668 KB Output is correct
69 Correct 331 ms 61844 KB Output is correct
70 Correct 310 ms 55640 KB Output is correct