码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5325 Crazy Bobo

时间:2015-07-28 23:16:36      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

对原来的边(u, v)  方向定为u->v当w[u] > w[v]

最大Set是max{u到达的点集合}

Crazy Bobo

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


Problem Description
Bobo 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.
 

Input
The input consists of several tests. For each tests:
The first line contains a integer n (1n500000). Then following a line contains n integers w1,w2,...,wn (1wi109,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 (1ai,bin).
The sum of n is not bigger than 800000.
 

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

Sample Input
7 3 30 350 100 200 300 400 1 2 2 3 3 4 4 5 5 6 6 7
 

Sample Output
5
 

Source
 
#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;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5325 Crazy Bobo

标签:

原文地址:http://blog.csdn.net/oilover/article/details/47111897

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!