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, p[N];
  41. vector<int> G[N];
  42. int sz[N], tin[N], ans[N], tout[N], tour[N], timer = 0, h[N];
  43. vector<ii> Q[N];
  44. string s[N];
  45. map<string, int> cnt[N];
  46.  
  47. void dfs(int u) {
  48. tin[u] = ++timer;
  49. tour[timer] = u;
  50. sz[u] = 1;
  51. for(int v: G[u]) {
  52. h[v] = h[u] + 1;
  53. dfs(v);
  54. sz[u] += sz[v];
  55. }
  56. tout[u] = timer;
  57. }
  58.  
  59. void sack(int u, bool keep = false) {
  60. int hvy = 0;
  61. for(int v : G[u]) if (sz[v] > sz[hvy])
  62. hvy = v;
  63.  
  64. for(int v : G[u]) if (v != hvy)
  65. sack(v, false);
  66.  
  67. if (hvy > 0) sack(hvy, true);
  68.  
  69. for(int v : G[u]) if (v != hvy) {
  70. FOR(i, tin[v], tout[v]) {
  71. int x = tour[i];
  72. cnt[h[x]][s[x]]++;
  73. }
  74. }
  75.  
  76. cnt[h[u]][s[u]]++;
  77. for(ii x : Q[u]) {
  78. int k, i; tie(k, i) = x;
  79. if (k + h[u] <= n) ans[i] = cnt[k + h[u]].size();
  80. }
  81.  
  82. if (keep == false) {
  83. FOR(i, tin[u], tout[u]) {
  84. int x = tour[i];
  85. cnt[h[x]].clear();
  86. }
  87. }
  88. }
  89.  
  90. void init(void) {
  91. cin >> n;
  92. FOR(i, 1, n) {
  93. cin >> s[i] >> p[i];
  94. if (!p[i]) continue;
  95. G[p[i]].pb(i);
  96. }
  97. cin >> q;
  98. FOR(i, 1, q) {
  99. int v, k;
  100. cin >> v >> k;
  101. Q[v].pb(mp(k, i));
  102. }
  103. }
  104.  
  105. void process(void) {
  106. FOR(i, 1, n) if (p[i] == 0) {
  107. dfs(i);
  108. sack(i);
  109. }
  110. FOR(i, 1, q) cout << ans[i] << ' ';
  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.02s 70536KB
stdin
Standard input is empty
stdout
Standard output is empty