105262024-04-04 16:25:43111K-léptű ősökcpp17Accepted 100/1001.011s242204 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,K;
	cin>>N>>K;
	vector<vector<int>>g(N);
	for(int i=0;i<N-1;i++){
		int a,b;
		cin>>a>>b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	vector<int>ans(N,-1),s;
	auto dfs=[&](auto self,int i,int p)->void{
		if(s.size()>=K){
			ans[i]=s[s.size()-K];
		}
		s.push_back(i);
		for(int j:g[i]){
			if(j==p){
				continue;
			}
			self(self,j,i);
		}
		s.pop_back();
	};
	dfs(dfs,0,-1);
	for(int i=0;i<N;i++){
		cout<<ans[i]<<' ';
	}
	cout<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2108 KiB
subtask225/25
2Accepted3ms2308 KiB
3Accepted3ms2532 KiB
4Accepted3ms2700 KiB
5Accepted3ms2916 KiB
6Accepted3ms2960 KiB
7Accepted3ms3248 KiB
8Accepted3ms3284 KiB
9Accepted4ms3740 KiB
10Accepted4ms4048 KiB
11Accepted4ms4132 KiB
subtask316/16
12Accepted28ms11700 KiB
13Accepted52ms25128 KiB
14Accepted61ms25876 KiB
15Accepted48ms25548 KiB
16Accepted56ms27588 KiB
17Accepted57ms29368 KiB
18Accepted56ms30508 KiB
subtask459/59
19Accepted68ms34964 KiB
20Accepted97ms43708 KiB
21Accepted109ms41672 KiB
22Accepted126ms51356 KiB
23Accepted128ms55844 KiB
24Accepted141ms51600 KiB
25Accepted337ms92888 KiB
26Accepted333ms103048 KiB
27Accepted607ms187544 KiB
28Accepted625ms187036 KiB
29Accepted820ms211904 KiB
30Accepted808ms219948 KiB
31Accepted748ms227728 KiB
32Accepted642ms242204 KiB
33Accepted1.011s181444 KiB