BZOJ 2783: [JLOI2012]树

Description

把一个正整数分成一列连续的正整数之和。这个数列必须包含至少两个正整数。你需要求出这个数列的最小长度。如果这个数列不存在则输出-1。

Input

   第一行是两个整数N和S,其中N是树的节点数。
   第二行是N个正整数,第i个整数表示节点i的正整数。
   接下来的N-1行每行是2个整数x和y,表示y是x的儿子。

Output

   输出路径节点总和为S的路径数量。

Sample Input

3 3

1 2 3

1 2

1 3

Sample Output

2

HINT

对于100%数据,N≤100000,所有权值以及S都不超过1000。

分析

本来我是想用一颗平衡数来找前驱的。结果被人提醒了一发,发现可以用个栈来维护,因为全是正的,所以直接二分,满足单调性。

代码

#include <bits/stdc++.h>

#define N 100005

struct NOTE
{
    int to,next;
}e[N*2];

int head[N];
int st[N],Q[N],sum[N];
int top,ans;
int s,n;
int cnt=0;

void add(int x,int y)
{
    e[++cnt].to=y;e[cnt].next=head[x];head[x]=cnt;
    e[++cnt].to=x;e[cnt].next=head[y];head[y]=cnt;
}

void dfs(int dep,int now,int root)
{
    st[++top]=now;
    int need=std::lower_bound(st+1,st+top+1,now-s)-st;
    if(st[need]+s==now && need>0 && need<=top)
        ans++;
    for(int i=head[dep];i;i=e[i].next)
    {
        if(e[i].to==root)
            continue;
        dfs(e[i].to,now+sum[e[i].to],dep);  
    }
    top--;
}

int main()
{
    scanf("%d%d",&n,&s);
    for(int i=1;i<=n;i++)
        scanf("%d",&sum[i]);
    for(int i=1;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
    }   

    st[++top]=0;
    dfs(1,sum[1],0);

    printf("%d\n",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值