Codeforces 580C Tree +dfs Search

Source: Internet
Author: User
Tags cmath

Description
Kefa decided to celebrate he 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 have already found out what is the vertices with cats in them.

The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he'll go, but unfortunately he's very afraid of cats, so there was no-he wil L go to the restaurant if the path from the restaurant to He house contains more than mconsecutive vertices with cats.

Your task is to help Kefa count the number of restaurants where he can go.

Input
The first line contains integers, N and M (2?≤?n?≤?105, 1?≤?m?≤?n)-the number of vertices of the tree and the Maximu M number of consecutive vertices with cats which is still OK for KEFA.

The second line contains n integers a1,?a2,?...,? A, where each AI either equals to 0 (then vertex i have no cat), or equals to 1 (then vertex I have a cat).

Next n?-? 1 lines contains the edges of the tree in the format "Xiyi" (without the quotes) (1?≤?xi,?yi?≤?n, Xi?≠?yi), where Xi and Yi is 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 consecuti ve vertices with cats.

Sample Input
Input
4 1
1 1 0 0
1 2
1 3
1 4
Output
2
Input
7 1
1 0 1 1 0 0 0
1 2
1 3
2 4
2 5
6 S
3 7
Output
2
Hint
Let us remind the 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 of the vertices connected by an edge, one vertex was a parent (the one closer to the root), and the Other one was a child. A vertex is called a leaf and if it has no children.

Note to the first sample test:the vertices containing cats is marked red. The restaurants is 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 is located at vertices 4, 5, 6, 7. Kefa can ' t go to restaurants 6, 7.

Test Instructions Parsing
    • Build a tree from the root to the leaf node as a path
    • No more than M "continuous consecutive" with cat nodes on the road
    • There are several paths that match the number of

First-time non-AC code

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <queue>using namespace STD;intN,m;BOOLcat[100005];BOOLvisited[100005]; vector<int>tree[100005];intRoad=0;voidDfsintVintMao) {//To the current node there are a few cats in succession    if(mao>m)return;intL=tree[v].size ();if(!l) {//leaveroad++;return; }intW for(intI=0; i<l;i++) {w=tree[v][i];if(Cat[w]) {DFS (w,mao+1); }Else{DFS (W,0); }    }}intMain () {scanf("%d%d", &n,&m);intD for(intI=1; i<=n;i++) {scanf("%d", &d);    Cat[i]=d; }intU,v; for(intI=1; i<n;i++) {scanf("%d%d", &u,&v);    Tree[u].push_back (v); }if(cat[1]) DFS (1,1);ElseDfs1,0);printf("%d\n", road);return 0;}
Cause of error
    • The default input data is the root-to-node, creating a one-way map
      # # Change # #
    • Create a bi-directional diagram
    • New bug: When the leaf node is judged by the one-way graph, the leaf node is the non-out edge
      -{Typical changes do not remember to follow the idea of the previous problem where there is a way out, and output debugging for a long time to locate the bug}
    • Modification: The non-removable connected node is the leaf node.

    • List Contents

Final modification of AC code

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <queue>using namespace STD;intN,m;BOOLcat[100005];BOOLvisited[100005]; vector<int>tree[100005];intRoad=0;voidDfsintVintMao) {//To the current node there are a few cats in successionvisited[v]=1;//cout<< "DFS" <<endl;    if(mao>m)return;intL=tree[v].size ();/*if (!l) {//leave} */    BOOLflag=true;intW for(intI=0; i<l;i++) {w=tree[v][i];if(!visited[w]) {//cout<< "W:" <<w<<endl;flag=false;if(Cat[w]) {DFS (w,mao+1); }Else{DFS (W,0); } visited[w]=1; }    }if(flag) {//leave    //cout<< "resturant:" <<v<<endl;road++;return; }}intMain () {scanf("%d%d", &n,&m);intD for(intI=1; i<=n;i++) {scanf("%d", &d);    Cat[i]=d; }intU,v; for(intI=1; i<n;i++) {scanf("%d%d", &u,&v);        Tree[u].push_back (v);    Tree[v].push_back (U); }if(cat[1]) DFS (1,1);ElseDfs1,0);printf("%d\n", road);return 0;}

Codeforces 580C Tree +dfs Search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.