dfs一遍,维护当前连续遇到的喵的数量,然后剪枝,每个统计孩子数量判断是不是叶子结点。
#include<bits/stdc++.h>
using namespace std; const int maxn = 2e5+;
int a[maxn];
int head[maxn],nxt[maxn<<],to[maxn<<],ect; inline void addEdge(int u,int v)
{
to[ect] = v;
nxt[ect] = head[u];
head[u] = ect++;
} int ct[maxn],m,cnt;
void dfs(int u,int f)
{
if(a[u]) ct[u] = ct[f]+;
if(ct[u]>m) return;
int ch = ;
for(int i = head[u]; ~i ; i = nxt[i]){
int v = to[i];
if(v == f) continue;
ch++;
dfs(v,u);
}
if(!ch) { cnt++; }
} //#define LOCAL int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
memset(head,-,sizeof(head));
int n; scanf("%d%d",&n,&m);
for(int i = ; i <= n; i++) scanf("%d",a+i);
for(int i = ; i < n; i++){
int u,v; scanf("%d%d",&u,&v);
addEdge(u,v); addEdge(v,u);
}
dfs(,);
printf("%d",cnt);
return ;
}