답안 #542337

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
542337 2022-03-26T08:48:10 Z inksamurai Parkovi (COCI22_parkovi) C++17
0 / 110
264 ms 524288 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=1;
		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 Runtime error 264 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 262 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 7 ms 9812 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 264 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -