hdu5325--dp+ Search--crazy Bobo

Source: Internet
Author: User

Bobo has a tree,whose vertices is conveniently labeled by,..., N.each node have a weightWi . All the weights is distrinct.
A set with M nodesv1,v2,.. . ,vm is a Bobo Set if:
-The subgraph of his tree induced by this set is connected.
-After we sort these nodes in set by their weights in ascending order,we getu1,u2,. . ,um , (That's,w ui< wu I+1 For I from 1 to m-1). For any nodexIn the path fromuI TouI+1 (excludinguI anduI+1 ), should satisfywx<wuI.
Your task is to find the maximum size of Bobo Set in a given tree.


Inputthe input consists of several tests. For each tests:
The first line contains a integer n (1≤n≤500000 ). Then following a line contains n integersW1,w2,.. . ,wn (1≤wi≤9 , all theWi is distrinct). Each of the following n-1 lines contain 2 integersai andbI , denoting an edge between verticesai andbI (1≤ai,bi≤n).
The sum of n is not bigger than 800000.


Outputfor each test output one line contains a integer,denoting the maximum size of Bobo Set.


Sample Input73 30 350 100 200 300 4001 22 33 44 55 66 7


Sample OUTPUT5


Source2015 multi-university Training Contest 3


Recommendwange2014 | We have carefully selected several similar problems for you:5324 5323 5322 5321 5320
/* Main idea: Find the longest ascending sequence (required to join together) DP thought Dp[u] + = Dp[v] from the longest start looking up * * #include <cstdio> #include <cstring> #include < Algorithm> #include <vector>using namespace std;int n;int b[500010];vector<int> g[500010];int dp[500010 ];struct edge{int num, id;} A[500010];bool CMP (Edge I, Edge j) {return i.num < J.num;}   int main () {int x, y;    while (~SCANF ("%d", &n)) {for (int i = 1; i < n; i++) g[i].clear ();        for (int i = 1; I <= n; i++) {scanf ("%d", &a[i].num);    A[i].id = i;    } for (int i = 1; I <= n; i++) b[i] = A[i].num;    Sort (A + 1, a + n + 1,cmp);        for (int i = 1; i < n; i++) {scanf ("%d%d", &x, &y);        G[y].push_back (x);    G[x].push_back (y);    } int max1 = 1;    memset (DP, 0, sizeof (DP));        for (int i = n; I >= 1; i--) {int u = a[i].id;        Dp[u] = 1;            for (int j = 0; J < G[u].size (); j + +) {int v = g[u][j];          if (B[v] > B[u]) {      Dp[u] + = Dp[v];        printf ("%d\n", Dp[u]);    }} max1 = Max (Dp[u], max1);   } printf ("%d\n", max1); } return 0;}

  

hdu5325--dp+ Search--crazy Bobo

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.