fork download
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target ("sse4")
  3.  
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. typedef double db;
  9. typedef long long ll;
  10. typedef long double ld;
  11. typedef string str;
  12.  
  13. typedef pair<int, int> pi;
  14. typedef pair<ll,ll> pl;
  15. typedef pair<ld,ld> pd;
  16. typedef complex<ld> cd;
  17.  
  18. typedef vector<int> vi;
  19. typedef vector<ll> vl;
  20. typedef vector<ld> vd;
  21. typedef vector<str> vs;
  22. typedef vector<pi> vpi;
  23. typedef vector<pl> vpl;
  24. typedef vector<cd> vcd;
  25.  
  26. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  27. #define F0R(i,a) FOR(i,0,a)
  28. #define ROF(i,a,b) for (int i = (b)-1; i >= (a); i--)
  29. #define R0F(i,a) ROF(i,0,a)
  30. #define trav(a,x) for (auto& a : x)
  31.  
  32. #define mp make_pair
  33. #define pb push_back
  34. #define eb emplace_back
  35. #define f first
  36. #define s second
  37. #define lb lower_bound
  38. #define ub upper_bound
  39.  
  40. #define sz(x) (int)x.size()
  41. #define all(x) begin(x), end(x)
  42. #define rall(x) rbegin(x), rend(x)
  43. #define rsz resize
  44. #define ins insert
  45.  
  46. const int MOD = 1e9+7; // 998244353 = (119<<23)+1
  47. const ll INF = 1e18;
  48. const int MX = 5e5+5;
  49. const ld PI = 4*atan((ld)1);
  50.  
  51. template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
  52. template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
  53.  
  54. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  55.  
  56. #include <ext/pb_ds/tree_policy.hpp>
  57. #include <ext/pb_ds/assoc_container.hpp>
  58. #include <ext/rope>
  59.  
  60. using namespace __gnu_pbds;
  61. using namespace __gnu_cxx;
  62.  
  63. template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  64.  
  65. #define ook order_of_key
  66. #define fbo find_by_order
  67.  
  68. namespace input {
  69. template<class T> void re(complex<T>& x);
  70. template<class T1, class T2> void re(pair<T1,T2>& p);
  71. template<class T> void re(vector<T>& a);
  72. template<class T, size_t SZ> void re(array<T,SZ>& a);
  73.  
  74. template<class T> void re(T& x) { cin >> x; }
  75. void re(double& x) { string t; re(t); x = stod(t); }
  76. void re(ld& x) { string t; re(t); x = stold(t); }
  77. template<class T, class... Ts> void re(T& t, Ts&... ts) {
  78. re(t); re(ts...);
  79. }
  80.  
  81. template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
  82. template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
  83. template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
  84. template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
  85. }
  86.  
  87. using namespace input;
  88.  
  89. namespace output {
  90. void pr(int x) { cout << x; }
  91. void pr(long x) { cout << x; }
  92. void pr(ll x) { cout << x; }
  93. void pr(unsigned x) { cout << x; }
  94. void pr(unsigned long x) { cout << x; }
  95. void pr(unsigned long long x) { cout << x; }
  96. void pr(float x) { cout << x; }
  97. void pr(double x) { cout << x; }
  98. void pr(ld x) { cout << x; }
  99. void pr(char x) { cout << x; }
  100. void pr(const char* x) { cout << x; }
  101. void pr(const string& x) { cout << x; }
  102. void pr(bool x) { pr(x ? "true" : "false"); }
  103. template<class T> void pr(const complex<T>& x) { cout << x; }
  104.  
  105. template<class T1, class T2> void pr(const pair<T1,T2>& x);
  106. template<class T> void pr(const T& x);
  107.  
  108. template<class T, class... Ts> void pr(const T& t, const Ts&... ts) {
  109. pr(t); pr(ts...);
  110. }
  111. template<class T1, class T2> void pr(const pair<T1,T2>& x) {
  112. pr("{",x.f,", ",x.s,"}");
  113. }
  114. template<class T> void pr(const T& x) {
  115. pr("{"); // const iterator needed for vector<bool>
  116. bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0;
  117. pr("}");
  118. }
  119.  
  120. void ps() { pr("\n"); } // print w/ spaces
  121. template<class T, class... Ts> void ps(const T& t, const Ts&... ts) {
  122. pr(t); if (sizeof...(ts)) pr(" "); ps(ts...);
  123. }
  124.  
  125. void pc() { pr("]\n"); } // debug w/ commas
  126. template<class T, class... Ts> void pc(const T& t, const Ts&... ts) {
  127. pr(t); if (sizeof...(ts)) pr(", "); pc(ts...);
  128. }
  129. #define dbg(x...) pr("[",#x,"] = ["), pc(x);
  130. }
  131.  
  132. using namespace output;
  133.  
  134. namespace io {
  135. void setIn(string s) { freopen(s.c_str(),"r",stdin); }
  136. void setOut(string s) { freopen(s.c_str(),"w",stdout); }
  137. void setIO(string s = "") {
  138. cin.sync_with_stdio(0); cin.tie(0); // fast I/O
  139. cin.exceptions(cin.failbit); // ex. throws exception when you try to read letter into int
  140. if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
  141. }
  142. }
  143.  
  144. using namespace io;
  145.  
  146. template<class T> T invGeneral(T a, T b) {
  147. a %= b; if (a == 0) return b == 1 ? 0 : -1;
  148. T x = invGeneral(b,a);
  149. return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;
  150. }
  151.  
  152. template<class T> struct modular {
  153. T val;
  154. explicit operator T() const { return val; }
  155. modular() { val = 0; }
  156. modular(const ll& v) {
  157. val = (-MOD <= v && v <= MOD) ? v : v % MOD;
  158. if (val < 0) val += MOD;
  159. }
  160.  
  161. // friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
  162. friend void pr(const modular& a) { pr(a.val); }
  163. friend void re(modular& a) { ll x; re(x); a = modular(x); }
  164.  
  165. friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
  166. friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }
  167. friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; }
  168.  
  169. modular operator-() const { return modular(-val); }
  170. modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
  171. modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
  172. modular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; }
  173. friend modular pow(modular a, ll p) {
  174. modular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
  175. return ans;
  176. }
  177. friend modular inv(const modular& a) {
  178. auto i = invGeneral(a.val,MOD); assert(i != -1);
  179. return i;
  180. } // equivalent to return exp(b,MOD-2) if MOD is prime
  181. modular& operator/=(const modular& m) { return (*this) *= inv(m); }
  182.  
  183. friend modular operator+(modular a, const modular& b) { return a += b; }
  184. friend modular operator-(modular a, const modular& b) { return a -= b; }
  185. friend modular operator*(modular a, const modular& b) { return a *= b; }
  186.  
  187. friend modular operator/(modular a, const modular& b) { return a /= b; }
  188. };
  189.  
  190. typedef modular<int> mi;
  191. typedef pair<mi,mi> pmi;
  192. typedef vector<mi> vmi;
  193. typedef vector<pmi> vpmi;
  194.  
  195. int n,k,p[MX],sum[MX],b[MX],posi[MX];
  196. bool need[MX], ban[MX];
  197. array<int,2> child[MX];
  198. vi v = {0,1};
  199.  
  200. void gen(int x) {
  201. trav(t,child[x]) if (t) {
  202. gen(t);
  203. ckmax(posi[x],posi[t]);
  204. }
  205. posi[x] ++;
  206. }
  207.  
  208. int st = -1;
  209. bool bad;
  210.  
  211. void updBelow(int x) {
  212. if (!x) return;
  213. b[x] = posi[x] = sum[x] = 0;
  214. if (!ban[x]) {
  215. vi v;
  216. trav(t,child[x]) if (t) {
  217. ckmax(b[x],b[t]);
  218. v.pb(posi[t]);
  219. sum[x] += sum[t];
  220. }
  221. if (sz(v) == 2 && posi[child[x][0]]+1 < b[child[x][1]]) bad = 1;
  222. while (sz(v) < 2) v.pb(0);
  223. if (v[0] < v[1]) swap(v[0],v[1]);
  224. posi[x] = min(v[0]+1,v[1]+2);
  225. if (b[x] || need[x]) b[x] ++, sum[x] ++;
  226. }
  227. updBelow(p[x]);
  228. }
  229.  
  230. void fassert(bool b) { if (!b) exit(5); }
  231.  
  232. int calcSum(int cur, int x, int h) {
  233. if (cur == st) {
  234. h = 0;
  235. trav(t,child[cur]) {
  236. //ps("HUH",t,b[t]);
  237. ckmax(h,b[t]);
  238. }
  239. h ++;
  240. }
  241. //if (st == cur) ps("????",cur,x,h);
  242. if (b[cur] > h || h > posi[cur]) {
  243. //ps("BAD H",cur,b[cur],posi[cur],h);
  244. bad = 1; return MOD;
  245. }
  246. if (x == cur) {
  247. //ps("OH",h,x);
  248. return v[h];
  249. }
  250. if (x < cur) {
  251. // assign it h-1 or h-2
  252. if (posi[child[cur][0]] >= h-1) {
  253. return 1+calcSum(child[cur][0],x,h-1)+v[h-2];
  254. } else {
  255. return 1+calcSum(child[cur][0],x,h-2)+v[h-1];
  256. }
  257. } else {
  258. assert(x > cur);
  259. if (b[child[cur][1]] == h-1) {
  260. return 1+sum[child[cur][0]]+calcSum(child[cur][1],x,h-1);
  261. } else if (b[child[cur][0]] == h-1) {
  262. return 1+sum[child[cur][0]]+calcSum(child[cur][1],x,h-2);
  263. } else {
  264. return 1+sum[child[cur][0]]+calcSum(child[cur][1],x,h-1);
  265. }
  266. }
  267. }
  268.  
  269. bool ad(int x) {
  270. bad = 0;
  271. need[x] = 1; updBelow(x);
  272. int z = calcSum(st,x,MOD);
  273. // ps("HA",x,z);
  274. //ps("HA",x,z,bad);
  275. if (!bad && z <= k) {
  276. //ps("HA",x,z);
  277. return 1;
  278. }
  279. //ps("UHOH",x,bad,z);
  280. need[x] = 0; ban[x] = 1;
  281. bad = 0; updBelow(x); fassert(!bad);
  282. //ps("WUT",b[2]);
  283. return 0;
  284. }
  285.  
  286. void solve(int x) {
  287. if (!ad(x)) return;
  288. //ps("HA",x); exit(0);
  289. trav(t,child[x]) if (t) solve(t);
  290. }
  291.  
  292. int main() {
  293. setIO(); re(n,k);
  294. while (sz(v) && v.back() <= 500000) v.pb(v[sz(v)-2]+v[sz(v)-1]+1);
  295. v.pop_back();
  296. FOR(i,1,n+1) {
  297. re(p[i]);
  298. if (p[i] == -1) st = i;
  299. else child[p[i]][i>p[i]] = i;
  300. }
  301. gen(st);
  302. //ps("HUH",st,child[2]); exit(0);
  303. solve(st);
  304. FOR(i,1,n+1) pr((int)need[i]);
  305. // you should actually read the stuff at the bottom
  306. }
  307.  
  308. /* stuff you should look for
  309.   * int overflow, array bounds
  310.   * special cases (n=1?), set tle
  311.   * do smth instead of nothing and stay organized
  312. */
  313.  
Success #stdin #stdout 0s 4356KB
stdin
71 14
3
1
6
3
4
10
8
6
8
20
12
14
12
16
14
10
18
16
18
39
22
24
22
31
26
27
24
29
27
29
20
33
35
33
31
37
35
37
-1
41
43
41
45
43
57
48
46
51
50
48
45
53
51
55
53
55
39
59
62
59
60
57
64
67
64
65
62
69
67
71
69
stdout
00100101010001010001000100000010000000100010100000000000100001000000000