#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a : x)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define rsz resize
#define ins insert
const int MOD = 1e9+7; // 998244353 = (119<<23)+1
const ll INF = 1e18;
const int MX = 5e5+5;
const ld PI = 4*atan((ld)1);
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ook order_of_key
#define fbo find_by_order
namespace input {
template<class T> void re(complex<T>& x);
template<class T1, class T2> void re(pair<T1,T2>& p);
template<class T> void re(vector<T>& a);
template<class T, size_t SZ> void re(array<T,SZ>& a);
template<class T> void re(T& x) { cin >> x; }
void re(double& x) { string t; re(t); x = stod(t); }
void re(ld& x) { string t; re(t); x = stold(t); }
template<class T, class... Ts> void re(T& t, Ts&... ts) {
re(t); re(ts...);
}
template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}
using namespace input;
namespace output {
void pr(int x) { cout << x; }
void pr(long x) { cout << x; }
void pr(ll x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(ld x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char* x) { cout << x; }
void pr(const string& x) { cout << x; }
void pr(bool x) { pr(x ? "true" : "false"); }
template<class T> void pr(const complex<T>& x) { cout << x; }
template<class T1, class T2> void pr(const pair<T1,T2>& x);
template<class T> void pr(const T& x);
template<class T, class... Ts> void pr(const T& t, const Ts&... ts) {
pr(t); pr(ts...);
}
template<class T1, class T2> void pr(const pair<T1,T2>& x) {
pr("{",x.f,", ",x.s,"}");
}
template<class T> void pr(const T& x) {
pr("{"); // const iterator needed for vector<bool>
bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0;
pr("}");
}
void ps() { pr("\n"); } // print w/ spaces
template<class T, class... Ts> void ps(const T& t, const Ts&... ts) {
pr(t); if (sizeof...(ts)) pr(" "); ps(ts...);
}
void pc() { pr("]\n"); } // debug w/ commas
template<class T, class... Ts> void pc(const T& t, const Ts&... ts) {
pr(t); if (sizeof...(ts)) pr(", "); pc(ts...);
}
#define dbg(x...) pr("[",#x,"] = ["), pc(x);
}
using namespace output;
namespace io {
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
cin.sync_with_stdio(0); cin.tie(0); // fast I/O
cin.exceptions(cin.failbit); // ex. throws exception when you try to read letter into int
if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}
}
using namespace io;
template<class T> T invGeneral(T a, T b) {
a %= b; if (a == 0) return b == 1 ? 0 : -1;
T x = invGeneral(b,a);
return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;
}
template<class T> struct modular {
T val;
explicit operator T() const { return val; }
modular() { val = 0; }
modular(const ll& v) {
val = (-MOD <= v && v <= MOD) ? v : v % MOD;
if (val < 0) val += MOD;
}
// friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
friend void pr(const modular& a) { pr(a.val); }
friend void re(modular& a) { ll x; re(x); a = modular(x); }
friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }
friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; }
modular operator-() const { return modular(-val); }
modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
modular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; }
friend modular pow(modular a, ll p) {
modular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
return ans;
}
friend modular inv(const modular& a) {
auto i = invGeneral(a.val,MOD); assert(i != -1);
return i;
} // equivalent to return exp(b,MOD-2) if MOD is prime
modular& operator/=(const modular& m) { return (*this) *= inv(m); }
friend modular operator+(modular a, const modular& b) { return a += b; }
friend modular operator-(modular a, const modular& b) { return a -= b; }
friend modular operator*(modular a, const modular& b) { return a *= b; }
friend modular operator/(modular a, const modular& b) { return a /= b; }
};
typedef modular<int> mi;
typedef pair<mi,mi> pmi;
typedef vector<mi> vmi;
typedef vector<pmi> vpmi;
int n,k,p[MX],sum[MX],b[MX],posi[MX];
bool need[MX], ban[MX];
array<int,2> child[MX];
vi v = {0,1};
void gen(int x) {
trav(t,child[x]) if (t) {
gen(t);
ckmax(posi[x],posi[t]);
}
posi[x] ++;
}
int st = -1;
bool bad;
void updBelow(int x) {
if (!x) return;
b[x] = posi[x] = sum[x] = 0;
if (!ban[x]) {
vi v;
trav(t,child[x]) if (t) {
ckmax(b[x],b[t]);
v.pb(posi[t]);
sum[x] += sum[t];
}
if (sz(v) == 2 && posi[child[x][0]]+1 < b[child[x][1]]) bad = 1;
while (sz(v) < 2) v.pb(0);
if (v[0] < v[1]) swap(v[0],v[1]);
posi[x] = min(v[0]+1,v[1]+2);
if (b[x] || need[x]) b[x] ++, sum[x] ++;
}
updBelow(p[x]);
}
void fassert(bool b) { if (!b) exit(5); }
int calcSum(int cur, int x, int h) {
if (cur == st) {
h = 0;
trav(t,child[cur]) {
//ps("HUH",t,b[t]);
ckmax(h,b[t]);
}
h ++;
}
//if (st == cur) ps("????",cur,x,h);
if (b[cur] > h || h > posi[cur]) {
//ps("BAD H",cur,b[cur],posi[cur],h);
bad = 1; return MOD;
}
if (x == cur) {
//ps("OH",h,x);
return v[h];
}
if (x < cur) {
// assign it h-1 or h-2
if (posi[child[cur][0]] >= h-1) {
return 1+calcSum(child[cur][0],x,h-1)+v[h-2];
} else {
return 1+calcSum(child[cur][0],x,h-2)+v[h-1];
}
} else {
assert(x > cur);
if (b[child[cur][1]] == h-1) {
return 1+sum[child[cur][0]]+calcSum(child[cur][1],x,h-1);
} else if (b[child[cur][0]] == h-1) {
return 1+sum[child[cur][0]]+calcSum(child[cur][1],x,h-2);
} else {
return 1+sum[child[cur][0]]+calcSum(child[cur][1],x,h-1);
}
}
}
bool ad(int x) {
bad = 0;
need[x] = 1; updBelow(x);
int z = calcSum(st,x,MOD);
// ps("HA",x,z);
//ps("HA",x,z,bad);
if (!bad && z <= k) {
//ps("HA",x,z);
return 1;
}
//ps("UHOH",x,bad,z);
need[x] = 0; ban[x] = 1;
bad = 0; updBelow(x); fassert(!bad);
//ps("WUT",b[2]);
return 0;
}
void solve(int x) {
if (!ad(x)) return;
//ps("HA",x); exit(0);
trav(t,child[x]) if (t) solve(t);
}
int main() {
setIO(); re(n,k);
while (sz(v) && v.back() <= 500000) v.pb(v[sz(v)-2]+v[sz(v)-1]+1);
v.pop_back();
FOR(i,1,n+1) {
re(p[i]);
if (p[i] == -1) st = i;
else child[p[i]][i>p[i]] = i;
}
gen(st);
//ps("HUH",st,child[2]); exit(0);
solve(st);
FOR(i,1,n+1) pr((int)need[i]);
// you should actually read the stuff at the bottom
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?), set tle
* do smth instead of nothing and stay organized
*/
I3ByYWdtYSBHQ0Mgb3B0aW1pemUgKCJPMyIpCiNwcmFnbWEgR0NDIHRhcmdldCAoInNzZTQiKQoKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwogCnR5cGVkZWYgZG91YmxlIGRiOwp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBsb25nIGRvdWJsZSBsZDsKdHlwZWRlZiBzdHJpbmcgc3RyOwoKdHlwZWRlZiBwYWlyPGludCwgaW50PiBwaTsKdHlwZWRlZiBwYWlyPGxsLGxsPiBwbDsKdHlwZWRlZiBwYWlyPGxkLGxkPiBwZDsKdHlwZWRlZiBjb21wbGV4PGxkPiBjZDsKCnR5cGVkZWYgdmVjdG9yPGludD4gdmk7CnR5cGVkZWYgdmVjdG9yPGxsPiB2bDsKdHlwZWRlZiB2ZWN0b3I8bGQ+IHZkOwp0eXBlZGVmIHZlY3RvcjxzdHI+IHZzOwp0eXBlZGVmIHZlY3RvcjxwaT4gdnBpOwp0eXBlZGVmIHZlY3RvcjxwbD4gdnBsOwp0eXBlZGVmIHZlY3RvcjxjZD4gdmNkOwoKI2RlZmluZSBGT1IoaSxhLGIpIGZvciAoaW50IGkgPSAoYSk7IGkgPCAoYik7IGkrKykKI2RlZmluZSBGMFIoaSxhKSBGT1IoaSwwLGEpCiNkZWZpbmUgUk9GKGksYSxiKSBmb3IgKGludCBpID0gKGIpLTE7IGkgPj0gKGEpOyBpLS0pCiNkZWZpbmUgUjBGKGksYSkgUk9GKGksMCxhKQojZG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