Title Description
In this problem, given a value S and a tree. There is a positive integer at each node of the tree, I asked how many paths to reach the summing node S. The depth of the path to the node must be in ascending order. Suppose a node is the root node, the root depth is 0, the depth of its son nodes is 1. Path does not have to start from the root.
Input Format
The first line of two integers N and S, where N is the number of nodes in the tree. The second row is a positive integer N, an integer of i-th node i represents a positive integer. The next N-1 lines each x is an integer of 2 and y, x represents y is the son.
Output Format
Sum of the output path node S is the number of paths.
Sample input and output
Input:
. 3. 3
. 1 2. 3
. 1 2
. 1. 3
Output:
2
Description / Tips
To 100% of the data, N <= 100000, and the value of the ownership not more than 1000 S.
answer
This is a dd title!
There is more to be more dd dd!
n does not need to move the brain directly over the square
key points:
1. The root node
when the current value is larger than s break 2.
Code
#include <iostream>
#include <cstdio>
#define ll long long
#define R register
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
while (c>'9'||c<'0') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=(x<<1)+(x<<3)+(c^48);c=getchar();}
return x*f;
}
const int maxn=1e5+5;
int s,n,ans;
int a[maxn],fa[maxn];
void init(){
n=read(),s=read();
for (R int i(1);i<=n;++i) a[i]=read(),fa[i]=i;
for (R int i(1),x,y;i<n;++i) x=read(),y=read(),fa[y]=x;
}
inline int getroot(int u){
if (fa[u]==u) return u;
return getroot(fa[u]);
}
void doit(){
int rt=getroot(1);
for (R int i(1);i<=n;++i){
int sum=a[i];
if (sum==s) ans++;
if (sum>s) continue;
int j=i;
while (j!=rt){
j=fa[j];
sum+=a[j];
if (sum==s) ++ans;
if (sum>s) break;
}
}
printf("%d\n",ans);
}
int main(){
init();
doit();
return 0;
}