Kefa and Park
#include<bits/stdc++.h>
#define max 100005
using namespace std;
int cats[max];
vector<int>tree[max];
int visit[max];
int ans=;
int n,m;
vector<int>::iterator it;
void init()
{
ans=;
memset(visit,,sizeof(visit));
for(int i=;i<n;i++)
tree[i].clear();
memset(cats,,sizeof(cats));
}
void dfs(int node,int cat)
{
//cout<<"zhuangtai"<<node<<" "<<cat<<tree[node].size()<<" "<<endl;
visit[node]=;
if(cat>m) return;
else
{
//cout<<"jiancha"<<node<<" "<<cat<<" "<<tree[node].size()<<endl; //if(tree[node].size()==1&&node!=1)
//{
//cout<<"shenegui"<<node<<" "<<cat<<endl;
//ans++;
//}
bool ok=;
for(int i=;i<tree[node].size();i++)
{
if(!visit[tree[node][i]])
{
ok=;
//visit[tree[node][i]]=1;
if(cats[tree[node][i]])
dfs(tree[node][i],cat+cats[tree[node][i]]);
else dfs(tree[node][i],);
}
//ans++;
}
//for(it=tree[node].begin();it!=tree[node].end();it++){
// if(!visit[*it]){
// if
//}
//}
ans+=ok;
}
}
/*4 1
1 1 0 0
1 2
1 3
1 4 7 1
1 0 1 1 0 0 0
1 2
1 3
2 4
2 5
3 6
3 7 3 2
1 1 1
1 2
2 3
*/
int main()
{ while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=;i<=n;i++)
{
cin>>cats[i];
}
int a,b;
//cout<<"ok"<<endl;
for(int i=;i<n;i++)
{
scanf("%d%d",&a,&b);
tree[a].push_back(b);
tree[b].push_back(a);
}
visit[]=;
dfs(,cats[]);
if(ans==) cout<<<<endl;
else
cout<<ans<<endl;
}
return ;
}
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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 mconsecutive 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 "xiyi" (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 1
1 1 0 0
1 2
1 3
1 4
2
7 1
1 0 1 1 0 0 0
1 2
1 3
2 4
2 5
3 6
3 7
2
Hint
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.
Kefa and Park的更多相关文章
- 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/probl ...
- CF580C Kefa and Park dfs
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual pa ...
- codeforces 580C Kefa and Park(DFS)
题目链接:http://codeforces.com/contest/580/problem/C #include<cstdio> #include<vector> #incl ...
- 【CF580C】Kefa and Park
题目大意:给定一棵 N 个节点的有根树(其中根节点始终为 1 号节点),点有点权,点权只有 1 和 0 两种,求从根节点到叶子节点的路径中,有多少条路径满足:路径上最大连续点权为 1 的节点个数不超过 ...
- 「日常训练」Kefa and Park(Codeforces Round #321 Div. 2 C)
题意与分析(CodeForces 580C) 给你一棵树,然后每个叶子节点会有一家餐馆:你讨厌猫(waht?怎么会有人讨厌猫),就不会走有连续超过m个节点有猫的路.然后问你最多去几家饭店. 这题我写的 ...
- CodeForces - 580C Kefa and Park 【BFS】
题目链接 http://codeforces.com/problemset/problem/580/C 题意 根节点是 1 然后所有的叶子结点都是饭店 从根节点到叶子结点的路径上 如果存在 大于m 个 ...
- Codeforces Round #321 (Div. 2) C Kefa and Park(深搜)
dfs一遍,维护当前连续遇到的喵的数量,然后剪枝,每个统计孩子数量判断是不是叶子结点. #include<bits/stdc++.h> using namespace std; ; int ...
- Codeforces Round #321 (Div. 2) Kefa and Park 深搜
原题链接: 题意: 给你一棵有根树,某些节点的权值是1,其他的是0,问你从根到叶子节点的权值和不超过m的路径有多少条. 题解: 直接dfs一下就好了. 代码: #include<iostream ...
- chd校内选拔赛题目+题解
题目链接 A. Currency System in Geraldion 有1时,所有大于等于1的数都可由1组成.没有1时,最小不幸的数就是1. #include<iostream> ...
随机推荐
- 如何修改ubuntu系统的电脑名(主机名)
在按照ubuntu系统时,会提示你给电脑填写一个名字,可能当时你没有想好,就随便填写了一个,可是以后就又有新的想法,想重新更换一个名字,该怎么办呢? 其实很简单.按照下面的步骤即可. 进去后,修改完, ...
- prob
void calc_probability(int num) { , j = , k = ; #define SIZE_NUM 8 int *array_num = NULL; int *rememb ...
- Kik CEO Ted Livingston发博称要成为西方的微信?
加拿大手机聊天应用Kik是一款手机通信录的社交软件,和Snapchat.微信相似,上个月刚拿到3830万美元融资.近日,Kik CEO Ted Livingston在medium博客上发表了the r ...
- cocos基础教程(1)Mac环境下搭建
下面主要介绍cocos2d-x环境的设置以及android的环境搭建 1.下载cocos2d-x 3.0正式版 http://www.cocos2d-x.org/download 2.下载a ...
- Nginx 开启PATHINFO支持ThinkPHP框架实例
ThinkPHP支持通过PATHINFO和URL rewrite的方式来提供友好的URL,只需要在配置文件中设置 'URL_MODEL' => 2 即可.在Apache下只需要开启mod_rew ...
- 小白科普之JavaScript的数组
一.与其他语言数据的比较 相同点:有序列表 不同点:js的数组的每一项可以保存任何类型的数据:数组的大小是可以动态调整的 二.数组创建的两种方法 1) var colors = new ...
- JSOI 2008 火星人prefix
FROM http://www.lydsy.com/JudgeOnline/problem.php?id=1014 LCP问题 给定串 S[0..n] , 对于一对(a,b)其中0<a,b< ...
- 工作中常用shell之ssh登陆不用输入"yes"
ip="192.168.5.166"ssh $ip -o StrictHostKeyChecking=no //ssh登陆不用输入"yes" ...
- 创建一个最简单的Linux随机启动服务
转自: http://xiaoxia.org/2011/11/15/create-a-simple-linux-daemon/
- Python egg
http://blog.csdn.net/turkeyzhou/article/details/8876658