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 ld PI = 4*atan((ld)1);
  49.  
  50. template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
  51. template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
  52.  
  53. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  54.  
  55. #include <ext/pb_ds/tree_policy.hpp>
  56. #include <ext/pb_ds/assoc_container.hpp>
  57. #include <ext/rope>
  58.  
  59. using namespace __gnu_pbds;
  60. using namespace __gnu_cxx;
  61.  
  62. template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  63.  
  64. #define ook order_of_key
  65. #define fbo find_by_order
  66.  
  67. namespace input {
  68. template<class T> void re(complex<T>& x);
  69. template<class T1, class T2> void re(pair<T1,T2>& p);
  70. template<class T> void re(vector<T>& a);
  71. template<class T, size_t SZ> void re(array<T,SZ>& a);
  72.  
  73. template<class T> void re(T& x) { cin >> x; }
  74. void re(double& x) { string t; re(t); x = stod(t); }
  75. void re(ld& x) { string t; re(t); x = stold(t); }
  76. template<class T, class... Ts> void re(T& t, Ts&... ts) {
  77. re(t); re(ts...);
  78. }
  79.  
  80. template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
  81. template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
  82. template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
  83. template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
  84. }
  85.  
  86. using namespace input;
  87.  
  88. namespace output {
  89. void pr(int x) { cout << x; }
  90. void pr(long x) { cout << x; }
  91. void pr(ll x) { cout << x; }
  92. void pr(unsigned x) { cout << x; }
  93. void pr(unsigned long x) { cout << x; }
  94. void pr(unsigned long long x) { cout << x; }
  95. void pr(float x) { cout << x; }
  96. void pr(double x) { cout << x; }
  97. void pr(ld x) { cout << x; }
  98. void pr(char x) { cout << x; }
  99. void pr(const char* x) { cout << x; }
  100. void pr(const string& x) { cout << x; }
  101. void pr(bool x) { pr(x ? "true" : "false"); }
  102. template<class T> void pr(const complex<T>& x) { cout << x; }
  103.  
  104. template<class T1, class T2> void pr(const pair<T1,T2>& x);
  105. template<class T> void pr(const T& x);
  106.  
  107. template<class T, class... Ts> void pr(const T& t, const Ts&... ts) {
  108. pr(t); pr(ts...);
  109. }
  110. template<class T1, class T2> void pr(const pair<T1,T2>& x) {
  111. pr("{",x.f,", ",x.s,"}");
  112. }
  113. template<class T> void pr(const T& x) {
  114. pr("{"); // const iterator needed for vector<bool>
  115. bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0;
  116. pr("}");
  117. }
  118.  
  119. void ps() { pr("\n"); } // print w/ spaces
  120. template<class T, class... Ts> void ps(const T& t, const Ts&... ts) {
  121. pr(t); if (sizeof...(ts)) pr(" "); ps(ts...);
  122. }
  123.  
  124. void pc() { pr("]\n"); } // debug w/ commas
  125. template<class T, class... Ts> void pc(const T& t, const Ts&... ts) {
  126. pr(t); if (sizeof...(ts)) pr(", "); pc(ts...);
  127. }
  128. #define dbg(x...) pr("[",#x,"] = ["), pc(x);
  129. }
  130.  
  131. using namespace output;
  132.  
  133. namespace io {
  134. void setIn(string s) { freopen(s.c_str(),"r",stdin); }
  135. void setOut(string s) { freopen(s.c_str(),"w",stdout); }
  136. void setIO(string s = "") {
  137. cin.sync_with_stdio(0); cin.tie(0); // fast I/O
  138. cin.exceptions(cin.failbit); // ex. throws exception when you try to read letter into int
  139. if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
  140. }
  141. }
  142.  
  143. using namespace io;
  144.  
  145. template<class T> T invGeneral(T a, T b) {
  146. a %= b; if (a == 0) return b == 1 ? 0 : -1;
  147. T x = invGeneral(b,a);
  148. return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;
  149. }
  150.  
  151. template<class T> struct modular {
  152. T val;
  153. explicit operator T() const { return val; }
  154. modular() { val = 0; }
  155. modular(const ll& v) {
  156. val = (-MOD <= v && v <= MOD) ? v : v % MOD;
  157. if (val < 0) val += MOD;
  158. }
  159.  
  160. // friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
  161. friend void pr(const modular& a) { pr(a.val); }
  162. friend void re(modular& a) { ll x; re(x); a = modular(x); }
  163.  
  164. friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
  165. friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }
  166. friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; }
  167.  
  168. modular operator-() const { return modular(-val); }
  169. modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
  170. modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
  171. modular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; }
  172. friend modular pow(modular a, ll p) {
  173. modular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
  174. return ans;
  175. }
  176. friend modular inv(const modular& a) {
  177. auto i = invGeneral(a.val,MOD); assert(i != -1);
  178. return i;
  179. } // equivalent to return exp(b,MOD-2) if MOD is prime
  180. modular& operator/=(const modular& m) { return (*this) *= inv(m); }
  181.  
  182. friend modular operator+(modular a, const modular& b) { return a += b; }
  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.  
  186. friend modular operator/(modular a, const modular& b) { return a /= b; }
  187. };
  188.  
  189. typedef modular<int> mi;
  190. typedef pair<mi,mi> pmi;
  191. typedef vector<mi> vmi;
  192. typedef vector<pmi> vpmi;
  193.  
  194. const int MX = 5e5+5;
  195.  
  196. int n,k;
  197. int dp[MX][30], p[MX], a[MX];
  198. vi v = {0,1};
  199. int st;
  200. array<int,2> child[MX];
  201. bool need[MX];
  202.  
  203. void pull(int cur) {
  204. int L = child[cur][0], R = child[cur][1];
  205. if (!need[cur] && dp[L][0] == 0 && dp[R][0] == 0) dp[cur][0] = 0;
  206. else dp[cur][0] = MOD;
  207. FOR(i,1,a[cur]+1) {
  208. dp[cur][i] = MOD;
  209. ckmin(dp[cur][i],1+dp[L][i-1]+dp[R][i-1]);
  210. if (i > 1) {
  211. ckmin(dp[cur][i],1+dp[L][i-1]+dp[R][i-2]);
  212. ckmin(dp[cur][i],1+dp[L][i-2]+dp[R][i-1]);
  213. }
  214. }
  215. }
  216.  
  217. void updBelow(int cur, int x) {
  218. if (x < cur) updBelow(child[cur][0],x);
  219. if (x > cur) updBelow(child[cur][1],x);
  220. pull(cur);
  221. }
  222.  
  223. bool ad(int x) {
  224. need[x] = 1; updBelow(st,x);
  225. int res = MOD; F0R(i,30) ckmin(res,dp[st][i]);
  226. //ps("HA",x,res);
  227. if (res <= k) return 1;
  228. need[x] = 0; updBelow(st,x); return 0;
  229. }
  230.  
  231. void solve(int x) {
  232. if (!ad(x)) return;
  233. trav(t,child[x]) if (t) solve(t);
  234. }
  235.  
  236. void gen(int x) {
  237. trav(t,child[x]) if (t) {
  238. gen(t);
  239. ckmax(a[x],a[t]);
  240. }
  241. a[x] ++; pull(x);
  242. }
  243.  
  244. int main() {
  245. setIO(); re(n,k);
  246. while (sz(v) && v.back() <= 500000) v.pb(v[sz(v)-2]+v[sz(v)-1]);
  247. FOR(i,1,n+1) {
  248. re(p[i]);
  249. if (p[i] == -1) st = i;
  250. else child[p[i]][i>p[i]] = i;
  251. }
  252. F0R(i,n+1) F0R(j,30) dp[i][j] = MOD;
  253. dp[0][0] = 0;
  254. gen(st);
  255. //ps("??",dp[st]);
  256. solve(st);
  257. FOR(i,1,n+1) pr((int)need[i]);
  258. // you should actually read the stuff at the bottom
  259. }
  260.  
  261. /* stuff you should look for
  262. * int overflow, array bounds
  263. * special cases (n=1?), set tle
  264. * do smth instead of nothing and stay organized
  265. */
  266.  
Success #stdin #stdout 0s 4264KB
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