Codeforces Round #321 (Div. 2) C. Kefa and Park dfs
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 11 1 0 0
1 2
1 3
1 4
Sample Output
2HINT
?
題意
給你一棵樹,然后每個葉子節(jié)點(diǎn)會有一家餐館
你討厭貓,就不會走有連續(xù)超過m個節(jié)點(diǎn)有貓的路
然后問你最多去幾家飯店
題解:
直接暴力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); }?
總結(jié)
以上是生活随笔為你收集整理的Codeforces Round #321 (Div. 2) C. Kefa and Park dfs的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux系统服务之dhcp
- 下一篇: 【HDU 5532 Almost Sor