Submission #1111660

# Submission time Handle Problem Language Result Execution time Memory
1111660 2024-11-12T13:43:18 Z vako_p Parkovi (COCI22_parkovi) C++14
110 / 110
631 ms 110408 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back

const int mxN = 1e6 + 5;
ll n,k,dis[mxN],dis1[mxN],d[mxN],cnt[mxN],mid;
vector<pair<ll,ll>> v[mxN];
vector<pair<ll,ll>> vv[mxN];
bool vis[mxN];

void dfs(ll at, ll par){
	for(auto it : v[at]){
		if(it.first == par) continue;
		dfs(it.first, at);
		dis[at] = max(dis[at], dis[it.first] + it.second);
	}
}

void dfs1(ll at, ll par){
	if(dis[at] <= mid){
		dis1[at] = dis[at];
		d[at] = mid;
		cnt[at] = 1;
		vis[at] = true;
		for(auto it : v[at]){
			if(it.first == par) continue;
		 	vis[it.first] = false;
		}
//		cout << "dis[at] <= mid" << at << ' ' << cnt[at] << ' ' << ' ' << d[at] << ' ' << dis1[at] << '\n';
		return;
	}
	vv[at].clear();
	ll c = 1,dd = 0,dd1 = 0,dd2 = 0;
	bool ok = true; 
	for(auto it : v[at]){
		if(it.first == par) continue;
		dfs1(it.first, at);
	 	d[it.first] -= it.second;
	 	if(d[it.first] >= 0) ok = false;
	 	dis1[it.first] += it.second;
		c += cnt[it.first] - (dis1[it.first] <= mid);
		dd2 = max(dd2, dis1[it.first]);
		if(dis1[it.first] <= mid){
			vv[at].pb({dis1[it.first], it.first});
			dd1 = max(dd1, dis1[it.first]);
			vis[it.first] = false;
		}
		else dd = max(dd, d[it.first]);
	}
	if(ok){
		cnt[at] = c;
		d[at] = mid;
		dis1[at] = dd1;
		vis[at] = true;
//		cout << "did not reach node" << at << ' ' << cnt[at] << ' ' << ' ' << d[at] << ' ' << dis1[at] << '\n';
		return;
	}
	sort(vv[at].begin(), vv[at].end()); 
	for(auto it : vv[at]){
		if(dd < it.first){
			cnt[at] = c;
			d[at] = mid;
			dis1[at] = dd1;
			vis[at] = true;
//			cout << "did not work without node" << at << ' ' << cnt[at] << ' ' << ' ' << d[at] << ' ' << dis1[at] << '\n';
			return;
		}
	}
	cnt[at] = c - 1;
	d[at] = dd;
	dis1[at] = dd2;
//	cout << "worked without node" << at << ' ' << cnt[at] << ' ' << ' ' << d[at] << ' ' << dis1[at] << '\n';
} 

bool ok(){
	for(int i = 0; i <= n; i++){
		vis[i] = false;
		dis1[i] = d[i] = cnt[i] = 0;
	}
	dfs1(1, 1);
	return (cnt[1] <= k);
}

int main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> k;
	for(int i = 0; i < n - 1; i++){
		ll a,b,w;
		cin >> a >> b >> w;
		v[a].pb({b, w});
		v[b].pb({a, w});
	}
	dfs(1, 1);
	ll l = -1, r = 1e16;
	while(r > l + 1){
		mid = l + (r - l) / 2;
		if(ok()) r = mid;
		else l = mid;
	}
