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

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 firs... 阅读详情
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 consecutive 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 "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.


题目大意:一棵树,有的节点上有猫,有的没有。求出从根节点出发,到达叶子节点的路径中连续出现猫的数量不能超过m的路径有几条。


解题思路:DFS。细节:vector构造树时因为不知道谁是父亲节点谁是子节点,因此两个点都当做父亲节点处理。判断叶子节点时,儿子个数为1的就是叶子节点。


代码如下:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#define ll long long
using namespace std;
const int maxn=100005;
int a[maxn],ans,sum,m;
int vis[maxn];
vector <int> v[maxn];
void dfs(int x,int cnt)
{
	if(cnt>m)
		return;
	int len=v[x].size();
	if(len==1 && x!=1)
	{
		if(a[x])
			cnt++;
		if(cnt>m)
			return;
		ans++;
		return ;
	}
	for(int i=0;i<len;i++)
	{
		if(vis[v[x][i]])
			continue;
		else
			vis[v[x][i]]=1;
		if(a[x])
			dfs(v[x][i],cnt+1);
		else
			dfs(v[x][i],0);
	}
}
int main(void)
{
	int n,x,y;
	ans=0,sum=0;
	memset(vis,0,sizeof(vis));
	scanf("%d%d",&n,&m);
	vis[1]=1;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=0;i<n-1;i++)
	{
		scanf("%d%d",&x,&y);
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs(1,0);
	printf("%d\n",ans);
}


【gateway nc adapter虚拟网卡 win11 联想】问题的解决 由于需要登录公司内网,于是启用奇安信VPN。但启动后报出网关未连接的问题,于是我检查了我的网络适配器,并尝试解决,以下给出几种我实践过程中的行动,并附带最后成功的解决方法。 阅读详情

相关推荐

STM32 FOC电机控制:手把手教你用CubeMX配置TIM1中心对齐PWM(含代码避坑)

本文详细解析了STM32 FOC电机控制中TIM1中心对齐PWM的配置方法,涵盖CubeMX图形化设置、HAL库代码调用及常见问题排查。特别针对L6230Q驱动芯片的使能逻辑和死区时间配置提供实用指南,帮助开发者高效实现无刷电机的高性能控制。

weixin_27027459的博客 442

链式前向星教学

链式前向星:这是一种存图结构,也被称为静态建表存图的方式,它是一种在时间和空间上最优的存图结构。无论是建图还是遍历图效率最高的存图的方式。 数据结构: struct Edgenode{ int to; int w; int next; }; int head[n]; EdgeNode Edges[m]; Edgendoe结构体中,有三个变量,第一个变量to,代表这条边的终点在哪,

PK__PK的博客 2414

第二十届全国大学生智能车竞赛-地瓜机器人智慧医疗挑战赛技术解析与实战指南

本文深入解析了第二十届全国大学生智能车竞赛-地瓜机器人智慧医疗挑战赛的核心技术。内容涵盖从零搭建数字孪生环境、实现AI视觉任务(二维码识别、引导线追踪、目标检测)、在边缘设备部署轻量化大模型进行图生文描述,到遥操作与自主控制的平滑切换,并提供了基于ROS2的系统集成与赛场实战指南,助力参赛者高效备赛。

weixin_29252171的博客 2769

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

nbiot和2g_浅谈NBIOT

一、什么是NBIOT?1、概念窄带物联网(Narrow Band Internet of Things, NB-IoT),NB-IoT构建于蜂窝网络,只消耗大约180KHz的带宽,使用License频段,可采取带内、保护带或独立载波等三种部署方式,与现有网络共存。可直接部署于GSM网络、UMTS网络或LTE网络,以降低部署成本、实现平滑升级。2、NBIOT的特点和目标(1)超强覆盖:相对GPRS来...

weixin_39534100的博客 1704

MySQL 分库分表及其平滑扩容方案

MyCAT不仅仅可以用作读写分离,以及分表分库、容灾管理,而且可以用于多租户应用开发、云平台基础设施,让你的架构具备很强的适应性和灵活性。分表对于一张多行(记录)多列(字段)的二维数据表,又分两种情形(1)垂直分表竖向切分,不同分表存储不同的字段,可以把不常用或者大容量、或者不同业务的字段拆分出去;扩容前每个节点的数据,有一半要迁移至一个新增节点中,对应关系比较简单。此时,四个节点的数据都已完整,只是有冗余(多存了和自己配对的节点的那部分数据),择机清除即可(过后随时进行,不影响业务)。...

m0_71777195的博客 1929

CodeForces - 580C Kefa and Park 无向图 树 遍历dfs

Training 5 - C题 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. Ve...

xukeke12138的博客 257

达梦数据库与sharding-jdbc的深度适配实践

本文详细介绍了达梦数据库与sharding-jdbc的深度适配实践,包括驱动与依赖管理、核心适配实现、注册与配置等关键步骤。通过扩展shardingSphere的方式,成功实现了达梦数据库的分库分表功能,并提供了常见问题解决方案和性能优化建议,助力企业级应用高效处理海量数据。

weixin_30300225的博客 623

P1118 数字三角形游戏

