HDU 5325 Crazy Bobo

对原来的边(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 (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.

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

2015 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;
}

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

时间: 2024-12-31 10:11:24

HDU 5325 Crazy Bobo的相关文章

hdu 5325 Crazy Bobo dfs

// hdu 5325 Crazy Bobo // // 题目大意: // // 给你一棵树,树上每一个节点都有一个权值w,选择尽可能多的节点, // 这些节点相互联通,而且依照权值升序排序之后得到节点编号, // 需相邻节点之间的随意节点都要大于较小的节点. // // 解题思路: // 对于每一对u,v,我们建一条这种边:权值小的像权值大的连一条边. // 这样,问题就转化为求以u最小的权值为根的子树上点集的最大值. // dfs就可以. // // // 感悟: // // 多校的一道题目

hdu 5325 Crazy Bobo 乱搞+搜索

Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 218    Accepted Submission(s): 60 Problem Description Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each node h

HDU 5325 CRAZY BOBO 排序

链接 Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 252    Accepted Submission(s): 74 Problem Description Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each no

HDU 5325 Crazy Bobo(思路+dfs 记忆化)

Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 612    Accepted Submission(s): 189 Problem Description Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each node

HDU 5325 Crazy Bobo(深搜)

题意:给一个n,接着输入n个数,表示n个点的值,接着输入n-1条边形成一个生成树 问最大有多少个点的集合使得该集合内的所有点都满足如下:对于集合内点大小相邻的两个点,该两点之间经过的所有点的大小都小于该两点 eg  7 3 30 350 100 200 300 400 1 2 2 3 3 4 4 5 5 6 6 7 该无向图可表示为1--2--3--4--5--6--7   取点6对于集合(3-7)来说,正好比他大的点为点3,且他们之间的所有点的 大小都小于该两点,满足条件   同理取该集合内的

DFS/BFS+思维 HDOJ 5325 Crazy Bobo

题目传送门 1 /* 2 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 3 在树上的路径权值都小于这两个点 4 DFS/BFS+思维:按照权值的大小,从小的到大的连有向边,搜索最多连接点数即是答案.因为排序后,他们之间的路径, 5 可定都是从当前节点u连过去的,那么都是小于这两个节点的.DFS需手动加栈,BFS类似拓扑排序的思路 6 */ 7 #pragma comment (linker, "/STACK:1024000000,10240000

HDOJ 5325 Crazy Bobo 树形DP

按照升序或者降序选择的点集可以满足条件..... 树上的每个节点可以从子节点转移,也可以从父亲节点转移 Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 325    Accepted Submission(s): 100 Problem Description Bobo has a tree,whose vert

HDU 3157 Crazy Circuits(有源汇上下界最小流)

HDU 3157 Crazy Circuits 题目链接 题意:一个电路板,上面有N个接线柱(标号1~N),还有两个电源接线柱 + -,给出一些线路,每个线路有一个下限值求一个可以让所有部件正常工作的总电流 没有则输出impossible 思路: 有源汇有上下界求最小流,建模方法为: 按无源汇先建图,跑超级源汇ss->tt一次,然后加入t->s,容量INF的边,在跑一次ss->tt,如果是满流,就有解,解为t->s边的当前流量 顺带写个最大流的,最大流就先把t->s加入直接跑

多校 hdu 5325

Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 1308    Accepted Submission(s): 400 Problem Description Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each nod