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> ...
随机推荐
- 设置dom元素可拖动,支持ie5+
摘要: 最近在项目中要做一个图片预览的功能,这时候会遇到用户上传很大的图片,已经超出视图界面.最终决定做一个在固定宽和高的位置,用户可以拖动图片查看.所以自己就写了一个支持ie5+,chrome,Fi ...
- Rescue
1039: Rescue Time Limit: 1 Sec Memory Limit: 32 MBSubmit: 1320 Solved: 306 Description Angel was c ...
- docker commit容器
docker commit 容器ID 镜象REPOSITORY 镜象TAG 如docker commit 52b41c68ac7b registry.lenovows.com:5000/video-a ...
- unity3d camera.culling mask
原地址:http://www.cnblogs.com/88999660/archive/2013/03/14/2959439.html 官方文档对CullingMask的注释只是说了通过位移运算符,可 ...
- WPF 路由事件总结
1.什么是路由事件 已下为MSDN中的定义 功能定义:路由事件是一种可以针对元素树中的多个侦听器(而不是仅针对引发该事件的对象)调用处理程序的事件. 实现定义:路由事件是一个 CLR 事件,可以由 R ...
- ZeroMQ(java)中组件间数据传输(Pipe的实现)
在ZeroMQ(java)中,整个IO的处理流程都是分层来进行的,当然处于最下端的肯定是前面介绍过的poller以及StreamEngin了....涉及到上层的话就还有session,以及socket ...
- Action的动态调用方法
Action执行的时候并不一定要执行execute方法,我们可以指定Action执行哪个方法: 1. 方法一(通过methed属性指定执行方法): 可以在配置文件中配置Action的时候用method ...
- Tushare的安装
TuShare是一个免费.开源的python财经数据接口包.主要实现对股票等金融数据从数据采集.清洗加工到数据存储的过程,能够为金融分析人员提供快速.整洁.和多样的便于分析的数据. 考虑到python ...
- RouterOS DNS劫持 -- A记录
通常我们使用RouterOS的DNS主要是用于实现DNS缓存功能,即由RouterOS实现DNS服务器解析功能,除了这个功能,RouterOS可以实现对内网域名解析劫持,即实现路由网关的A记录查询. ...
- centos安装ssdb
在编译之前要下gcc编译器 yum -y install gcc* 编译和安装 wget --no-check-certificate https://github.com/ideawu/ssdb ...