Submission #1071142

# Submission time Handle Problem Language Result Execution time Memory
1071142 2024-08-23T05:14:28 Z qqspeed20015 Parkovi (COCI22_parkovi) C++14
110 / 110
434 ms 35592 KB
#include <bits/stdc++.h>
using namespace std;
 
const int N = 2e5 + 3;
const long long INF = 1e18;
 
int n, k;
vector<pair <int, int>> adj[N];
 
int num_park = 0;
bool is_park[N];
long long near[N], reach[N];
 
void input() {
	cin >> n >> k;
 
	for(int i = 1; i < n; i++) {
		int u, v, w; cin >> u >> v >> w;
		adj[u].push_back(make_pair(v, w));
		adj[v].push_back(make_pair(u, w));
	}
}
 
void dfs(int u, int p, long long limit) {
	near[u] = INF; reach[u] = 0;
 
	for (int i = 0; i < (int) adj[u].size(); i++) {
		int v = adj[u][i].first, w = adj[u][i].second;
 
		if (v != p) {
			dfs(v, u, limit);
 
			if (reach[v] + min(1LL * w, near[v]) <= limit) {
				near[u] = min(near[u], near[v] + w);
				if(reach[v] + near[v] > limit) reach[u] = max(reach[u], reach[v] + w);
			} else {
				num_park++;
				is_park[v] = true;
				near[u] = min(near[u], 1LL * w);
			}
		}	
	}
}
 
bool check(long long limit) {
	num_park = 0;
	for(int i = 1; i <= n; i++) is_park[i] = false;
 
	dfs(1, -1, limit);
 
	if (reach[1] + near[1] > limit) {
		num_park++;
		is_park[1] = true;
	}
 
	return (num_park <= k);
}
 
