当前位置: 首页 > news >正文

1500*C. Kefa and Park(dfstree)

Kefa and Park - 洛谷 

Problem - 580C - Codeforces

Examples

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

 解析:

        dfs遍历,记录前一个结点权值是否为1,并且累计路径1的个数。        

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+5;
int n,m,a[N],res;
vector<int>e[N];
void dfs(int u,int cnt,int f,int fa){if(f) cnt+=a[u];else cnt=a[u];if(cnt>m) return;if(e[u].size()==1&&e[u][0]==fa) res++;for(int i=0;i<e[u].size();i++){if(e[u][i]!=fa) dfs(e[u][i],cnt,a[u],u);}
}
signed main(){scanf("%lld%lld",&n,&m);for(int i=1;i<=n;i++){scanf("%lld",&a[i]);}for(int i=1;i<n;i++){int x,y;scanf("%lld%lld",&x,&y);e[x].push_back(y);e[y].push_back(x);}dfs(1,0,0,-1);printf("%lld",res);return 0;
}

http://www.lryc.cn/news/178885.html

相关文章:

  • 【2023保研】双非上岸东南网安
  • Redis与Mybatis
  • MySQL架构 InnoDB存储引擎
  • K8S-CNI
  • Redis 集合类型(Set)和命令 (数据类型 四)
  • thinkphp5 如何模拟在apifox里面 post数据接收
  • 建造者模式 创建型模式之三
  • 发布以太坊测试网络中的第一笔交易
  • No module named ipykernel解决方案
  • Java 基于 SpringBoot 的校园疫情防控系统
  • windows的ui自动化测试相关
  • Mybatis 二级缓存(使用Ehcache作为二级缓存)
  • C语言 Cortex-A7核 IIC实验
  • 【每日一题】2769. 找出最大的可达成数字
  • 开源电子合同签署平台小程序源码 在线签署电子合同小程序源码 合同在线签署源码
  • 36 二叉树中序遍历
  • 广州华锐互动:VR结绳逃生训练模拟真实火灾场景,增强训练沉浸感
  • Flink安装及简单使用
  • QT信号槽
  • Spring Boot 技术架构图(InsCode AI 创作助手辅助)
  • python使用mitmproxy和mitmdump抓包在手机上抓包(三)
  • react create-react-app v5 从零搭建(使用 npm run eject)
  • 在微信小程序中跳转到另一个小程序(多种实现方式)
  • beanstalkd 启动跟停止【经常使用 nohup 和 配合来启动程序,如: nohup ./test 同时免疫SIGINT和SIGHUP信号】
  • 企业年报API的应用:从金融投资到市场研究
  • 基于Matlab实现评价型模型求解方法(附上源码+数据)
  • Prettier - Code formatter格式化规则文件
  • 用C++实现文件读写操作
  • 【我的创作纪念日】使用pix2pixgan实现barts2020数据集的处理(完整版本)
  • 背包算法(Knapsack problem)