forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
summing-in-a-tree.cpp
78 lines (71 loc) · 1.42 KB
/
summing-in-a-tree.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
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
using namespace std;
map<int,int> *mp[500001];
set<int> *ans[500001];
vector< vector<int> > g(500001);
int want[500001];
int n,h;
long long c=0;
int dfs(int u,int l)
{
int mx=-1,mv=-1,v;
for(int i=0;i<g[u].size();i++)
{
v= g[u][i];
int sz = dfs(v,l+1);
if(sz>mx)
{
mx=sz;
mv=v;
}
}
if(mx!=-1)
{
mp[u]=mp[mv];
ans[u]=ans[mv];
}
else
{
mp[u] = new map<int,int>();
ans[u]= new set<int>();
}
(*mp[u])[l]++;
if(want[l]!=0 && want[l]<=(*mp[u])[l])
{
(*ans[u]).insert(l);
}
for(long long int i=0;i<g[u].size();i++)
{
v= g[u][i];
if(v!= mv)
{
for(auto it=(*mp[v]).begin();it!= (*mp[v]).end();it++)
{
(*mp[u])[it->first]+= it->second;
if(want[it->first] !=0 && want[it->first]<=(*mp[u])[it->first])
(*ans[u]).insert(it->first);
}
}
}
c+= (long long)(*ans[u]).size();
return (int)(*mp[u]).size();
}
int main()
{
scanf("%d %d",&n,&h);
for(int i=1;i<n;i++)
{
int p;
scanf("%d",&p);
g[p].push_back(i);
}
for(int i=0;i<=h;i++)
{
scanf("%d",&want[i]);
if(want[i]==0)
c+=n;
}
int sz=dfs(0,0);
printf("%lld\n",c);
return 0;
}