fork download
  1. #include<bits/stdc++.h>
  2. #include<ext/pb_ds/assoc_container.hpp>
  3. #include<ext/pb_ds/tree_policy.hpp>
  4. #define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  5. #define Fileio freopen("output.txt","w",stdout);freopen("input.txt","r",stdin);
  6. #define all(v) v.begin(),v.end()
  7. #define rall(v) v.rbegin(),v.rend()
  8. #define MEM(a,x) memset(a,x,sizeof(a))
  9. #define SZ(v) v.size()
  10. #define nl "\n"
  11. #define bug cout<<"bug"<<nl;
  12. #define pi acos(-1.0)
  13. #define ll long long
  14. #define pb push_back
  15. #define mp make_pair
  16. #define pii pair< int,int >
  17. #define pll pair< ll,ll >
  18. #define vii vector< int >
  19. #define vll vector< ll >
  20. #define vpi vector< pii >
  21. #define vpl vector<pll>
  22. #define MX 100005
  23. #define EPS 1e-12
  24. #define ss second
  25. #define ff first
  26. using namespace std;
  27. using namespace __gnu_pbds;
  28.  
  29. template<typename T>
  30. using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
  31. template<typename T>
  32. using ordered_multiset=tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
  33.  
  34. inline ll powr(int a,int b){ll res=1;while(b){if(b&1) res*=a;a*=a;b/=2;}return res;}
  35. int cases=1;
  36.  
  37. #ifdef ppqq
  38. #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
  39. template < typename Arg1 >
  40. void __f(const char* name, Arg1&& arg1){
  41. cerr << name << " is " << arg1 << std::endl;
  42. }
  43. template < typename Arg1, typename... Args>
  44. void __f(const char* names, Arg1&& arg1, Args&&... args){
  45. const char* comma = strchr(names+1, ',');
  46. cerr.write(names, comma - names) << " is " << arg1 <<" ";
  47. __f(comma+1, args...);
  48. }
  49. #else
  50. #define debug(...)
  51. #endif
  52.  
  53. ///******************************************START******************************************
  54. string col;
  55. int white[4006],black[4005];
  56. vii adj[4005];
  57. void dfs(int u,int p)
  58. {
  59. //debug(u);
  60. for(auto it: adj[u])
  61. {
  62. if(it==p) continue;
  63. dfs(it,u);
  64. black[u]+=black[it];
  65. white[u]+=white[it];
  66. }
  67. if(col[u-1]=='W') white[u]++;
  68. else black[u]++;
  69. //debug(u,white[u],black[u]);
  70. }
  71. int main()
  72. {
  73. FIO;
  74. int t;
  75. cin>>t;
  76. while(t--)
  77. {
  78. int n;
  79. cin>>n;
  80. for(int i=2;i<=n;i++)
  81. {
  82. int x;
  83. cin>>x;
  84. adj[i].pb(x);
  85. adj[x].pb(i);
  86. }
  87.  
  88. cin>>col;
  89.  
  90. dfs(1,-1);
  91. int cnt=0;
  92.  
  93. for(int i=1;i<=n;i++)
  94. {
  95. if(white[i]==black[i]) cnt++;
  96. }
  97.  
  98. cout<<cnt<<nl;
  99.  
  100. for(int i=0;i<n+3;i++)
  101. {
  102. adj[i].clear();
  103. white[i]=0;
  104. black[i]=0;
  105. }
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. }
  120.  
Runtime error #stdin #stdout #stderr 0.01s 5568KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc