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 A1118 Birds in Forest (25 分)——并查集

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  2. 一键搭建LNMP脚本

    还有不足的地方,请谅解   2天时间刚做到安装mysql这里.... # [root@localhost ~]# cat /etc/centos-release # CentOS release 6. ...

  3. AI 前馈神经网络

    前馈神经网络(Feedforward Neural Network,简称FNN),也叫多层感知机(Multilayer Perceptron,简称MLP).FNN的目标是通过学习参数θ,得到最佳的函数 ...

  4. qt 程序中执行额外程序和脚本

    1.最简单的,我们可以通过system直接启动一个应用程序或者脚本:(但是要调用 #include <stdlib.h>) system("./helloworld") ...

  5. Android6.0权限大全和权限分类

    本文转载至: https://blog.csdn.net/qq_26440221/article/details/53097868 自从出了Android6.0权限管理之后,再也不能像以前那样粘贴复制 ...

  6. Matplotlib 简单图例

    图例参考:http://matplotlib.org/gallery.html API参考:http://matplotlib.org/api/pyplot_summary.html # -*- co ...

  7. CF97C Winning Strategy 构造、图论

    题目传送门:http://codeforces.com/problemset/problem/97/C 题意:给出$n$与一个范围在$[0,1]$内的递增序列$P_0-P_n$,试构造一个无穷序列$\ ...

  8. Flask核心机制--上下文源码剖析

    一.前言 了解过flask的python开发者想必都知道flask中核心机制莫过于上下文管理,当然学习flask如果不了解其中的处理流程,可能在很多问题上不能得到解决,当然我在写本篇文章之前也看到了很 ...

  9. OpenTracing:开放式分布式追踪规范

    前言 想实现一个简单的追踪系统似乎是容易的,需要必要的调用链id,时间戳等:想实现一款易用不侵入代码的追踪系统就很麻烦了,需要接触CLR和IL相关知识:即使你费劲心力做出了那些,如果性能不够好,也没有 ...

  10. 五年.net程序员Java学习之路

    大学毕业后笔者进入一家外企,做企业CRM系统开发,那时候开发效率最高的高级程序语言,毫无疑问是C#.恰逢公司也在扩张,招聘了不少.net程序员,笔者作为应届生,也乐呵呵的加入到.net程序员行列中. ...