Codeforces Round #321 (Div. 2)C. Kefa and Park

C. Kefa and Park
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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 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 test(s)
input
4 1
1 1 0 0
1 2
1 3
1 4
output
2
input
7 1
1 0 1 1 0 0 0
1 2
1 3
2 4
2 5
3 6
3 7
output
2
Note

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.

http://codeforces.com/contest/580/problem/C

大概题意就是给了一颗树,n个节点,每个节点有猫或者没猫,最多能忍受连续m个有猫的节点,问满足条件能到达多少个leaves。

简单的树的遍历,当时用的BFS,后来用DFS就可以解决,而且代码很好写,也是第一次用vector,确实很好用。

#include<cstdio>
#include<cstring>
#include<vector>
#define maxn 100005
using namespace std;
vector<int>t[maxn];
int ans=0;
int cat[maxn];
int n,m,a,b;
void dfs(int now,int pre,int c)
{
	int flag;
	if(cat[now])
	c++;
	else c=0;
	if(c>m)
	return ;
	flag=0;
	int leng=t[now].size();
	for(int i=0;i<leng;i++)
	{
		int k=t[now][i];
		if(k==pre)
		continue;
		dfs(k,now,c);
		flag=1;
	}
	if(!flag)
	{

	ans++;
	}
}

int main()
{
	
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&cat[i]);
	}
	for(int i=1;i<=n-1;i++)
	{
		scanf("%d%d",&a,&b);
		t[a].push_back(b);
		t[b].push_back(a);
	}
	dfs(1,0,0);
	printf("%d\n",ans);
}

<IBM DB2>《DB2 官方监控脚本 2022版本》 DB2 监控脚本 阅读详情

相关推荐

协方差矩阵

对两个类内协方差矩阵进行对角化,可以将它们变换为对角矩阵。这个过程可以通过对每个协方差矩阵进行特征值分解来实现,从而得到它们的特征向量和特征值,进而得到它们的对角矩阵形式。这个过程的意义在于,它可以帮助我们找到一个新的坐标系,使得在这个坐标系下,每个特征之间都是独立的,从而简化了计算。具体来说,当我们使用对角协方差矩阵作为特征向量的坐标系时,它意味着每个特征之间都是相互独立的,从而方便了进一步的分析和处理。在实际应用中,对角化协方差矩阵可以用于降维、特征选择和分类等任务。

m0_55196097的博客 1万+

cf 580 c(邻接表+dfs)

C. Kefa and Park time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kefa decided to celebrate his first big sa

__小明003的博客 777

二阶系统的带宽(频域分析)

下面以典型I型系统为例,计算系统的开环截止频率和闭环截止频率。设I型系统为: 1:系统的开环截止频率为 2:系统的闭环截止频率 其实我们在工程整定时,把ζ = 0.707,也把系统的带宽近视看成自然震荡角频率即 (wb≈wc≈wnw_{b}\approx w_{c}\approx w_{n}wb​≈wc​≈wn​)对于二阶系统。 ...

wanrenqi的博客 2万+

C. Kefa and Park【树的遍历】

#include<bits/stdc++.h> using namespace std; const int N=1e5+10; int a[N],cnt[N],st[N],n,m,ans; vector<int>ve[N]; void dfs(int u,int t,int maxv)//t连续的1 maxv总路径中最长的连续的1 { maxv=max(maxv,t+a[u]); if(a[u]==0) cnt[u]=0; else cnt[u]=t+a[u]; st[u.

辉小歌的博客 523

MT4/MT5数据源接入全攻略:从买服务、自研API到低成本DDE,哪种方案适合你?

本文全面解析MT4/MT5数据源接入的三种主流方案:商业服务、自研API和低成本DDE,详细对比其技术参数、成本结构和适用场景。帮助技术团队根据业务需求选择最优方案,确保外汇交易系统的数据流稳定性和实时性,提升交易体验和业务连续性。

weixin_42568801的博客 173

信息安全与技术——(十一)恶意代码检测与防范技术

恶意代码(Malicious Code )或恶意软件(Malware )主要是指以危害信息的安全等不良意图为目的的程序,它们一般潜伏在受害计算机系统中实施破坏或窃取信息。

m0_63853448的博客 5992

Kefa and Park

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 cont...

weixin_44824383的博客 232

本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印

本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印

ProceduralMan的博客 7411

Codeforces Round #321 (Div. 2) 580C - Kefa and Park

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

huziling的博客 635

2.6 SST的核心性能指标体系

本文构建了固态变压器(SST)的核心性能指标体系,涵盖电气性能、可靠性、经济性和环境适配性四大类指标。电气性能指标包括运行效率(≥97%)、功率密度(≥900kVA/m³)、谐波畸变率(≤3%)等关键参数;可靠性指标设定MTBF≥43800h、年均故障率≤1.2次/年等标准;经济性指标对比分析了初始购置成本(45-55万元)和全生命周期成本(48-55万元);环境适配性指标则明确了温度、海拔等工况要求。该体系采用量化公式和国标限值,为SST的性能测试、选型和优化提供了统一标准。

