(DFS) Bzoj 2783

Source: Internet
Author: User

2783: [JLOI2012] Tree time limit:1 Sec Memory limit:128 MB
submit:551 solved:323
[Submit] [Status] [Discuss] Description sequence Submit file: Sequence.pas/c/cpp input File: sequence.in Output File: Sequence.out Problem Description:Divides a positive integer into a sequence of consecutive positive integers. This sequence must contain at least two positive integers. You need to ask for the minimum length of this sequence. If this sequence does not exist then output-1. Input Format:Each row consists of a positive integer n. Each file contains multiple lines, which are read in until the end of the file. output Format:For each n, the output line is the minimum length of the sequence.

The first line is two integers n and s, where n is the number of nodes in the tree.

The second line is n positive integers, and the I integer represents a positive integer for node i.

The next line of N-1 is 2 integers x and y, which means Y is the son of X.

Output format:

The number of paths that the output path node sums to S.

Input Sample:

Sample output:

3 3

1 2 3

1 2

1 3

2

Data range:

For 30% data, n≤100;

For 60% data, n≤1000;

For 100% data, n≤100000, ownership value, and s are not more than 1000.

Data range:For all data, n≤263.

This is JLOI2012 's T1, sent out just to complete the question.

=============================================================================================

In this problem, given a value of s and a tree. At each node of the tree there is a positive integer that asks how many paths the sum of the nodes reaches S. The depth of the nodes in the path must be ascending. Assuming node 1 is the root node, the root depth is 0, and its son node has a depth of 1. The path does not necessarily start at the root node.

Input

The first line is two integers n and s, where n is the number of nodes in the tree.

The second line is n positive integers, and the I integer represents a positive integer for node i.

The next line of N-1 is 2 integers x and y, which means Y is the son of X.

Output

The number of paths that the output path node sums to S.

Sample INPUT3 3

1 2 3

1 2

1 3
Sample Output2hint

For 100% data, n≤100000, ownership value and s are not more than 1000

#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath > #include <cstdlib> #include <algorithm> #include <queue> #include <vector> #include <set > #include <stack>using namespace std;set<int> st;vector<int> e[100005];int n,s,a[100005],in[    100005],sum[100005],ans;void dfs (int u) {if (St.find (sum[u]-s)!=st.end ()) ans++;    St.insert (Sum[u]);        for (int i=0;i<e[u].size (); i++) {int v=e[u][i];        SUM[V]=SUM[U]+A[V];    DFS (v); } st.erase (Sum[u]);}    int main () {int x, y;    scanf ("%d%d", &n,&s);    St.insert (0);    for (int i=1;i<=n;i++) scanf ("%d", &a[i]);        for (int i=1;i<n;i++) {scanf ("%d%d", &x,&y);        E[x].push_back (y);    in[y]++;    } int root;            for (int i=1;i<=n;i++) {if (in[i]==0) {root=i;        Break    }} Sum[root]=a[root];    DFS (root); printf ("%d\n", aNS); return 0;}

  

(DFS) Bzoj 2783

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.