2552021-03-06 16:39:50mraronSzállításcpp14Accepted 40/408ms4956 KiB
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
#include<random>
#include<chrono>
#include<bitset>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#  define LOG(x) ((void)0)
#endif

using ll = long long;
using ull = unsigned long long ;
using ld = long double ;
using str = string;
using ordered_set=tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update>;

const double PI=acos(-1);
const ll INF = 1LL<<62;
const ll MINF = -(1LL<<62);

template<typename T> T getint() {
	T val=0;
	char c;
	
	bool neg=false;
	while((c=gc()) && !(c>='0' && c<='9')) {
		neg|=c=='-';
	}

	do {
		val=(val*10)+c-'0';
	} while((c=gc()) && (c>='0' && c<='9'));

	return val*(neg?-1:1);
}

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int>(0, n-1)(rng)

int n,k;
vector<int> adj[20001];
int par[20001];

int mx[20001];
int nxt[20001];
int head[20001];

void dfs1(int x) {
	if(sz(adj[x])==0) {
		mx[x]=1;
		head[x]=1;
		return ;
	}
	
	int mx_ind=adj[x][0];
	for(auto i:adj[x]) {
		dfs1(i);
		if(mx[mx_ind]<mx[i]) {
			mx_ind=i;
		}
	}
	
	head[mx_ind]=0;
	head[x]=1;
	mx[x]=1+mx[mx_ind];
	nxt[x]=mx_ind;
}

int main() {
	IO;
	cin>>n>>k;
	for(int i=1;i<=n;++i) {
		cin>>par[i];
		adj[par[i]].pb(i);
	}
	
	vector<pair<int,int>> lst;
	dfs1(1);
	for(int i=1;i<=n;++i) {
		if(head[i]) lst.pb({mx[i], i});
	}
		
	sort(all(lst));
	reverse(all(lst));
	k=min(k, sz(lst));
	
	int res=-1;
	vector<int> ans;
	for(int i=0;i<k;++i) {
		res+=lst[i].xx;
		while(nxt[lst[i].yy]) lst[i].yy=nxt[lst[i].yy];
		ans.pb(lst[i].yy);
	}
	
	cout<<res<<"\n";
	for(auto i:ans) cout<<i<<" ";
	cout<<"\n";
	
	
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/02ms2752 KiB
2Accepted0/03ms3256 KiB
3Accepted2/21ms2872 KiB
4Accepted2/22ms2888 KiB
5Accepted2/22ms2896 KiB
6Accepted2/22ms2904 KiB
7Accepted2/22ms2920 KiB
8Accepted2/22ms2924 KiB
9Accepted2/22ms2932 KiB
10Accepted2/22ms2936 KiB
11Accepted2/22ms3164 KiB
12Accepted2/23ms3204 KiB
13Accepted2/23ms3284 KiB
14Accepted2/23ms3468 KiB
15Accepted2/24ms3536 KiB
16Accepted2/24ms3604 KiB
17Accepted3/34ms3836 KiB
18Accepted3/34ms3920 KiB
19Accepted3/38ms4848 KiB
20Accepted3/37ms4956 KiB