10526 | 2024-04-04 16:25:43 | 111 | K-léptű ősök | cpp17 | Accepted 100/100 | 1.011s | 242204 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 2108 KiB | ||||
subtask2 | 25/25 | ||||||
2 | Accepted | 3ms | 2308 KiB | ||||
3 | Accepted | 3ms | 2532 KiB | ||||
4 | Accepted | 3ms | 2700 KiB | ||||
5 | Accepted | 3ms | 2916 KiB | ||||
6 | Accepted | 3ms | 2960 KiB | ||||
7 | Accepted | 3ms | 3248 KiB | ||||
8 | Accepted | 3ms | 3284 KiB | ||||
9 | Accepted | 4ms | 3740 KiB | ||||
10 | Accepted | 4ms | 4048 KiB | ||||
11 | Accepted | 4ms | 4132 KiB | ||||
subtask3 | 16/16 | ||||||
12 | Accepted | 28ms | 11700 KiB | ||||
13 | Accepted | 52ms | 25128 KiB | ||||
14 | Accepted | 61ms | 25876 KiB | ||||
15 | Accepted | 48ms | 25548 KiB | ||||
16 | Accepted | 56ms | 27588 KiB | ||||
17 | Accepted | 57ms | 29368 KiB | ||||
18 | Accepted | 56ms | 30508 KiB | ||||
subtask4 | 59/59 | ||||||
19 | Accepted | 68ms | 34964 KiB | ||||
20 | Accepted | 97ms | 43708 KiB | ||||
21 | Accepted | 109ms | 41672 KiB | ||||
22 | Accepted | 126ms | 51356 KiB | ||||
23 | Accepted | 128ms | 55844 KiB | ||||
24 | Accepted | 141ms | 51600 KiB | ||||
25 | Accepted | 337ms | 92888 KiB | ||||
26 | Accepted | 333ms | 103048 KiB | ||||
27 | Accepted | 607ms | 187544 KiB | ||||
28 | Accepted | 625ms | 187036 KiB | ||||
29 | Accepted | 820ms | 211904 KiB | ||||
30 | Accepted | 808ms | 219948 KiB | ||||
31 | Accepted | 748ms | 227728 KiB | ||||
32 | Accepted | 642ms | 242204 KiB | ||||
33 | Accepted | 1.011s | 181444 KiB |