//	r = 1135814621;
	cout << r << '\n';
	mid = r;
	ok();
	ll cc = k;
	for(int i = 1; i <= n; i++){
		if(vis[i]){
			cout << i << ' ';
			cc--;
			assert(cc >= 0);
		}
	}
	for(int i = 1; i <= n and cc > 0; i++){
		if(!vis[i]){
			cout << i << ' ';
			cc--;
		}
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 53584 KB Output is correct
2 Correct 10 ms 53936 KB Output is correct
3 Correct 9 ms 53584 KB Output is correct
4 Correct 9 ms 53584 KB Output is correct
5 Correct 10 ms 53840 KB Output is correct
6 Correct 10 ms 53584 KB Output is correct
7 Correct 11 ms 53584 KB Output is correct
8 Correct 10 ms 53704 KB Output is correct
9 Correct 9 ms 53584 KB Output is correct
10 Correct 9 ms 53584 KB Output is correct
11 Correct 9 ms 53584 KB Output is correct
12 Correct 12 ms 49744 KB Output is correct
13 Correct 11 ms 49744 KB Output is correct
14 Correct 10 ms 53584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 90440 KB Output is correct
2 Correct 288 ms 84352 KB Output is correct
3 Correct 457 ms 68524 KB Output is correct
4 Correct 94 ms 63816 KB Output is correct
5 Correct 115 ms 70604 KB Output is correct
6 Correct 97 ms 69960 KB Output is correct
7 Correct 99 ms 66892 KB Output is correct
8 Correct 99 ms 67252 KB Output is correct
9 Correct 103 ms 67400 KB Output is correct
10 Correct 103 ms 67920 KB Output is correct
11 Correct 123 ms 69560 KB Output is correct
12 Correct 169 ms 69972 KB Output is correct
13 Correct 183 ms 76624 KB Output is correct
14 Correct 111 ms 67712 KB Output is correct
15 Correct 96 ms 68168 KB Output is correct
16 Correct 135 ms 72008 KB Output is correct
17 Correct 96 ms 68212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 560 ms 104532 KB Output is correct
2 Correct 530 ms 100936 KB Output is correct
3 Correct 369 ms 95828 KB Output is correct
4 Correct 203 ms 80972 KB Output is correct
5 Correct 539 ms 109044 KB Output is correct
6 Correct 535 ms 102216 KB Output is correct
7 Correct 622 ms 104008 KB Output is correct
8 Correct 631 ms 102672 KB Output is correct
9 Correct 554 ms 102180 KB Output is correct
10 Correct 416 ms 96332 KB Output is correct
11 Correct 213 ms 85240 KB Output is correct
12 Correct 585 ms 104264 KB Output is correct
13 Correct 630 ms 104656 KB Output is correct
14 Correct 581 ms 106868 KB Output is correct
15 Correct 295 ms 106000 KB Output is correct
16 Correct 298 ms 104972 KB Output is correct
17 Correct 231 ms 96536 KB Output is correct
18 Correct 163 ms 91464 KB Output is correct
19 Correct 308 ms 100680 KB Output is correct
20 Correct 309 ms 102376 KB Output is correct
21 Correct 284 ms 103760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 53584 KB Output is correct
2 Correct 10 ms 53936 KB Output is correct
3 Correct 9 ms 53584 KB Output is correct
4 Correct 9 ms 53584 KB Output is correct
5 Correct 10 ms 53840 KB Output is correct
6 Correct 10 ms 53584 KB Output is correct
7 Correct 11 ms 53584 KB Output is correct
8 Correct 10 ms 53704 KB Output is correct
9 Correct 9 ms 53584 KB Output is correct
10 Correct 9 ms 53584 KB Output is correct
11 Correct 9 ms 53584 KB Output is correct
12 Correct 12 ms 49744 KB Output is correct
13 Correct 11 ms 49744 KB Output is correct
14 Correct 10 ms 53584 KB Output is correct
15 Correct 161 ms 90440 KB Output is correct
16 Correct 288 ms 84352 KB Output is correct
17 Correct 457 ms 68524 KB Output is correct
18 Correct 94 ms 63816 KB Output is correct
19 Correct 115 ms 70604 KB Output is correct
20 Correct 97 ms 69960 KB Output is correct
21 Correct 99 ms 66892 KB Output is correct
22 Correct 99 ms 67252 KB Output is correct
23 Correct 103 ms 67400 KB Output is correct
24 Correct 103 ms 67920 KB Output is correct
25 Correct 123 ms 69560 KB Output is correct
26 Correct 169 ms 69972 KB Output is correct
27 Correct 183 ms 76624 KB Output is correct
28 Correct 111 ms 67712 KB Output is correct
29 Correct 96 ms 68168 KB Output is correct
30 Correct 135 ms 72008 KB Output is correct
31 Correct 96 ms 68212 KB Output is correct
32 Correct 560 ms 104532 KB Output is correct
33 Correct 530 ms 100936 KB Output is correct
34 Correct 369 ms 95828 KB Output is correct
35 Correct 203 ms 80972 KB Output is correct
36 Correct 539 ms 109044 KB Output is correct
37 Correct 535 ms 102216 KB Output is correct
38 Correct 622 ms 104008 KB Output is correct
39 Correct 631 ms 102672 KB Output is correct
40 Correct 554 ms 102180 KB Output is correct
41 Correct 416 ms 96332 KB Output is correct
42 Correct 213 ms 85240 KB Output is correct
43 Correct 585 ms 104264 KB Output is correct
44 Correct 630 ms 104656 KB Output is correct
45 Correct 581 ms 106868 KB Output is correct
46 Correct 295 ms 106000 KB Output is correct
47 Correct 298 ms 104972 KB Output is correct
48 Correct 231 ms 96536 KB Output is correct
49 Correct 163 ms 91464 KB Output is correct
50 Correct 308 ms 100680 KB Output is correct
51 Correct 309 ms 102376 KB Output is correct
52 Correct 284 ms 103760 KB Output is correct
53 Correct 93 ms 70208 KB Output is correct
54 Correct 104 ms 68384 KB Output is correct
55 Correct 99 ms 69192 KB Output is correct
56 Correct 205 ms 69800 KB Output is correct
57 Correct 463 ms 71440 KB Output is correct
58 Correct 87 ms 67912 KB Output is correct
59 Correct 443 ms 72512 KB Output is correct
60 Correct 79 ms 67912 KB Output is correct
61 Correct 81 ms 66120 KB Output is correct
62 Correct 168 ms 68936 KB Output is correct
63 Correct 81 ms 67796 KB Output is correct
64 Correct 155 ms 70252 KB Output is correct
65 Correct 104 ms 68148 KB Output is correct
66 Correct 435 ms 70496 KB Output is correct
67 Correct 88 ms 66760 KB Output is correct
68 Correct 504 ms 72264 KB Output is correct
69 Correct 513 ms 105544 KB Output is correct
70 Correct 531 ms 102100 KB Output is correct
71 Correct 607 ms 110408 KB Output is correct
72 Correct 521 ms 71852 KB Output is correct
73 Correct 150 ms 71332 KB Output is correct
74 Correct 160 ms 71348 KB Output is correct
75 Correct 241 ms 73276 KB Output is correct
76 Correct 167 ms 72776 KB Output is correct
77 Correct 271 ms 72388 KB Output is correct
78 Correct 229 ms 74304 KB Output is correct