Codeforces Round #321 (Div. 2) C. Kefa and Park dfs

本题描述了Kefa在含有猫的公园中选择去哪些餐厅的问题,通过DFS算法遍历树状结构来找出所有满足条件的餐厅。

C. Kefa and Park

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/580/problem/C

Description

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 ≤ nxi ≠ 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\n",ans);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值