HDU 5325 Crazy Bobo,hdu5325crazybobo

來源:互聯網
上載者:User

HDU 5325 Crazy Bobo,hdu5325crazybobo

對原來的邊(u, v)  方向定為u->v當w[u] > w[v]

最大Set是max{u到達的點集合}

Crazy BoboTime Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 382    Accepted Submission(s): 116


Problem DescriptionBobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each node has a weight wi. All the weights are distrinct.
A set with m nodes v1,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 get u1,u2,...,um,(that is,wui<wui+1 for i from 1 to m-1).For any node x in the path from ui to ui+1(excluding ui and ui+1),should satisfy wx<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 integers w1,w2,...,wn (1≤wi≤109,all the wi is distrinct).Each of the following n-1 lines contain 2 integers ai and bi,denoting an edge between vertices ai and bi (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 Input
73 30 350 100 200 300 4001 22 33 44 55 66 7
 
Sample Output
5
 
Source2015 Multi-University Training Contest 3 
#pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;typedef long long ll;#define prt(k) cout<<#k" = "<<k<<endl;const int N = 501000;int dp[N], g[N], w[N];struct edge{        int v, next;}e[N<<1];int head[N], mm;void add(int u, int v){        e[mm].v= v;        e[mm].next = head[u];        head[u] = mm++;}int pa[N];int up(int u){        if (~g[u]) return g[u];        g[u] = 1;        int v = pa[u];        if (w[u] > w[v]) g[u] = up(v) + 1;        return g[u];}int n;pair<int, int> p[N];int main(){        while (scanf("%d", &n)==1) {                for (int i=1;i<=n;i++) scanf("%d", w+i), p[i]=make_pair(w[i], i);                sort(p+1, p+n+1);                mm = 0; memset(head, -1, sizeof head);                for (int i=0;i<n-1;i++) {                        int u, v;                        scanf("%d%d", &u, &v);                        add(u, v);                        add(v, u);                }                memset(dp, -1, sizeof dp);                memset(g, -1, sizeof g);                pa[1] = 1;  g[1] = 1;                int ans = 1;                for (int i=n;i>=1;i--) {                        int u = p[i].second;                        dp[u] = 1;                        for (int j=head[u];~j;j=e[j].next) {                                int v = e[j].v;                                if (w[u] < w[v]) dp[u] += dp[v];                        }                        ans = max(ans, dp[u]);                }                printf("%d\n", ans);        }        return 0;}



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.