답안 #930455

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
930455 2024-02-19T19:46:51 Z NK_ Parkovi (COCI22_parkovi) C++17
110 / 110
921 ms 47116 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) int(x.size())

using ll = long long;
template<class T> using V = vector<T>;
using pl = pair<ll, ll>;
using vi = V<int>;
using vpl = V<pl>;
using vl = V<ll>;

const ll INFL = 1e18;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N, K; cin >> N >> K;

	V<vpl> adj(N); vi d(N); for(int i = 0; i < N - 1; i++) {
		int u, v, w; cin >> u >> v >> w; --u, --v;
		adj[u].pb(mp(v, w));
		adj[v].pb(mp(u, w));
	}

	vi ans; ll k = -1;
	function<pl(int, int, ll)> dfs = [&](int u, int p, ll d) {
		ll close = -INFL, far = 0;
		for(auto& e : adj[u]) {
			int v; ll w; tie(v, w) = e; if (v == p) continue;
			pl R = dfs(v, u, w);
			if (R.f) close = max(close, R.s);
			else far = max(far, R.s);
		}

		pl R;
		if (close >= far) R = mp(1, close - d);
		else R = mp(0, far + d);
		
		if (R.f && R.s < 0) R = mp(0, 0);
		if (!R.f && R.s > k) {
			ans.pb(u); R = mp(1, k - d);
		}

		return R;
	};

	ll lo = 0, hi = ll(3e15);
	while(lo < hi) {
		ll mid = (lo + hi) / 2;
		ans = {}; k = mid;
		dfs(0, -1, INFL);
		if (sz(ans) <= K) hi = mid;
		else lo = mid + 1;
	}

	ans = {}; k = lo;
	dfs(0, -1, INFL);

	set<int> in(begin(ans), end(ans));

	int cur = 0;
	while(sz(ans) < K) {
		if (!in.count(cur)) ans.pb(cur);
		cur++;
	}

	cout << lo << nl;
	for(auto& x : ans) cout << x + 1 << " ";
	cout << nl;

	exit(0-0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 338 ms 34944 KB Output is correct
2 Correct 321 ms 35880 KB Output is correct
3 Correct 170 ms 15684 KB Output is correct
4 Correct 627 ms 15184 KB Output is correct
5 Correct 530 ms 14728 KB Output is correct
6 Correct 586 ms 14728 KB Output is correct
7 Correct 482 ms 14740 KB Output is correct
8 Correct 607 ms 15612 KB Output is correct
9 Correct 568 ms 15216 KB Output is correct
10 Correct 526 ms 15568 KB Output is correct
11 Correct 487 ms 17048 KB Output is correct
12 Correct 514 ms 17236 KB Output is correct
13 Correct 568 ms 18652 KB Output is correct
14 Correct 407 ms 16980 KB Output is correct
15 Correct 417 ms 16432 KB Output is correct
16 Correct 418 ms 17232 KB Output is correct
17 Correct 452 ms 16260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 318 ms 36692 KB Output is correct
2 Correct 294 ms 35516 KB Output is correct
3 Correct 279 ms 33620 KB Output is correct
4 Correct 270 ms 33636 KB Output is correct
5 Correct 346 ms 45560 KB Output is correct
6 Correct 347 ms 40660 KB Output is correct
7 Correct 379 ms 42648 KB Output is correct
8 Correct 387 ms 36688 KB Output is correct
9 Correct 344 ms 36504 KB Output is correct
10 Correct 281 ms 35664 KB Output is correct
11 Correct 285 ms 34384 KB Output is correct
12 Correct 394 ms 47116 KB Output is correct
13 Correct 367 ms 44776 KB Output is correct
14 Correct 384 ms 41168 KB Output is correct
15 Correct 312 ms 35668 KB Output is correct
16 Correct 324 ms 34208 KB Output is correct
17 Correct 273 ms 33876 KB Output is correct
18 Correct 358 ms 36172 KB Output is correct
19 Correct 354 ms 43720 KB Output is correct
20 Correct 337 ms 42708 KB Output is correct
21 Correct 342 ms 40452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 338 ms 34944 KB Output is correct
16 Correct 321 ms 35880 KB Output is correct
17 Correct 170 ms 15684 KB Output is correct
18 Correct 627 ms 15184 KB Output is correct
19 Correct 530 ms 14728 KB Output is correct
20 Correct 586 ms 14728 KB Output is correct
21 Correct 482 ms 14740 KB Output is correct
22 Correct 607 ms 15612 KB Output is correct
23 Correct 568 ms 15216 KB Output is correct
24 Correct 526 ms 15568 KB Output is correct
25 Correct 487 ms 17048 KB Output is correct
26 Correct 514 ms 17236 KB Output is correct
27 Correct 568 ms 18652 KB Output is correct
28 Correct 407 ms 16980 KB Output is correct
29 Correct 417 ms 16432 KB Output is correct
30 Correct 418 ms 17232 KB Output is correct
31 Correct 452 ms 16260 KB Output is correct
32 Correct 318 ms 36692 KB Output is correct
33 Correct 294 ms 35516 KB Output is correct
34 Correct 279 ms 33620 KB Output is correct
35 Correct 270 ms 33636 KB Output is correct
36 Correct 346 ms 45560 KB Output is correct
37 Correct 347 ms 40660 KB Output is correct
38 Correct 379 ms 42648 KB Output is correct
39 Correct 387 ms 36688 KB Output is correct
40 Correct 344 ms 36504 KB Output is correct
41 Correct 281 ms 35664 KB Output is correct
42 Correct 285 ms 34384 KB Output is correct
43 Correct 394 ms 47116 KB Output is correct
44 Correct 367 ms 44776 KB Output is correct
45 Correct 384 ms 41168 KB Output is correct
46 Correct 312 ms 35668 KB Output is correct
47 Correct 324 ms 34208 KB Output is correct
48 Correct 273 ms 33876 KB Output is correct
49 Correct 358 ms 36172 KB Output is correct
50 Correct 354 ms 43720 KB Output is correct
51 Correct 337 ms 42708 KB Output is correct
52 Correct 342 ms 40452 KB Output is correct
53 Correct 435 ms 15056 KB Output is correct
54 Correct 743 ms 15652 KB Output is correct
55 Correct 588 ms 15952 KB Output is correct
56 Correct 609 ms 16412 KB Output is correct
57 Correct 921 ms 19924 KB Output is correct
58 Correct 620 ms 15192 KB Output is correct
59 Correct 717 ms 26820 KB Output is correct
60 Correct 581 ms 16108 KB Output is correct
61 Correct 494 ms 14700 KB Output is correct
62 Correct 588 ms 19920 KB Output is correct
63 Correct 573 ms 16168 KB Output is correct
64 Correct 746 ms 25268 KB Output is correct
65 Correct 582 ms 16004 KB Output is correct
66 Correct 670 ms 20100 KB Output is correct
67 Correct 598 ms 14808 KB Output is correct
68 Correct 791 ms 26992 KB Output is correct
69 Correct 284 ms 35672 KB Output is correct
70 Correct 261 ms 33956 KB Output is correct
71 Correct 358 ms 41812 KB Output is correct
72 Correct 150 ms 15548 KB Output is correct
73 Correct 143 ms 16148 KB Output is correct
74 Correct 171 ms 20668 KB Output is correct
75 Correct 530 ms 17600 KB Output is correct
76 Correct 512 ms 18296 KB Output is correct
77 Correct 536 ms 17556 KB Output is correct
78 Correct 500 ms 19512 KB Output is correct