#include #define LL long long #define dl double #define Pi pair #define SZ(a) ((int)a.size()) using namespace std; const int N = 1e5 + 10; int T; int n,k,fa[N],c[N]; vectore[N]; priority_queuepq; int dep[N]; bool vis[N]; void dfs(int x){ vis[x] = 0; if(c[x])pq.emplace(dep[x],x); for(auto v : e[x])dep[v] = dep[x] + 1,dfs(v); } void modify(int x){ vis[x] = 1; for(auto v : e[x]) if(!vis[v])modify(v); } int main(){ // freopen("in.txt","r",stdin); // freopen("o.txt","w",stdout); std::ios::sync_with_stdio(false); std::cin.tie(nullptr); cin >> T; while(T--){ cin >> n >> k; for(int i = 1;i <= n;i++)e[i].clear(); for(int i = 2;i <= n;i++){ int p;cin >> p; e[p].push_back(i); fa[i] = p; } for(int i = 1;i <= n;i++)cin >> c[i]; dfs(1); int ans = 0; while(!pq.empty()){ int x = pq.top().first,y = pq.top().second;pq.pop(); if(vis[y])continue; ans++; for(int i = 1;i <= k && fa[y];i++)y = fa[y]; modify(y); } cout << ans << endl; } return 0; } /**/