codeforces580C
Kefa and Park
一棵以1为根的树,树上有些点是红的。一个叶子是合法的当且仅当从根到它的路径上出现的连续红点个数不超过m。求有多少个叶子是合法的。Input
第一行两个整数n和m(2≤n ≤105,1≤m≤n)
第二行n个整数0或1,如果是1,表示第i个点是红点。
接下来n-1行,每行两个整数x和y,表示树上的一条边。Output输出满足条件的叶子节点数Examples
4 1
1 1 0 0
1 2
1 3
1 4
2
7 1
1 0 1 1 0 0 0
1 2
1 3
2 4
2 5
3 6
3 7
2
Note
第一个样例 红点已经被标记出来了。叶子节点是2,3,4. 编号为2的叶子节点不合法
第二个样例: 叶子节点是4, 5, 6, 7.其中 6,7不合法.
sol:暴力dfs是O(n)的,dfs时多记一个变量表示当前连续几个红点了,当然答案也可以从父亲那里转移
判断是否是叶子就要多记两个变量入度和出度,因为是双向边,所以in和out都为1的就是叶子
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,M=;
int n,m,Cor[N];
namespace Tree
{
int tot=,Next[M],to[M],head[M];
int Num[N],Indeg[N],Outdeg[N];
inline void add(int x,int y)
{
Indeg[y]++;
Outdeg[x]++;
Next[++tot]=head[x];
to[tot]=y;
head[x]=tot;
return;
}
inline void dfs(int x,int fa,int cnt)
{
int i;
Num[x]=max(Num[x],cnt);
for(i=head[x];i;i=Next[i]) if(to[i]!=fa)
{
Num[to[i]]=max(Num[to[i]],Num[x]);
if(Cor[to[i]])
{
dfs(to[i],x,cnt+);
}
else
{
dfs(to[i],x,);
}
}
return;
}
inline int Solve()
{
int i,ans=;
Num[]=Cor[];
dfs(,,Num[]);
for(i=;i<=n;i++) if(Indeg[i]==&&Outdeg[i]==)
{
if(Num[i]<=m) ans++;
}
return ans;
}
}
int main()
{
int i;
R(n); R(m);
for(i=;i<=n;i++) R(Cor[i]);
for(i=;i<n;i++)
{
int x=read(),y=read();
Tree::add(x,y);
Tree::add(y,x);
}
Wl(Tree::Solve());
return ;
}
codeforces580C的更多相关文章
随机推荐
- 如何应用ML的建议-上
本博资料来自andrew ng的13年的ML视频中10_X._Advice_for_Applying_Machine_Learning. 遇到问题-部分(一) 错误统计-部分(二) 正确的选取数据集- ...
- golang 转换markdown文件为html
使用blackfriday go get -u gopkg.in/russross/blackfriday.v2 go: package markdown import ( "fmt&quo ...
- linux编程头文件所在路径的问题
一.问题引入 1.头文件与库 当我们在PC主机linux环境下(如ubuntu),编写linux应用程序,然后利用gcc来编译.在源代码的开始位置会写入头文件,那是因为我们使用了系统提供的库函数,例如 ...
- xshell替代工具finalShell
主要特性:1.多平台支持Windows,Mac OS X,Linux2.多标签,批量服务器管理.3.支持登录Ssh和Windows远程桌面.4.漂亮的平滑字体显示,内置100多个配色方案.5.终端,s ...
- Ionic app 通知在Moto 360 Watch上显示通知(2)
在前一篇文章中,我们已经将Wtach的环境测试成功,下面进入我们自己消息的接收. 1.安装JPush插件在我们的App中,这个具体步骤可以参考 Ionic 安装JPush过程 2.在App上的登录模块 ...
- mysql 通过慢查询日志查写得慢的sql语句
MySQL通过慢查询日志定位那些执行效率较低的SQL 语句,用--log-slow-queries[=file_name]选项启动时,mysqld 会写一个包含所有执行时间超过long_query_t ...
- C#_委托与事件
委托: 把方法当作参数进行传递 public delegate void AddDelegate(string name); public class Ad{ //addDelegate就是委托的一个 ...
- jackjson-databind-2.9.3 笔记
问题 客户端请求: {"skip":0,"take":10,"corpName":"","cityCode&q ...
- GlusterFS分布式存储数据的恢复机制(AFR)的说明
GlusterFSFS恢复数据都是基于副本卷来说的,GlusterFSFS复制卷是采用镜像的方式做的,并且是同步事务性操作.简单来说就是,某一个客户要写文件时,先把这个文件锁住,然后同时写两个或多个副 ...
- swift 各种学习
swift使用cocoapods引用oc第三方库 1. 创建桥接文件 2. 在主工程的 build Settings 搜索 bridge 设置 Objective-C Bridging Headi ...