技术工作者 606

手把手教你为高云DDR3控制器添加物理约束:从位置锁定到参数配置

本文详细介绍了在高云FPGA平台上为DDR3控制器添加物理约束的实战指南,从位置锁定到参数配置。通过解析高云DDR3控制器的架构,提供关键模块的位置约束和参数调优方法,帮助开发者解决时序不稳定、初始化失败等常见问题,实现稳定可靠的DDR3接口。

weixin_33738555的博客 276

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/C Description Kefa decided to celebrate his first big salary by going...

weixin_34320724的博客 100

手把手教你学Simulink——基于传统电网建模与稳定性分析的场景实例:基于同步发电机的单机无穷大系统建模与稳定性分析

本文详细介绍了使用MATLAB/Simulink进行单机无穷大系统(SMIB)建模与稳定性分析的方法。教程从系统结构设计入手,包含同步发电机、输电线路、无穷大母线等核心模块的建模步骤和参数配置示例。重点讲解了七个关键步骤:项目创建、模块配置、控制系统集成、负载添加(可选)以及仿真运行与结果分析,并提供了具体的MATLAB代码示例。最后通过性能评估指标(功角差、电压跌落、频率偏差)分析系统稳定性,为电力系统动态特性研究提供了实用指导,强调准确参数设置对仿真结果的重要性。

MHD0815的博客 298

Codeforces Round #321 (Div. 2) C. Kefa and Park time limit per test

C. Kefa and Park time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kefa decided to celebrate his first big salary by go

ysCover 936

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Codeforces Round #321 (Div. 2) C. Kefa and Park

题意:给你一颗树,从根节点出发到叶子节点,如果中间有连续m个标记的点就返回,问有多少个方法思路:dfs水题,模拟跑跑就好了错误及反思:写的太快,忘了考虑只有一个儿子的情况了。。。代码:#include<bits/stdc++.h> using namespace std; const int N = 100100; int n,m; int arr[N]; int ans=0; vector<int

Roll_Keyboard的博客 316

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

「日常训练」Kefa and Park(Codeforces Round #321 Div. 2 C)

题意与分析(CodeForces 580C) 给你一棵树,然后每个叶子节点会有一家餐馆;你讨厌猫(waht?怎么会有人讨厌猫),就不会走有连续超过m个节点有猫的路。然后问你最多去几家饭店。 这题我写的挫的要死,实际上只需要一次dfs就可以解决了,我愣是用了一次bfs+一次dfs来写——dfs是为了判断是否是叶子节点的。。。。但是bfs干的活完全可以让dfs在找叶子节点的时候顺带解决了,所以就很坑。...

weixin_30725315的博客 174

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Codeforces Round #321 (Div. 2) 580C Kefa and Park(dfs)

题目链接: 第一行给你n、m,接下来的一行有n个数,如果为1则代表这个点有cat,接下来的n-1行每行给出x、y,表示x、y相连。问你从1出发 能到达的叶子结点有几个,要求到达叶子结点的路上连续有cat的点不超过m。 a数组保存结点是否含有cat,从1这个点开始dfs,每次dfs对cat数组操作,记录连续的含有cat的点数,回溯时再判断当前结点是否满足 条件,要求结点为叶子

GKHack's Blog 2041

Codeforces Round #321 (Div. 2) C Kefa and Park(深搜)

dfs一遍,维护当前连续遇到的喵的数量,然后剪枝,每个统计孩子数量判断是不是叶子结点。 #include<bits/stdc++.h> using namespace std; const int maxn = 2e5+5; int a[maxn]; int head[maxn],nxt[maxn<<1],to[maxn<<1],ect; ...

weixin_34347651的博客 109

Codeforces round #321 (DIV. 2)

题解链接:http://www.cygmasot.com/index.php/2015/09/23/codeforces_580/ 链接 A:Kefa and First Steps 题意:the length of the maximum non-decreasing subsegment 直接搞,水题 #include #include #i

九野的博客 1958

Kefa and Park(简单dfs)

Kefa and Park 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 ofnvertices with the root at vertex1...

qq_44797733的博客 354

【CodeForces】 CF580C. Kefa and Park【树的遍历】

题意 从根节点遍历每一条路径查找符合要求的路径的条数 思路 暴力建图,跑dfs即可。 AC代码 #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") #include<bits/stdc++.h> using namespace std; #define x first #define y second typedef long l

Gorgeous_wxd 477
上一篇: BIT2014级软件学院程序设计-02进制转换
下一篇: POJ 1001Exponentiation
Blue_Ghost_
博客等级 码龄12年 8粉丝 31原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值