@

题面

链接

C. Kefa and Park

题意

求叶节点数量,叶节点满足,从根节点到叶节点的路径上最长连续1的长度小于m

题解

这道题目主要是实现,当不满足条件时直接返回。

到达叶节点后统计答案,用vector存图的话,无向图时,叶节点的边只有一条,也就是\(g[i].size()==1\)而不是0

需要特判是一条链的情况,一条链的话根节点的\(g[i].size()==1\)也成立

代码

#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back using namespace std;
const int N=1e5+10;
vector<int>g[N];
int a[N],ans,n,m; void dfs(int u,int fa,int sum,int maxx){
if(maxx>m){
return;
}
//统计答案
if(g[u].size()==1&&max(maxx,sum+a[u])<=m&&u!=1){
// cout<<"----------"<<u<<endl;
ans++;
return;
}
for(auto y:g[u]){
if(y==fa) continue;
if(a[u]==1){
if(a[fa]==1){
dfs(y,u,sum+1,max(maxx,sum+1));
}else{
dfs(y,u,1,max(maxx,1*1ll));
}
}else{
dfs(y,u,0,maxx);
}
}
} void solve()
{
cin>>n>>m;
rep(i,1,n){
cin>>a[i];
}
rep(i,1,n-1){
int u,v;cin>>v>>u;
g[u].pb(v);
g[v].pb(u);
}
//当前结点、根节点,目前连续猫数。
dfs(1,0,0,0);
cout<<ans<<endl;
} signed main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
// cin>>_;
// while(_--)
solve();
return 0;
}

总结

这道题目主要是dfs的实现,树的遍历,以及在遍历过程中维护相关信息。同时需要考虑一些细节,特殊情况比如树是一条链。

Codeforces Round 303 (Div. 2)C. Kefa and Park(DFS、实现)的更多相关文章

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

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

    dfs一遍,维护当前连续遇到的喵的数量,然后剪枝,每个统计孩子数量判断是不是叶子结点. #include<bits/stdc++.h> using namespace std; ; int ...

  3. 水题 Codeforces Round #303 (Div. 2) D. Queue

    题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...

  4. DP Codeforces Round #303 (Div. 2) C. Woodcutters

    题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...

  5. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  6. 水题 Codeforces Round #303 (Div. 2) A. Toy Cars

    题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...

  7. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

  8. Codeforces Round #321 (Div. 2) B. Kefa and Company 二分

    B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...

  9. Codeforces Round #321 (Div. 2) A. Kefa and First Steps 水题

    A. Kefa and First Steps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/58 ...

  10. Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp

    题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...

随机推荐

  1. CentOS使用iptables开放3000端口

    关闭firewall systemctl stop firewalld.service 禁止firewall开机启动 systemctl disable firewalld.service 设置ipt ...

  2. springboot集成swagger之knife4j实战(升级版)

    官方文档链接:https://doc.xiaominfo.com/ 一.Knifej和swagger-bootstrap-ui对比 Knife4j在更名之前,原来的名称是叫swagger-bootst ...

  3. python编程中,各种随机种子seed设置总结

    python随机种子seed的作用(强化学习常用到)_汀.的博客-CSDN博客先上代码import mathimport gymfrom gym import spaces, loggerfrom g ...

  4. 【二】tensorflow调试报错、TF深度学习强化学习教学

    相关文章: [一]tensorflow安装.常用python镜像源.tensorflow 深度学习强化学习教学 [二]tensorflow调试报错.tensorflow 深度学习强化学习教学 [三]t ...

  5. LyScript 实现Hook改写MessageBox

    LyScript 可实现自定义汇编指令的替换功能,用户可以自行编写一段汇编指令,将程序中特定的通用函数进行功能改写与转向操作,此功能原理是简单的Hook操作. 插件地址:https://github. ...

  6. 基于Hyper-V搭建免费桌面云

    Hyper-V 是 Microsoft 的硬件虚拟化产品. 它用于创建并运行计算机的软件版本,称为"虚拟机". 每个虚拟机都像一台完整的计算机一样运行操作系统和程序. 如果需要计算 ...

  7. CH32V208蓝牙从机sleep模式下功耗测试

    本测试基于CH32V208W的开发板:蓝牙从机模式:使用程序BLE_UART 在进行功耗测试的时候尽量去除额外耗电器件,将开发板上的VDD于VIO相连接,测功耗时直接给VDD供电. 将会对500ms, ...

  8. 48从零开始用Rust编写nginx,搭建一个简单又好看官方网站

    wmproxy wmproxy已用Rust实现http/https代理, socks5代理, 反向代理, 负载均衡, 静态文件服务器,websocket代理,四层TCP/UDP转发,内网穿透等,会将实 ...

  9. Hadoop-基础知识面试题

    1.Hadoop集群的最主要瓶颈 磁盘IO 2.Hadoop三大组件 (1).HDFS HDFS(Hadoop Distributed File System)是 Hadoop 项目的核心子项目,主要 ...

  10. C++——异常处理模块笔记

    异常处理是C++中的重要概念之一,用于处理在程序执行过程中可能发生的错误或异常情况.异常是指在程序执行过程中发生的一些不寻常的事件,例如除零错误.访问无效内存等.C++提供了一套异常处理机制,使得程序 ...