「日常训练」Kefa and Park(Codeforces Round #321 Div. 2 C)
题意与分析(CodeForces 580C)
给你一棵树,然后每个叶子节点会有一家餐馆;你讨厌猫(waht?怎么会有人讨厌猫),就不会走有连续超过m个节点有猫的路。然后问你最多去几家饭店。
这题我写的挫的要死,实际上只需要一次dfs就可以解决了,我愣是用了一次bfs+一次dfs来写——dfs是为了判断是否是叶子节点的。。。。但是bfs干的活完全可以让dfs在找叶子节点的时候顺带解决了,所以就很坑。
一个比较好的写法见https://www.cnblogs.com/qscqesze/p/4831507.html。
代码
#include <bits/stdc++.h>
#define MP make_pair
#define PB emplace_back
#define fi first
#define se second
#define ZERO(x) memset((x), 0, sizeof(x))
#define ALL(x) (x).begin(),(x).end()
#define rep(i, a, b) for (repType i = (a); i <= (b); ++i)
#define per(i, a, b) for (repType i = (a); i >= (b); --i)
#define QUICKIO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
using ll=long long;
using repType=ll;
const int MAXN=100000+5;
bool red[MAXN];
bool ok[MAXN];
bool vis[MAXN];
vector<int> G[MAXN],nG[MAXN];
int n;
int dfs(int x)
{
if(nG[x].size()==0)
{
//cout<<x<<endl;
return ok[x];
}
else
{
int ans=0;
rep(i,0,int(nG[x].size())-1)
ans+=dfs(nG[x][i]);
return ans;
}
}
int bfs(int m)
{
ZERO(ok);
queue<pair<int,int> > q;
q.push(MP(1,0));
vis[1]=true;
while(!q.empty())
{
auto now=q.front(); q.pop();
//cout<<now.fi<<" "<<now.se<<" "<<red[now.fi]<<endl;
if(now.se+red[now.fi]>m) continue;
else ok[now.fi]=true;
rep(i,0,int(G[now.fi].size())-1)
{
int v=G[now.fi][i];
if(!vis[v])
{
vis[v]=true;
nG[now.fi].PB(v);
if(red[now.fi])
q.push(MP(v,now.se+1));
else q.push(MP(v,0));
}
}
}
return dfs(1);
}
int main()
{
int m;
cin>>n>>m;
rep(i,1,n) cin>>red[i];
rep(i,1,n)
{
int x,y; cin>>x>>y;
G[x].PB(y);
G[y].PB(x);
}
ZERO(vis);
cout<<bfs(m)<<endl;
return 0;
}
「日常训练」Kefa and Park(Codeforces Round #321 Div. 2 C)的更多相关文章
- 「日常训练」Watering Flowers(Codeforces Round #340 Div.2 C)
题意与分析 (CodeForces 617C) 题意是这样的:一个花圃中有若干花和两个喷泉,你可以调节水的压力使得两个喷泉各自分别以\(r_1\)和\(r_2\)为最远距离向外喷水.你需要调整\(r_ ...
- 「日常训练」Alternative Thinking(Codeforces Round #334 Div.2 C)
题意与分析 (CodeForces - 603A) 这题真的做的我头疼的不得了,各种构造样例去分析性质... 题意是这样的:给出01字符串.可以在这个字符串中选择一个起点和一个终点使得这个连续区间内所 ...
- 「日常训练」More Cowbell(Codeforces Round #334 Div.2 B)
题意与分析(CodeForces 604B) 题意是这样的:\(n\)个数字,\(k\)个盒子,把\(n\)个数放入\(k\)个盒子中,每个盒子最多只能放两个数字,问盒子容量的最小值是多少(水题) 不 ...
- 「日常训练」Ice Cave(Codeforces Round 301 Div.2 C)
题意与分析(CodeForces 540C) 这题坑惨了我....我和一道经典的bfs题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是 ...
- 「日常训练」School Marks(Codeforces Round 301 Div.2 B)
题意与分析(CodeForces 540B) 题意大概是这样的,有一个考试鬼才能够随心所欲的控制自己的考试分数,但是有两个限制,第一总分不能超过一个数,不然就会被班里学生群嘲:第二分数的中位数(科目数 ...
- 「日常训练」The Intriguing Obsession(CodeForces Round #439 Div.2 C)
2018年11月30日更新,补充了一些思考. 题意(CodeForces 869C) 三堆点,每堆一种颜色:连接的要求是同色不能相邻或距离必须至少3.问对整个图有几种连接方法,对一个数取模. 解析 要 ...
- 「日常训练」Regular Bridge(Codeforces Round 306 Div.2 D)
题意与分析 图论基础+思维题. 代码 #include <bits/stdc++.h> #define MP make_pair #define PB emplace_back #defi ...
- 「日常训练」Two Substrings(Codeforces Round 306 Div.2 A)
题意与分析 一道非常坑的水题.分析醒了补. 代码 #include <bits/stdc++.h> #define MP make_pair #define PB emplace_back ...
- 「专题训练」Hard problem(Codeforces Round #367 Div. 2 C)
题意与分析 题意:给出\(n\)个字符串,可以反转任意串,反转每个串都有其对应的花费\(c_i\).经过操作后是否能满足字符串\(\forall i \in [1,n] \text{且} i \in ...
随机推荐
- 单独调用kindeditor的多图上传组件实现多图上传
本例是单独调用kindeditor多图上传的组件来进行多图上传,兼容性你懂得! 官方示例地址:http://kindeditor.net/ke4/examples/multi-image-dialog ...
- Java添加事件的几种方式(转载了codebrother的文章)
/** * Java事件监听处理——自身类实现ActionListener接口,作为事件监听器 * * @author codebrother */ class EventListener1 exte ...
- Python 学习笔记(十)Python集合(一)
回顾 int/float/str/list/tuple/dict 整数型和浮点型是不可变的,不是序列 字符串是不可变的,是序列 列表是可变的,是序列 元组是不可变的,是序列 字典是可变得,但不是序列 ...
- 使用 Solr 构建企业级搜索服务器
最近因项目需要一个全文搜索引擎服务, 在考察了Lucene及Solr后,我们选择了Solr. 本文简要记录了基于Solr搭建一个企业搜索服务器的过程.网上的资料太多千篇一律,也可能版本不同,总之在参照 ...
- Undefined symbols for architecture arm64: "_OBJC_CLASS_$XXX", referenced from: objc-class-ref in XXX
ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 ...
- webstorm如何支持markdown
首先安装markdown 插件 第一个即可.
- mac appstore应用下载失败,不能更新等等问题,都可以解决
打开终端,输入以下命令 open `getconf DARWIN_USER_CACHE_DIR`/com.apple.appstore 进入目录,删除 com.apple.appstore  重启系 ...
- js实现99乘法表的编写(双层for循环与递归方法)
双层for循环实现方法: function nine (num) { ; i <= num; i++){ var str = ''; ; k <= num; k++){ if(i > ...
- CentOS7 LNMP+phpmyadmin环境搭建(二、LNMP环境搭建)
上一篇博客我们在虚拟机上安装了centos7,接下来,就开始安装lnmp环境吧. 还是跟之前一样,进入命令行后,先使用su命令切换到root权限. 首先配置防火墙 CentOS 7.0默认使用的是f ...
- Debian中CodeIgniter+nginx+MariaDB+phpMyAdmin配置
本文不讲述软件安装过程,记述本人在Debia中配置CodeIgniter时遇到的问题及解决方法,希望能够为有需要的人提供帮助. 一.Debian版本及所需的软件 Debian 9.8 stretch ...