-
Notifications
You must be signed in to change notification settings - Fork 0
/
G.cpp
67 lines (64 loc) · 1.15 KB
/
G.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<bits/stdc++.h>
#include<string>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
vector<long long>adj[5000];
long long chi[5000],b[5000],w[5000],co;
string s;
void postorder(long long u)
{
if(chi[u]==0)
{
if(s[u]=='B')
b[u]++;
else
w[u]++;
return;
}
for(long long i=0;i<adj[u].size();i++)
{
postorder(adj[u][i]);
b[u]+=b[adj[u][i]];
w[u]+=w[adj[u][i]];
}
if(s[u]=='B')
b[u]++;
else
w[u]++;
if(b[u]==w[u])
co++;
}
int main()
{
fastio;
long long t,n,i,ara[5000];
cin>>t;
while(t--)
{
cin>>n;
co=0;
for(i=0;i<n;i++)
{
//vis[i]=0;
chi[i]=0;
b[i]=0;
w[i]=0;
}
for(i=1;i<n;i++)
{
cin>>ara[i];
ara[i]--;
chi[ara[i]]=1;
adj[ara[i]].push_back(i);
}
cin>>s;
postorder(0);
cout<<co<<"\n";
s.clear();
for(i=0;i<n;i++)
{
adj[i].clear();
}
}
return 0;
}