0w1

CFR 580 C. Kefa and Park ( DFS )

Problem - C - Codeforces
Not much do I really need to mention. We will only have to traverse the graph with a single DFS.

void dfs( const vvi &G, const vi &has_cat, int cat_cnt, int u, int fa, int &ans ){
    if( has_cat[ u ] ) ++cat_cnt; // cat_cnt += has_cat[ u ];
    else cat_cnt = 0;
    if( cat_cnt > M ) return;
    if( G[ u ].size() == 1 and G[ u ][ 0 ] == fa ) return ( void )( ++ans );
    for( int v : G[ u ] )
        if( v != fa )
            dfs( G, has_cat, cat_cnt, v, u, ans );
}

void solve(){
    cin >> N >> M;
    vi has_cat( N );
    for( int i = 0; i < N; ++i )
        cin >> has_cat[ i ];
    vvi G( N );
    for( int i = 0; i < N - 1; ++i ){
        int u, v; cin >> u >> v;
        --u, --v;
        G[ u ].push_back( v );
        G[ v ].push_back( u );
    }
    int ans = 0;
    dfs( G, has_cat, 0, 0, -1, ans );
    cout << ans << endl;
}