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> ...
随机推荐
- 优化MySQL,还是使用缓存?读一篇文章有感
今天我想对一个Greenfield项目上可以采用的各种性能优化策略作个对比.换言之,该项目没有之前决策强加给它的各种约束限制,也还没有被优化过. 具体来说,我想比较的两种优化策略是优化MySQL和缓存 ...
- Telnet是什么?
Telnet协议是 用途一: 测试端口号 例子:测试本机的80端口是否打开 1.获得本机IP dos命令:ipconfig 可以看到本机的IP地址是192.168.0.102,当然这也是局域网IP,并 ...
- [置顶] Android应用开发之版本更新你莫愁
传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 今天我们学习如何实现Android应用的自动更新版本功能,这是在各种语言编写的应用中都 ...
- 使用nginx lua实现网站统计中的数据收集
导读网站数据统计分析工具是各网站站长和运营人员经常使用的一种工具,常用的有 谷歌分析.百度统计和腾讯分析等等.所有这些统计分析工具的第一步都是网站访问数据的收集.目前主流的数据收集方式基本都是基于ja ...
- unity StreamingAssets路径
原地址:http://blog.csdn.net/nateyang/article/details/8493791 我们在读写例如XML和TXT文件的时候,在电脑上和手机上路径不一致,造成了很多麻烦, ...
- 获取IOS 设备基本信息
原地址:http://www.cnblogs.com/U-tansuo/p/ios_basis_info.html 1.获取设备类型 (Iphone/ipad 几?) #import "s ...
- OpenGL Vertex Array
转载 http://blog.csdn.net/dreamcs/article/details/7699603
- k-means
参考:http://www.cnblogs.com/jerrylead/archive/2011/04/06/2006910.html k-means是无监督的聚类算法,比较简单,但包含的思想不简单, ...
- mysql数据库性能优化(包括SQL,表结构,索引,缓存)
优化目标减少 IO 次数IO永远是数据库最容易瓶颈的地方,这是由数据库的职责所决定的,大部分数据库操作中超过90%的时间都是 IO 操作所占用的,减少 IO 次数是 SQL 优化中需要第一优先考虑,当 ...
- poj1789 Truck History
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20768 Accepted: 8045 De ...