답안 #542339

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
542339 2022-03-26T08:48:32 Z inksamurai Parkovi (COCI22_parkovi) C++17
110 / 110
1887 ms 60884 KB
#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3ulnVOy ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<"\n";}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e

const int inf=1e15+11;
using vp=vec(pii);

signed main(){
_3ulnVOy;
	int n,k;
	cin>>n>>k;
	vec(vp) adj(n);
	rep(i,n-1){
		int u,v,w;
		cin>>u>>v>>w;
		u-=1,v-=1;
		adj[u].pb({v,w});
		adj[v].pb({u,w});
	}

	int cnt=0;
	vi pns;
	auto dfs=[&](auto self,int v,int par,int can)->pii{
		pii now={0,-inf}; 
		// distance to farthest node with no red node being near
		// distance to red node
		vec(pair<int,pii>) cands;
		for(auto e:adj[v]){
			auto [u,w]=e;
			if(u==par) continue;
			pii nep=self(self,u,v,can);
			if(nep.fi+w>can){
				pns.pb(u);
				now.se=max(now.se,can-w);
				cnt+=1;
			}else{
				now.se=max(now.se,nep.se-w);
				cands.pb({w,nep});
			}
		}
		for(auto tp:cands){
			int w=tp.fi;
			pii p=tp.se;
			now.fi=max(now.fi,p.fi+w);
		}
		if(now.fi<=now.se){
			now.fi=-inf;
		}
		return now;
	};

	auto quq=[&](int x)->bool{
		cnt=0;
		pns.clear();
		pii p=dfs(dfs,0,-1,x);
		if(p.fi!=-inf){
			cnt+=1;
			pns.pb(0);
		}
		return cnt<=k;
		// print("\n...",cnt);
	};

	// quq(3);
	int l=1,r=inf,c=-1;
	while(l<=r){
		int m=(l+r)/2;
		if(quq(m)){
			c=m;
			r=m-1;
		}else{
			l=m+1;
		}
	}

	quq(c);

	{
		vi usd(n,0);
		rep(i,sz(pns)){
			usd[pns[i]]=1;
		}
		rep(i,n){
			if(!usd[i] and sz(pns)<k){
				pns.pb(i);
			}
		}
	}

	print(c);
	for(auto v:pns){
		cout<<v+1<<" ";
	}
	print();
//
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 728 ms 53600 KB Output is correct
2 Correct 754 ms 58760 KB Output is correct
3 Correct 672 ms 30048 KB Output is correct
4 Correct 1744 ms 19896 KB Output is correct
5 Correct 1706 ms 19312 KB Output is correct
6 Correct 1702 ms 19404 KB Output is correct
7 Correct 1693 ms 18244 KB Output is correct
8 Correct 1841 ms 19044 KB Output is correct
9 Correct 1760 ms 19288 KB Output is correct
10 Correct 1855 ms 19712 KB Output is correct
11 Correct 1081 ms 23704 KB Output is correct
12 Correct 1037 ms 24160 KB Output is correct
13 Correct 1180 ms 26624 KB Output is correct
14 Correct 1048 ms 22688 KB Output is correct
15 Correct 1030 ms 21624 KB Output is correct
16 Correct 1061 ms 23836 KB Output is correct
17 Correct 1069 ms 22192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 770 ms 55912 KB Output is correct
2 Correct 734 ms 58272 KB Output is correct
3 Correct 680 ms 55268 KB Output is correct
4 Correct 674 ms 55148 KB Output is correct
5 Correct 635 ms 58724 KB Output is correct
6 Correct 699 ms 58768 KB Output is correct
7 Correct 719 ms 60884 KB Output is correct
8 Correct 738 ms 59700 KB Output is correct
9 Correct 723 ms 59036 KB Output is correct
10 Correct 704 ms 57824 KB Output is correct
11 Correct 706 ms 55728 KB Output is correct
12 Correct 694 ms 60132 KB Output is correct
13 Correct 719 ms 60796 KB Output is correct
14 Correct 740 ms 59620 KB Output is correct
15 Correct 704 ms 57164 KB Output is correct
16 Correct 707 ms 54812 KB Output is correct
17 Correct 701 ms 54548 KB Output is correct
18 Correct 714 ms 57564 KB Output is correct
19 Correct 652 ms 55768 KB Output is correct
20 Correct 702 ms 57676 KB Output is correct
21 Correct 725 ms 56432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 728 ms 53600 KB Output is correct
16 Correct 754 ms 58760 KB Output is correct
17 Correct 672 ms 30048 KB Output is correct
18 Correct 1744 ms 19896 KB Output is correct
19 Correct 1706 ms 19312 KB Output is correct
20 Correct 1702 ms 19404 KB Output is correct
21 Correct 1693 ms 18244 KB Output is correct
22 Correct 1841 ms 19044 KB Output is correct
23 Correct 1760 ms 19288 KB Output is correct
24 Correct 1855 ms 19712 KB Output is correct
25 Correct 1081 ms 23704 KB Output is correct
26 Correct 1037 ms 24160 KB Output is correct
27 Correct 1180 ms 26624 KB Output is correct
28 Correct 1048 ms 22688 KB Output is correct
29 Correct 1030 ms 21624 KB Output is correct
30 Correct 1061 ms 23836 KB Output is correct
31 Correct 1069 ms 22192 KB Output is correct
32 Correct 770 ms 55912 KB Output is correct
33 Correct 734 ms 58272 KB Output is correct
34 Correct 680 ms 55268 KB Output is correct
35 Correct 674 ms 55148 KB Output is correct
36 Correct 635 ms 58724 KB Output is correct
37 Correct 699 ms 58768 KB Output is correct
38 Correct 719 ms 60884 KB Output is correct
39 Correct 738 ms 59700 KB Output is correct
40 Correct 723 ms 59036 KB Output is correct
41 Correct 704 ms 57824 KB Output is correct
42 Correct 706 ms 55728 KB Output is correct
43 Correct 694 ms 60132 KB Output is correct
44 Correct 719 ms 60796 KB Output is correct
45 Correct 740 ms 59620 KB Output is correct
46 Correct 704 ms 57164 KB Output is correct
47 Correct 707 ms 54812 KB Output is correct
48 Correct 701 ms 54548 KB Output is correct
49 Correct 714 ms 57564 KB Output is correct
50 Correct 652 ms 55768 KB Output is correct
51 Correct 702 ms 57676 KB Output is correct
52 Correct 725 ms 56432 KB Output is correct
53 Correct 1646 ms 19872 KB Output is correct
54 Correct 1700 ms 20432 KB Output is correct
55 Correct 1887 ms 21224 KB Output is correct
56 Correct 1781 ms 20944 KB Output is correct
57 Correct 1717 ms 21484 KB Output is correct
58 Correct 1720 ms 20040 KB Output is correct
59 Correct 1475 ms 22492 KB Output is correct
60 Correct 1853 ms 19724 KB Output is correct
61 Correct 1546 ms 18228 KB Output is correct
62 Correct 1545 ms 19064 KB Output is correct
63 Correct 1846 ms 19816 KB Output is correct
64 Correct 1602 ms 20176 KB Output is correct
65 Correct 1740 ms 20004 KB Output is correct
66 Correct 1564 ms 20128 KB Output is correct
67 Correct 1596 ms 18812 KB Output is correct
68 Correct 1664 ms 22020 KB Output is correct
69 Correct 753 ms 58628 KB Output is correct
70 Correct 721 ms 55112 KB Output is correct
71 Correct 786 ms 60628 KB Output is correct
72 Correct 694 ms 28520 KB Output is correct
73 Correct 627 ms 28808 KB Output is correct
74 Correct 660 ms 28028 KB Output is correct
75 Correct 1139 ms 25032 KB Output is correct
76 Correct 1149 ms 24464 KB Output is correct
77 Correct 1166 ms 24076 KB Output is correct
78 Correct 1101 ms 24196 KB Output is correct