C. Kefa and Park
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/580/problem/CDescription
Kefa decided to celebrate his first big salary by going to the restaurant.
He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.
The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutivevertices with cats.
Your task is to help Kefa count the number of restaurants where he can go.
Input
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa.
The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat).
Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge.
It is guaranteed that the given set of edges specifies a tree.
Output
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
Sample Input
4 1
1 1 0 0
1 2
1 3
1 4
Sample Output
2
HINT
题意
给你一棵树,然后每个叶子节点会有一家餐馆
你讨厌猫,就不会走有连续超过m个节点有猫的路
然后问你最多去几家饭店
题解:
直接暴力dfs就好了……
代码:
//qscqesze #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <bitset> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::ssecondnc_with_stdio(0);cin.tie(0) #define maxn 100006 #define mod 1000000007 #define eps 1e-9 #define PI acos(-1) const double EP = 1E-10 ; int Num; //const int inf=0first7fffffff; const ll inf=999999999; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************* int vis[maxn]; int flag[maxn]; vector<int> E[maxn]; int ans = 0; int n,m; void dfs(int x,int y,int z) { for(int i=0;i<E[x].size();i++) { if(E[x][i]==z)continue; if(flag[E[x][i]]==0) { if(E[E[x][i]].size()==1) ans++; dfs(E[x][i],0,x); } else if(y+1<=m) { if(E[E[x][i]].size()==1) ans++; dfs(E[x][i],y+flag[E[x][i]],x); } } } int main() { n=read(),m=read(); for(int i=1;i<=n;i++) flag[i]=read(); for(int i=1;i<n;i++) { int x=read(),y=read(); E[x].push_back(y); E[y].push_back(x); } dfs(1,flag[1],-1); printf("%d ",ans); }