有这么一个游戏: 写出一个1~N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直到只剩下一个数字位置。下面是一个例子:     3   1   2   4       4   3   6         7   9          16 最后得到16这样一个数字。 现在想要倒着玩这样一个游戏

ysCover 1542

基于Django+Vue3的智能目标检测系统设计与实现,Vue3前台系统+Django后台管理系统开发,Web前后端分离,YOLOv8 Web目标检测,实现图片、视频和摄像头检测等功能,全网独发

目标检测是计算机视觉中的重要任务,广泛应用于安防监控、自动驾驶、智能家居等领域。YOLO系列模型由于其高效的检测速度和较高的准确率,成为目标检测任务的首选算法之一。本项目结合 YOLOv8 与 Django + Vue3,构建一个Vue3前台系统+Django后台管理系统开发,便于用户进行目标检测的操作和展示,实现对图片、视频实时目标检测,不仅可以用于大论文的工作量展示,还可以作为毕业设计。支持更换自己模型、图片检测、实时视频检测、置信度调节和IoU参数调节。同时支持目标检测、实例分割、关键点检测等任务

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 5431

YOLOv11 损失函数进阶:Varifocal Loss 让正负样本精度双升

创建self.alpha = alpha # 正样本权重系数self.gamma = gamma # 聚焦系数self.lambda_reg = lambda_reg # 回归损失权重# 分类损失:Varifocal Classification Loss# 正样本权重:基于预测置信度# 计算 BCE 损失并加权cls_loss = cls_loss.sum() / (cls_true.sum() + 1e-6) # 按正样本数归一化。

m0_71589036的博客 439

hihoCoder 买零食

时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho很喜欢在课间去小卖部买零食。然而不幸的是,这个学期他又有在一教的课,而一教的小卖部姐姐以冷若冰霜著称。第一次去一教小卖部买零食的时候,小Ho由于不懂事买了好一大堆东西,被小卖部姐姐给了一个“冷若冰霜”的眼神,食欲都下降了很多。 从那以后,小Ho就学乖了,去小卖部买东西只敢同时

ysCover 1338

【ARM Trace32(劳特巴赫) 高级篇 20 -- SNOOPer 使用介绍】

在 Lauterbach TRACE32 调试系统中,Snooper是一种特殊的监视工具,它能够在不中断处理器执行的情况下监视和记录特定内存地址的数据变化。这对于系统级调试、性能分析、数据流监视以及实时系统的问题追踪特别有用,尤其是在无法使用中断的情况下。当你设置了一个或多个Snooper时,TRACE32 硬件将不断地监视指定的内存地址,一旦发生了读写操作,就会将相关信息记录下来。这样你就可以看到某个地址的数据是什么时候以及如何被修改的,而不会影响目标系统的运行。

CodingCos的博客 2763

hdu 1427 速算24点(java,DFS,全排列)

速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5070    Accepted Submission(s): 1310 Problem Description 速算24点相信绝大多数人都玩过。就是随机给你

ysCover 1333

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

蓝桥杯 PREV-9 大臣的旅费(深搜)

历届试题 大臣的旅费   时间限制:1.0s   内存限制:256.0MB     问题描述 很久以前,T王国空前繁荣。为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市。 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首都直接或者通过其他大城市间接到达。同时,如果不重复经过大城市,从首都到达每个大城

ysCover 1317

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

poj 2676 Sudoku (基础DFS)

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15456   Accepted: 7574   Special Judge Description Sudoku is a very simple task. A square table wi

ysCover 1237

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Codeforces Round #313 (Div. 2) D. Equivalent Strings(简单搜索)

D. Equivalent Strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Today on a lecture about strings Gerald learned

ysCover 1189

poj 1011 Sticks (DFS+剪枝)

Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Description George took sticks of the same length and cut them randomly unt

ysCover 956

poj 1129 Channel Allocation (DFS 图的染色问题)

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13152   Accepted: 6721 Description When a radio station is broadcasting over a very large

ysCover 938

FZU 2196 Escape(BFS预处理+BFS搜索)

Problem 2196 Escape Accept: 123    Submit: 678 Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description 小明进入地下迷宫寻找宝藏,找到宝藏后却发生地震,迷宫各处产生岩浆,小明急忙向出口处逃跑。如果丢下宝藏,小明就能迅速离开迷宫,但小明并不想

ysCover 911

poj 3083 Children of the Candy Corn (DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10881   Accepted: 4678 Description The cornfield maze is a popular Halloween tre

ysCover 911

poj 1416 Shredding Company (DFS)

Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4595   Accepted: 2633 Description You have just been put in charge of developing a new sh

ysCover 908

hdu 1175 连连看 (简单DFS)

连连看                                                    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)                                                          

ysCover 877

poj 2488 A Knight's Journey (DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34660   Accepted: 11827 Description Background  The knight is getting bored of seeing t

ysCover 851

hdu 5547 Sudoku

Sudoku Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 710    Accepted Submission(s): 268 Problem Description Yi Sima was one of th

ysCover 850
上一篇: Codeforces Round #321 (Div. 2)B. Kefa and Company
下一篇: POJ 1426 Find The Multiple (dfs)
雨狮子
博客等级 码龄12年 24粉丝 185原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值