#include #define fo(i,a,b) for(int i=a;i<=b;++i) #define fod(i,a,b) for(int i=a;i>=b;--i) #define efo(i,q) for(int i=0;i<(int)B[q].size();++i) #define NX(q) ((q)&(-(q))) using namespace std; typedef long long LL; typedef pair pii; const int N=100500,INF=1e9+7; int read(int &n) { bool q=0;n=0;char ch=' '; for(;ch!='-'&&(ch<'0'||ch>'9');ch=getchar()); if(ch=='-')ch=getchar(),q=1; for(;ch>='0'&&ch<='9';ch=getchar())n=(n<<3)+(n<<1)+ch-48; return q?n=-n:n; } unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); mt19937 rand_num(seed); // 大随机数 int RD(int q){return rand_num()%q;} int n,K,ans; int a[N]; vectorB[N]; int zx[N]; int dfn[N],dep[N],Si[N]; int z[N]; int d[N]; int f[N]; void ADD(int q,int e) { for(;q<=n;q+=NX(q))f[q]+=e; } int find(int q) { int ans=0; for(;q;q-=NX(q))ans+=f[q]; return ans; } void link(int q,int w) { B[q].push_back(w); B[w].push_back(q); } int dfs(int q,int fa) { dfn[q]=++dfn[0]; dep[q]=dep[fa]+1; d[++d[0]]=q; zx[q]=d[max(1,d[0]-K)]; Si[q]=1; efo(i,q)if(B[q][i]!=fa)Si[q]+=dfs(B[q][i],q); --d[0]; return Si[q]; } int main() { int q,w,_; read(_); while(_--) { read(n),read(K); fo(i,2,n)read(q),link(q,i); dfs(1,0); d[0]=0; fo(i,1,n) { read(z[i]); if(z[i])d[++d[0]]=i; } sort(d+1,d+1+d[0],[](int q,int w){return dep[zx[q]]>dep[zx[w]];}); int ans=0; fo(i,1,d[0]) { q=d[i]; if(find(dfn[q]))continue; ++ans; w=zx[q]; ADD(dfn[w],1); ADD(dfn[w]+Si[w],-1); } printf("%d\n",ans); fo(i,0,n)B[i].clear(),f[i]=dfn[i]=Si[i]=zx[i]=0; dfn[0]=0;d[0]=0; } return 0; }