@

题面

链接

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. Rocketmq学习4——Broker消息持久化原理源码浅析

    一丶前言 在<Rocketmq学习3--消息发送原理源码浅析>中,我们学习了消息发送的要点: 本地缓存+rpc 请求namesever + 定时刷新,topic路由信息 负载均衡的选择一个 ...

  2. 手撕Vuex-实现共享数据

    经过上一篇章介绍,完成了添加全局 $store,接下来就是实现共享数据的功能. 在 Vuex 中,共享数据是通过 state 来实现的,所以我们需要在 Nuex.js 文件中实现 state 的功能. ...

  3. 强化学习技巧三:Python多进程

    1.Python多进程模块 Python中的多进程是通过multiprocessing包来实现的,和多线程的threading.Thread差不多,它可以利用multiprocessing.Proce ...

  4. 月薪10K码农,跳槽到40K架构师,技术学习路线图汇总

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.介绍 Hey there! Roadmap to becoming a web devel ...

  5. P10009 [集训队互测 2022] 线段树 题解

    题目链接:P10009 [集训队互测 2022] 线段树 神仙分块题,先给一下出题人的神仙官解: 官解链接 前面还看得懂.后面是啥?这不是 ds 题咋和 dp.轮廓线扯上关系了.看了半天,还是这个启发 ...

  6. IntelliJ IDEA 在电脑上安装多个JDK 切换的方法

    在电脑上来回切换多个版本的JDK进行开发,方法很简单: 1.下载jdk 下载的时候不要下载安装包,而是下载zip包,这样直接解压就可,不污染注册表,难引起其他问题 2.解压后 把JDK配置到IDEA里 ...

  7. List大陷阱,这个问题,造成我的很多问题,我靠,今天才发现MyList.Duplicates := dupIgnore;不sort就无效。

    procedure TfrmMain.Button1Click(Sender: TObject); var MyLogisticsCompanyApi: TLogisticsCompanyApi; b ...

  8. CF1834

    A 给出一个由 \(1,-1\) 组成的序列.一次操作可以让一个数变相反. 要多少次操作,才能让整个序列和非负且积等于 \(1\). 大 氵题. B 定义两个数 \(A,B\) 有一个价值:每一位上的 ...

  9. windows网络流量监控

    NPCap 官网 https://nmap.org/npcap/ 这是抓包必须先安装的工具,具体的原因可以看 https://github.com/buger/goreplay/wiki/Runnin ...

  10. 【Unity3D】人机交互Input

    1 前言 ​ Input 是 Unity3D 中用于人机交互的工具类,用户可以调用其 GetKey.GetMousePosition.GetMouseButton.GetAxis.GetButton ...