Codeforces Round #321 (Div. 2) C dfs处理(双向边叶子节点的判断)
2 seconds
256 megabytes
standard input
standard output
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 consecutive vertices with cats.
Your task is to help Kefa count the number of restaurants where he can go.
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.
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.
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
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.
题意:给一棵树 告诉你n个节点上分别是否有猫 从根节点开始到叶子节点的路径上最多能连续经过m只猫
问从根节点,能够到达多少个叶子节点
题解:dfs处理 搜索的同时 ,记录连续经过的猫的最大值
有一点技巧处理,如何判断叶子节点的问题 :因为存储的是双向边,除了根节点,
只有叶子节点的父亲节点的个数为1 标记处理一下。就可以判断出叶子节点。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int nedge;
int n,m;
int pre[];
int have[];
int used[];
int a,b,c;
int ans=;
int vis[];
struct node
{
int to;
int w;
int pre;
}N[];
void add(int aa,int bb,int cc)
{
nedge++;
N[nedge].to=bb;
N[nedge].w=cc;
N[nedge].pre=pre[aa];
pre[aa]=nedge;
}
void dfs(int s,int jishu,int zhi)
{
if(s!=&&zhi<=m&&vis[s]<)
{
ans++;
return ;
}
for(int i=pre[s];i;i=N[i].pre)
{
if(!used[N[i].to])
{
used[N[i].to]=;
if(have[N[i].to])
dfs(N[i].to,jishu+,max(zhi,jishu+));
else
dfs(N[i].to,,zhi);
}
}
}
int main()
{
scanf("%d %d",&n,&m);
nedge=;
memset(pre,,sizeof(pre));
memset(used,,sizeof(used));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
scanf("%d",&have[i]);
for(int i=;i<n;i++)
{
scanf("%d %d",&a,&b);
add(b,a,);
vis[b]++;
add(a,b,);
vis[a]++;
}
used[]=;
if(have[])
dfs(,,);
else
dfs(,,);
cout<<ans<<endl;
return ;
}
Codeforces Round #321 (Div. 2) C dfs处理(双向边叶子节点的判断)的更多相关文章
- 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 ...
- 「日常训练」Kefa and Park(Codeforces Round #321 Div. 2 C)
题意与分析(CodeForces 580C) 给你一棵树,然后每个叶子节点会有一家餐馆:你讨厌猫(waht?怎么会有人讨厌猫),就不会走有连续超过m个节点有猫的路.然后问你最多去几家饭店. 这题我写的 ...
- Codeforces Round #321 (Div. 2) Kefa and Park 深搜
原题链接: 题意: 给你一棵有根树,某些节点的权值是1,其他的是0,问你从根到叶子节点的权值和不超过m的路径有多少条. 题解: 直接dfs一下就好了. 代码: #include<iostream ...
- Codeforces Round #321 (Div. 2) A, B, C, D, E
580A. Kefa and First Steps 题目链接: A. Kefa and First Steps 题意描述: 给出一个序列,求最长不降连续子序列多长? 解题思路: 水题,签到 代码: ...
- Codeforces Round #321 (Div. 2)C(tree dfs)
题意:给出一棵树,共有n个节点,其中根节点是Kefa的家,叶子是restaurant,a[i]....a[n]表示i节点是否有猫,问:Kefa要去restaurant并且不能连续经过m个有猫的节点有多 ...
- Codeforces Round #381 (Div. 2) D dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
- Codeforces Round #290 (Div. 2) B (dfs)
题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相 ...
- Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)
题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...
随机推荐
- Luogu [P1248] 加工生产调度
题目链接 这个题可以贪心 我们首先想:对于所有产品,我们大致可以将其分为三类: ①.在A车间的时间要比B车间长. ②.两者一样. ③.在B车间的时间要比A车间长. 对于这三大类,怎么安排顺序? 可以看 ...
- swl字符串
创建字符串方法 去掉时间戳 #define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT ...
- NOIP2018 全国热身赛 第二场 (不开放)
NOIP2018 全国热身赛 第二场 (不开放) 题目链接:http://noi.ac/contest/26/problem/60 一道蛮有趣的题目. 然后比赛傻逼了. 即将做出来的时候去做别的题了. ...
- PKI
公钥基础设施(Public Key Infrastructure,简称PKI)是眼下网络安全建设的基础与核心,是电子商务安全实施的基本保障,因此,对PKI技术的研究和开发成为眼下信息安全领域的热点.本 ...
- vue.js 发布后路径引用问题
在发布到iis目录下时候,如果放在网站的根目录下的时候,是不会有什么问题的 但是一旦放在了非根目录的其他文件夹里面,这时候index.html里引用的js和css文件路径都会找不到 错误如下 打开in ...
- emwin如何在windows10下vs2015或2017进行仿真。
Make sure the selected Windows SDK is installed:Properties -> Configuration Properties -> Gene ...
- BFS:HDU2597-Dating with girls(2) (分时间标记状态)
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Git-Git初始化
创建版本库及第一次提交 通过如下操作来查看一下您的Git版本. $ git --version git version 1.7.4 在开始 Git 之旅之前,我们需要设置一下 Git 的配置变量,这是 ...
- Storm: 性能优化 (转载)
Storm 性能优化 原文地址:http://www.jianshu.com/p/f645eb7944b0 目录 场景假设 调优步骤和方法 Storm 的部分特性 Storm 并行度 Storm 消 ...
- POJ 3041 Asteroids (二分图最小点覆盖集)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24789 Accepted: 13439 Descr ...