// #pragma comment(linker, "/STACK:1024000000,1024000000") #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rep(i, l, r) for(int i=l; i<=r; i++) #define dow(i, l, r) for(int i=l; i>=r; i--) #define fi x #define se second #define pb push_back #define mp make_pair #define clr(x, c) memset(x,c,sizeof(x)) typedef long long ll; typedef unsigned long long ull; typedef pair Pii; inline int read() { int x=0,f=0; char ch=getchar(); while (ch<'0' || '9'n) struct edge{int y; edge *n;} e[maxm], *fir[maxn], *pt=e; inline void initEdge() { clr(fir,0); pt=e; } inline void addEdge(int x, int y) { pt->y=y, pt->n=fir[x], fir[x]=pt++; // pt->y=x, pt->n=fir[y], fir[y]=pt++; } // =========================== 快速幂 // inline int pow(int x, int t) { // int g = 1; // while (t) { // if (t&1) g = 1LL*g*x%Q; // x = 1LL*x*x%Q; // t >>= 1; // } // return g; // } // ====================================================== 主程序 int h[maxn], c[maxn]; int dep[maxn]; queueq; struct node{int x,y;}; bool cmp(node a, node b){return a.y>b.y;} vectorv; int main() { int T = read(); while (T--) { int n = read(), k = read(); initEdge(); h[1] = 0; dep[1] = 0; q.push(1); rep(i, 2, n) addEdge(h[i]=read(), i); while (!q.empty()) { int x = q.front(); q.pop(); travel(x) dep[p->y] = dep[x] + 1, q.push(p->y); } v.clear(); rep(i, 1, n) if (read()) v.push_back((node){i, dep[i]}); if (v.empty()) { puts("0"); continue; } sort(v.begin(), v.end(), cmp); int m = (int)v.size(), ans = 0; rep(i, 1, n) c[i] = 0; rep(i, 0, m-1) { int x = v[i].x, r = k, color = 0; while (r >= 0 && x) { if (c[x]) { color = c[x]; break; } x = h[x]; r--; } x = v[i].x, r = k; if (!color) color = x, ans += 1; while (r >= 0 && x && !c[x]) { c[x] = color; x = h[x]; r--; } } printf("%d\n", ans); } return 0; }