Kefa and Park

CodeForces - 580C

一棵以1为根的树,树上有些点是红的。一个叶子是合法的当且仅当从根到它的路径上出现的连续红点个数不超过m。求有多少个叶子是合法的。Input
第一行两个整数n和m(2≤n ≤105,1≤m≤n) 
第二行n个整数0或1,如果是1,表示第i个点是红点。 
接下来n-1行,每行两个整数x和y,表示树上的一条边。Output输出满足条件的叶子节点数Examples

Input
4 1
1 1 0 0
1 2
1 3
1 4
Output
2
Input
7 1
1 0 1 1 0 0 0
1 2
1 3
2 4
2 5
3 6
3 7
Output
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的更多相关文章

随机推荐

  1. PAT A1132 Cut Integer (20 分)——数学题

    Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long int ...

  2. AI 深度生成模型

    深度生成模型 1.玻尔兹曼机

  3. Android学习之基础知识七—碎片的使用

    碎片(Fragment)是一种可以嵌入在活动中的UI片断,它能让程序更加合理和充分地利用大屏幕的空间,它与活动相似,可以简单的理解为一个迷你型的活动,它也有自己的生命周期.碎片在平板的应用非常广泛. ...

  4. 使用proxy来简单的实现一个观察者

    var obv = (function() { var cache = new Map(); var observe = function (proxy, fn) { if (!cache.has(p ...

  5. linux中断源码分析 - 概述(一)

    本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 关于中断和异常 一般在书中都会把中断和异常一起说明,因为它们具有相同的特点,同时也有不同的地方.在CPU里,中断 ...

  6. BZOJ 2784 时间流逝

    BZOJ 2784 时间流逝 古典概率论... 可以发现由于能量圈数量限制,所以所构成的必定为树状结构(即便是转成最小能量圈和能量圈权值和之后存在重复状态,但是每个状态的含义不同,而且不能自身转移自身 ...

  7. UWP简单示例(二):快速开始你的3D编程

    准备 IDE:Visual Studio 开源库:GitHub.SharpDx 入门示例:SharpDX_D3D12HelloWorld 为什么选择 SharpDx? SharpDx 库与 UWP 兼 ...

  8. GNU构建系统和AutoTools

    注:本篇博客是阅读文末[参考博客]的讲解所写,内容非原创,仅是学习笔记 1. 概述2. 不同视角的程序构建2.1 用户视角2.2 开发者视角3. 导图图片4. configure选项参考博客 1. 概 ...

  9. C_数据结构_栈

    # include <stdio.h> # include <malloc.h> # include <stdlib.h> typedef struct Node ...

  10. 2016-03-22 OneZero团队 Daily Scrum Meeting

    会议时间: 2016-03-22 9:33-9:57am 会议内容: 一.在原有Sprint Backlog基础上,我们加了亮点(摇一摇功能:随机选取一条记录在界面显示,以提醒主页君回忆) 需求分析图 ...