bzoj2783: [JLOI2012]树
乱搞可过。
注意是刚好等于S
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #include<set> using namespace std; int n,S,c[110000]; struct node { int x,y,next; }a[110000];int len,last[110000]; void ins(int x,int y) { len++; a[len].x=x;a[len].y=y; a[len].next=last[x];last[x]=len; } int sum; multiset<int>ss; void dfs(int x,int d) { if(ss.find(d-S)!=ss.end())sum++; ss.insert(d); for(int k=last[x];k;k=a[k].next) { int y=a[k].y; dfs(y,d+c[y]); } ss.erase(d); } int main() { freopen("a.in","r",stdin); freopen("a.out","w",stdout); scanf("%d%d",&n,&S); for(int i=1;i<=n;i++)scanf("%d",&c[i]); int x,y; len=0;memset(last,0,sizeof(last)); for(int i=1;i<n;i++) { scanf("%d%d",&x,&y); ins(x,y); } ss.insert(0);dfs(1,c[1]); printf("%d\n",sum); return 0; }
pain and happy in the cruel world.