fork download
  1. /* AUTHOR: TUAN ANH - BUI */
  2. // ~~ icebear ~~
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7. typedef pair<int, int> ii;
  8. typedef pair<int, ii> iii;
  9.  
  10. template<class X, class Y>
  11. bool minimize(X &x, const Y &y) {
  12. if (x > y) return x = y, true;
  13. return false;
  14. }
  15.  
  16. template<class X, class Y>
  17. bool maximize(X &x, const Y &y) {
  18. if (x < y) return x = y, true;
  19. return false;
  20. }
  21.  
  22. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  23. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  24. #define REP(i, n) for(int i=0; i<(n); ++i)
  25. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  26. #define MASK(i) (1LL << (i))
  27. #define BIT(S, i) (((S) >> (i)) & 1)
  28. #define mp make_pair
  29. #define pb push_back
  30. #define fi first
  31. #define se second
  32. #define all(x) x.begin(), x.end()
  33. #define task "icebear"
  34. /*END OF TEMPLATE. ICEBEAR AND THE CAT WILL WIN TST26 */
  35.  
  36. const int MOD = 1e9 + 7;
  37. const int inf = (int)1e9 + 27092008;
  38. const ll INF = (ll)1e18 + 27092008;
  39. const int N = 5e5 + 5;
  40. int n, q;
  41. vector<int> G[N];
  42. char c[N];
  43. int cnt[N][26], sz[N], tin[N], ans[N], tout[N], tour[N], timer = 0, h[N];
  44. vector<ii> Q[N];
  45.  
  46. void dfs(int u) {
  47. tin[u] = ++timer;
  48. tour[timer] = u;
  49. sz[u] = 1;
  50. for(int v: G[u]) {
  51. h[v] = h[u] + 1;
  52. dfs(v);
  53. sz[u] += sz[v];
  54. }
  55. tout[u] = timer;
  56. }
  57.  
  58. void sack(int u, bool keep = true) {
  59. int hvy = 0;
  60. for(int v : G[u]) if (sz[v] > sz[hvy])
  61. hvy = v;
  62.  
  63. for(int v : G[u]) if (v != hvy)
  64. sack(v, false);
  65.  
  66. if (hvy > 0) sack(hvy, true);
  67.  
  68. for(int v : G[u]) if (v != hvy) {
  69. FOR(i, tin[v], tout[v]) {
  70. int x = tour[i];
  71. cnt[h[x]][c[x] - 'a']++;
  72. }
  73. }
  74.  
  75. cnt[h[u]][c[u] - 'a']++;
  76. for(ii x : Q[u]) {
  77. int k, i; tie(k, i) = x;
  78. int odd = 0;
  79. REP(c, 26) if (cnt[k][c] & 1)
  80. odd++;
  81. ans[i] = (odd <= 1);
  82. }
  83.  
  84. if (keep == false) {
  85. FOR(i, tin[u], tout[u]) {
  86. int x = tour[i];
  87. cnt[h[x]][c[x] - 'a']--;
  88. }
  89. }
  90. }
  91.  
  92. void init(void) {
  93. cin >> n >> q;
  94. FOR(i, 2, n) {
  95. int p; cin >> p;
  96. G[p].pb(i);
  97. }
  98. FOR(i, 1, n) cin >> c[i];
  99. FOR(i, 1, q) {
  100. int v, k;
  101. cin >> v >> k;
  102. Q[v].pb(mp(k, i));
  103. }
  104. }
  105.  
  106. void process(void) {
  107. h[1] = 1;
  108. dfs(1);
  109. sack(1);
  110. FOR(i, 1, q) cout << (ans[i] ? "Yes\n" : "No\n");
  111. }
  112.  
  113.  
  114. int main() {
  115. ios_base::sync_with_stdio(0);
  116. cin.tie(0); cout.tie(0);
  117. if (fopen(task".inp", "r")) {
  118. freopen(task".inp", "r", stdin);
  119. freopen(task".out", "w", stdout);
  120. }
  121. int tc = 1;
  122. // cin >> tc;
  123. while(tc--) {
  124. init();
  125. process();
  126. }
  127. return 0;
  128. }
  129.  
  130.  
Success #stdin #stdout 0.01s 36740KB
stdin
Standard input is empty
stdout
Standard output is empty