codeforces 580D Kefa and Dishes

C. Kefa and Park

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 consecutive vertices 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 test(s)

Input

4 11 1 0 01 21 31 4

Output

2

Input

7 11 0 1 1 0 0 01 21 32 42 53 63 7

Output

2

Note

Let us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.

Note to the first sample test:  The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can‘t go only to the restaurant located at vertex 2.

Note to the second sample test:  The restaurants are located at vertices 4, 5, 6, 7. Kefa can‘t go to restaurants 6, 7.

 1 #include<cstdio>
 2 #include<vector>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 typedef long long ll;
 7 int ans=0,m;
 8 int out[100005];
 9 vector<int>r[100005];
10 bool mark[100005],vis[100005];
11 void dfs(int u,int cat)
12 {
13     if(mark[u])++cat;
14     else cat=0;
15     if(cat>m)return;
16     vis[u]=1;
17     int cnt=0;
18     for(int v=0;v<r[u].size();v++)
19         if(!vis[r[u][v]])
20         {
21             dfs(r[u][v],cat);
22             cnt++;
23         }
24     if(!cnt)ans++;
25 }
26 int main()
27 {
28     int n;
29     scanf("%d%d",&n,&m);
30     for(int i=1;i<=n;i++)
31     {
32         int tmp;
33         scanf("%d",&tmp);
34         mark[i]=tmp;
35     }
36     for(int i=0;i<n-1;i++)
37     {
38         int u,v;
39         scanf("%d%d",&u,&v);
40         r[v].push_back(u);
41         r[u].push_back(v);
42     }
43     dfs(1,0);
44     printf("%d\n",ans);
45     return 0;
46 }
时间: 2024-11-07 00:35:04

codeforces 580D Kefa and Dishes的相关文章

dp + 状态压缩 - Codeforces 580D Kefa and Dishes

Kefa and Dishes Problem's Link Mean: 菜单上有n道菜,需要点m道.每道菜的美味值为ai. 有k个规则,每个规则:在吃完第xi道菜后接着吃yi可以多获得vi的美味值. 问:最多可以获得多少美味值? (1≤m≤n≤18,0≤k≤n∗(n−1)) analyse: 经典的状压DP. 由于最多18道菜,可用一个数s(s<=2^18)来唯一标识一种状态. 对于一个状态s,枚举两个位置i和j:i从已选择的菜中选定,j从未选择的菜中选定. 下一个状态ss的就是:吃完i后接着

Codeforces 580D Kefa and Dishes(状态压缩DP)

题目链接:http://codeforces.com/problemset/problem/580/D 题目大意:有n盘菜每个菜都有一个满意度,k个规则,每个规则由x y c组成,表示如果再y之前吃x那么满意度会额外增加c,现在凯迪想吃m盘菜,并且满意度最大,请求出满意度.解题思路:状压DP,设dp[i][j]表示在状态i并且最后一道菜放在位置j时的最大满意度.注意要处理好一道菜时的情况,以及注意二进制表示中1的个数超过m的情况. 代码: 1 #include<bits/stdc++.h> 2

Codeforces 580D Kefa and Dishes(状压DP)

题目大概说要吃掉n个食物里m个,吃掉各个食物都会得到一定的满意度,有些食物如果在某些食物之后吃还会增加满意度,问怎么吃满意度最高. dp[S][i]表示已经吃掉的食物集合是S且刚吃的是第i个食物的最大满意度 ..没什么好说的 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 5 int val[18],pairs[18][18]; 6 long long d[1<<18][18]; 7 8

codeforces 580D:Kefa and Dishes

Description When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were n dishes. Kefa knows that he needs exactly m dishes. But at that, he doesn't want to order the same dish twice to taste as many d

Codeforces Round #321 (Div. 2) D. Kefa and Dishes (状压dp,再A一发)

题意: 有n个菜,需要m个菜,k个规则  每个菜吃下去的满足感为 a1......an 每个规则: 吃完第u个菜后吃第v个菜满足感+c; 问:怎么吃才能幸福感最大? dp[1<<18][20]:一维表示吃了的菜(1表示吃了,0表吃没吃),第二维表示最后一个吃的菜 dp的初始化: dp[1<<i][i] = saty[i]; 状态转移:dp[i|(1 << k)][k] = max(dp[i][j] + rule[j][k] + safy[k],dp[i|(1 <&

codeforces round 321 div2 D Kefa and Dishes(状态压缩dp)

题意:一共n道菜,吃m道,有k个规则,每个菜有自己的价值,每个规则说明吃完X接着吃Y可以额外获得Z个价值. 问可以获得的最大价值是多少. 思路:1<<18保存所有状态,第二维保存最后吃的哪道菜,然后从吃的菜里选一道,从没吃的菜里选一道,吃完X吃Y, 更新dp,判断一下是否当前吃了m道,更新ans便可 #include <iostream> #include <algorithm> #include <cstring> #include <cmath&g

Codeforces Round #321 (Div. 2) D Kefa and Dishes

用spfa,和dp是一样的.转移只和最后一个吃的dish和吃了哪些有关. 把松弛改成变长.因为是DAG,所以一定没环.状态最多有84934656,514ms跑过,cf机子就是快. #include<bits/stdc++.h> using namespace std; typedef pair<int,int> nd; typedef long long ll; #define fi first #define se second int n,m,a[18]; int g[18][

CF580D Kefa and Dishes 状压dp

When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were n dishes. Kefa knows that he needs exactly m dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as pos

CodeForces 580B Kefa and Company

Description: Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has a