标签:
dfs一遍,维护当前连续遇到的喵的数量,然后剪枝,每个统计孩子数量判断是不是叶子结点。
#include<bits/stdc++.h> using namespace std; const int maxn = 2e5+5; int a[maxn]; int head[maxn],nxt[maxn<<1],to[maxn<<1],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]+1; if(ct[u]>m) return; int ch = 0; 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,-1,sizeof(head)); int n; scanf("%d%d",&n,&m); for(int i = 1; i <= n; i++) scanf("%d",a+i); for(int i = 1; i < n; i++){ int u,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
标签:
原文地址:http://www.cnblogs.com/jerryRey/p/4831302.html