#include using namespace std; #define ll long long #define db double #define X first #define Y second #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define rep0(i,a,b) for(int i=(a);i<(b);++i) #define per(i,a,b) for(int i=(a);i>=(b);--i) #define fore(i,a) for(int i=0;ig[N]; void dfs(int x) { dl[x]=++tt;stk[++tp]=x; if(tp>k)f[x]=stk[tp-k];else f[x]=1; for(int i=0;i=dep[y];} #define ls x<<1,l,m #define rs x<<1|1,m+1,r void build(int x,int l,int r) { s[x]=0;if(l==r)return; int m=l+r>>1;build(ls);build(rs); } void upd(int x,int l,int r,int tl,int tr) { if(tl<=l&&r<=tr){s[x]++;return;} int m=l+r>>1; if(tl<=m)upd(ls,tl,tr); if(tr>m)upd(rs,tl,tr); } int qry(int x,int l,int r,int p) { if(l==r)return s[x]; int m=l+r>>1; return s[x]+(p<=m?qry(ls,p):qry(rs,p)); } int main() { int T=rd(); while(T--) { n=rd();k=rd();m=0;tt=0;tp=0; rep(i,2,n)p[i]=rd(),g[p[i]].push_back(i); rep(i,1,n)a[i]=rd(); dep[1]=0;dfs(1); rep(i,1,n)if(a[i])b[++m]=i; sort(b+1,b+m+1,cmp); build(1,1,n); int ans=0; rep(i,1,m) { int x=b[i],t=qry(1,1,n,dl[x]); if(t)continue; int y=f[x];ans++; upd(1,1,n,dl[y],dr[y]); } printf("%d\n",ans); rep(i,1,n)g[i].clear(); } return 0; }