void process() {
	long long l = 0, r = 1e15, res = 0;
	while (l <= r) {
		long long m = (l + r) >> 1;
		if (check(m)) {
			res = m;
			r = m - 1;
		} else {
			l = m + 1;
		}
	}
 
	cout << res << endl;

	check(res);
	vector<int> nodes;
	for(int i = 1; i <= n; i++) if(is_park[i]) nodes.push_back(i);
	for(int i = 1; i <= n; i++) if((int)nodes.size() < k && !is_park[i]) nodes.push_back(i);
	for(int node : nodes) cout << node << " ";
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    input();
    process();
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8280 KB Output is correct
2 Correct 3 ms 8280 KB Output is correct
3 Correct 2 ms 8284 KB Output is correct
4 Correct 2 ms 8284 KB Output is correct
5 Correct 1 ms 8284 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 2 ms 8284 KB Output is correct
8 Correct 2 ms 8284 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 2 ms 8284 KB Output is correct
11 Correct 2 ms 8284 KB Output is correct
12 Correct 2 ms 8284 KB Output is correct
13 Correct 1 ms 8284 KB Output is correct
14 Correct 2 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 204 ms 31628 KB Output is correct
2 Correct 229 ms 33632 KB Output is correct
3 Correct 160 ms 20688 KB Output is correct
4 Correct 387 ms 19432 KB Output is correct
5 Correct 416 ms 19280 KB Output is correct
6 Correct 419 ms 19280 KB Output is correct
7 Correct 362 ms 18000 KB Output is correct
8 Correct 390 ms 18428 KB Output is correct
9 Correct 388 ms 18768 KB Output is correct
10 Correct 393 ms 19264 KB Output is correct
11 Correct 353 ms 20560 KB Output is correct
12 Correct 344 ms 20748 KB Output is correct
13 Correct 381 ms 21840 KB Output is correct
14 Correct 349 ms 19256 KB Output is correct
15 Correct 328 ms 18780 KB Output is correct
16 Correct 345 ms 20308 KB Output is correct
17 Correct 333 ms 19680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 203 ms 34164 KB Output is correct
2 Correct 177 ms 33364 KB Output is correct
3 Correct 201 ms 32080 KB Output is correct
4 Correct 173 ms 32084 KB Output is correct
5 Correct 204 ms 35108 KB Output is correct
6 Correct 214 ms 34252 KB Output is correct
7 Correct 252 ms 35592 KB Output is correct
8 Correct 197 ms 33620 KB Output is correct
9 Correct 204 ms 33364 KB Output is correct
10 Correct 199 ms 32960 KB Output is correct
11 Correct 186 ms 32084 KB Output is correct
12 Correct 230 ms 35448 KB Output is correct
13 Correct 226 ms 35272 KB Output is correct
14 Correct 229 ms 34404 KB Output is correct
15 Correct 202 ms 32084 KB Output is correct
16 Correct 212 ms 31328 KB Output is correct
17 Correct 165 ms 31236 KB Output is correct
18 Correct 204 ms 32420 KB Output is correct
19 Correct 194 ms 32904 KB Output is correct
20 Correct 215 ms 33476 KB Output is correct
21 Correct 221 ms 32840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8280 KB Output is correct
2 Correct 3 ms 8280 KB Output is correct
3 Correct 2 ms 8284 KB Output is correct
4 Correct 2 ms 8284 KB Output is correct
5 Correct 1 ms 8284 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 2 ms 8284 KB Output is correct
8 Correct 2 ms 8284 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 2 ms 8284 KB Output is correct
11 Correct 2 ms 8284 KB Output is correct
12 Correct 2 ms 8284 KB Output is correct
13 Correct 1 ms 8284 KB Output is correct
14 Correct 2 ms 8284 KB Output is correct
15 Correct 204 ms 31628 KB Output is correct
16 Correct 229 ms 33632 KB Output is correct
17 Correct 160 ms 20688 KB Output is correct
18 Correct 387 ms 19432 KB Output is correct
19 Correct 416 ms 19280 KB Output is correct
20 Correct 419 ms 19280 KB Output is correct
21 Correct 362 ms 18000 KB Output is correct
22 Correct 390 ms 18428 KB Output is correct
23 Correct 388 ms 18768 KB Output is correct
24 Correct 393 ms 19264 KB Output is correct
25 Correct 353 ms 20560 KB Output is correct
26 Correct 344 ms 20748 KB Output is correct
27 Correct 381 ms 21840 KB Output is correct
28 Correct 349 ms 19256 KB Output is correct
29 Correct 328 ms 18780 KB Output is correct
30 Correct 345 ms 20308 KB Output is correct
31 Correct 333 ms 19680 KB Output is correct
32 Correct 203 ms 34164 KB Output is correct
33 Correct 177 ms 33364 KB Output is correct
34 Correct 201 ms 32080 KB Output is correct
35 Correct 173 ms 32084 KB Output is correct
36 Correct 204 ms 35108 KB Output is correct
37 Correct 214 ms 34252 KB Output is correct
38 Correct 252 ms 35592 KB Output is correct
39 Correct 197 ms 33620 KB Output is correct
40 Correct 204 ms 33364 KB Output is correct
41 Correct 199 ms 32960 KB Output is correct
42 Correct 186 ms 32084 KB Output is correct
43 Correct 230 ms 35448 KB Output is correct
44 Correct 226 ms 35272 KB Output is correct
45 Correct 229 ms 34404 KB Output is correct
46 Correct 202 ms 32084 KB Output is correct
47 Correct 212 ms 31328 KB Output is correct
48 Correct 165 ms 31236 KB Output is correct
49 Correct 204 ms 32420 KB Output is correct
50 Correct 194 ms 32904 KB Output is correct
51 Correct 215 ms 33476 KB Output is correct
52 Correct 221 ms 32840 KB Output is correct
53 Correct 349 ms 19448 KB Output is correct
54 Correct 403 ms 19796 KB Output is correct
55 Correct 434 ms 20356 KB Output is correct
56 Correct 412 ms 20056 KB Output is correct
57 Correct 399 ms 20684 KB Output is correct
58 Correct 378 ms 19796 KB Output is correct
59 Correct 406 ms 22220 KB Output is correct
60 Correct 357 ms 18704 KB Output is correct
61 Correct 341 ms 17732 KB Output is correct
62 Correct 351 ms 18800 KB Output is correct
63 Correct 359 ms 18888 KB Output is correct
64 Correct 386 ms 20196 KB Output is correct
65 Correct 367 ms 19280 KB Output is correct
66 Correct 399 ms 19848 KB Output is correct
67 Correct 355 ms 18656 KB Output is correct
68 Correct 424 ms 21640 KB Output is correct
69 Correct 182 ms 33616 KB Output is correct
70 Correct 155 ms 31856 KB Output is correct
71 Correct 214 ms 34768 KB Output is correct
72 Correct 141 ms 20024 KB Output is correct
73 Correct 144 ms 19184 KB Output is correct
74 Correct 152 ms 20644 KB Output is correct
75 Correct 344 ms 21192 KB Output is correct
76 Correct 358 ms 20304 KB Output is correct
77 Correct 338 ms 20440 KB Output is correct
78 Correct 336 ms 20264 KB Output is correct