DFS again, maintain the number of currently encountered meow, and then prune, each statistic the number of children to judge is not a leaf node.
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 2e5+5;intA[MAXN];inthead[maxn],nxt[maxn<<1],to[maxn<<1],ect;inlinevoidAddedge (intUintv) {To[ect]=v; Nxt[ect]=Head[u]; Head[u]= ect++;}intct[maxn],m,cnt;voidDfsintUintf) { if(A[u]) ct[u] = ct[f]+1; if(ct[u]>m)return; intCH =0; for(inti = Head[u]; ~i; i =Nxt[i]) { intv =To[i]; if(v = = f)Continue; CH++; DFS (V,U); } if(!CH) {cnt++; }}//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endifmemset (Head,-1,sizeof(head)); intN scanf"%d%d",&n,&m); for(inti =1; I <= N; i++) scanf ("%d", A +i); for(inti =1; I < n; i++){ intU,v; scanf"%d%d",&u,&v); Addedge (U,V); Addedge (V,u); } DFS (1,0); printf ("%d", CNT); return 0;}
Codeforces Round #321 (Div. 2) C Kefa and Park