题意与分析(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)的更多相关文章

  1. 「日常训练」Watering Flowers(Codeforces Round #340 Div.2 C)

    题意与分析 (CodeForces 617C) 题意是这样的:一个花圃中有若干花和两个喷泉,你可以调节水的压力使得两个喷泉各自分别以\(r_1\)和\(r_2\)为最远距离向外喷水.你需要调整\(r_ ...

  2. 「日常训练」Alternative Thinking(Codeforces Round #334 Div.2 C)

    题意与分析 (CodeForces - 603A) 这题真的做的我头疼的不得了,各种构造样例去分析性质... 题意是这样的:给出01字符串.可以在这个字符串中选择一个起点和一个终点使得这个连续区间内所 ...

  3. 「日常训练」More Cowbell(Codeforces Round #334 Div.2 B)

    题意与分析(CodeForces 604B) 题意是这样的:\(n\)个数字,\(k\)个盒子,把\(n\)个数放入\(k\)个盒子中,每个盒子最多只能放两个数字,问盒子容量的最小值是多少(水题) 不 ...

  4. 「日常训练」Ice Cave(Codeforces Round 301 Div.2 C)

    题意与分析(CodeForces 540C) 这题坑惨了我....我和一道经典的bfs题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是 ...

  5. 「日常训练」School Marks(Codeforces Round 301 Div.2 B)

    题意与分析(CodeForces 540B) 题意大概是这样的,有一个考试鬼才能够随心所欲的控制自己的考试分数,但是有两个限制,第一总分不能超过一个数,不然就会被班里学生群嘲:第二分数的中位数(科目数 ...

  6. 「日常训练」The Intriguing Obsession(CodeForces Round #439 Div.2 C)

    2018年11月30日更新,补充了一些思考. 题意(CodeForces 869C) 三堆点,每堆一种颜色:连接的要求是同色不能相邻或距离必须至少3.问对整个图有几种连接方法,对一个数取模. 解析 要 ...

  7. 「日常训练」Regular Bridge(Codeforces Round 306 Div.2 D)

    题意与分析 图论基础+思维题. 代码 #include <bits/stdc++.h> #define MP make_pair #define PB emplace_back #defi ...

  8. 「日常训练」Two Substrings(Codeforces Round 306 Div.2 A)

    题意与分析 一道非常坑的水题.分析醒了补. 代码 #include <bits/stdc++.h> #define MP make_pair #define PB emplace_back ...

  9. 「专题训练」Hard problem(Codeforces Round #367 Div. 2 C)

    题意与分析 题意:给出\(n\)个字符串,可以反转任意串,反转每个串都有其对应的花费\(c_i\).经过操作后是否能满足字符串\(\forall i \in [1,n] \text{且} i \in ...

随机推荐

  1. thrift C++ Centos 安装

    1.在官方下载thrift http://thrift.apache.org/download 这里下载thrift-0.11.0.tar.gz版本 2.如果想支持安装Cpp版本就需要先安装boost ...

  2. web.xml的加载过程是context-param >> listener >> fileter >> servlet

    web.xml的加载过程是context-param >> listener  >> fileter  >> servlet

  3. 1、ClassLoader.getResourceAsStream() 与Class.getResourceAsStream()的区别

    1.ClassLoader.getResourceAsStream() 与Class.getResourceAsStream()的区别 例如你写了一个MyTest类在包com.test.mycode ...

  4. loading等待效果

    效果预览:这两个球一直在转,不能进行其他操作 div放在最外层 <div id="loadingImg" style="height: 100%;width: 10 ...

  5. mongodb安装(比较全一点)

    Linux下MongoDB安装和配置详解  转:https://www.cnblogs.com/pfnie/articles/6759105.html 一.创建MongoDB的安装路径 在/usr/l ...

  6. HDU 2030 汉字统计(汉字Asics码为负,占两个char)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2030 汉字统计 Time Limit: 2000/1000 MS (Java/Others)    M ...

  7. c# 关闭socket的标准方法

    aSocket.Shutdown(SocketShutdown.Both); aSocket.Close(); c#关闭socket时,单独使用socket.close()通常会造成资源提前被释放,应 ...

  8. mysql获取正在运行的sql

    select id,db,host,time,info,command from information_schema.processlist where command<>'sleep' ...

  9. 个人开源Git地址

    开源Git地址 序号 Git地址 描述 1 https://github.com/winds-june 各种源码.直接调用的jar包          2    

  10. ZXing.net 生成和解析二维码

    nuget引用zxing.net包 public partial class Form1 : Form { public Form1() { InitializeComponent(); } priv ...