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 has a weight .
All the weights are distrinct.

A set with m nodes  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 ,(that
is, for
i from 1 to m-1).For any node  in
the path from  to (excluding  and ),should
satisfy .

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 ().
Then following a line contains n integers  (,all
the  is
distrinct).Each of the following n-1 lines contain 2 integers  and ,denoting
an edge between vertices  and  ().

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

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5326 5324 5323 5322 5321

/*

參考此人博客 :http://www.mamicode.com/info-detail-948802.html
记得用c++交

*/

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
using namespace std;

#define N 800005

vector<int>g[N];
int n;
int ans[N];
int a[N];

int dfs(int u)
{
    if(ans[u]) return ans[u];
    ans[u]=1;
    for(int i=0;i<g[u].size();i++)
    {
        int to=g[u][i];
        ans[u]+=dfs(to);
    }
    return ans[u];
}

int main()
{
    int i,j;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(i=1;i<=n;i++)
            g[i].clear();
        memset(ans,0,sizeof(ans));
        int u,v;
        i=n-1;
        while(i--)
        {
            scanf("%d%d",&u,&v);
            if(a[u]<a[v]) g[u].push_back(v);
            else g[v].push_back(u);
        }
        int temp=0;
        for(i=1;i<=n;i++)
        {
            temp=max(temp,dfs(i));
        }
        printf("%d\n",temp);
    }
    return 0;
}
时间: 2024-11-08 09:37:04

HDU 5325 Crazy Bobo(思路+dfs 记忆化)的相关文章

hdu 5325 Crazy Bobo dfs

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

HDU 1484 Basic wall maze (dfs + 记忆化)

Basic wall maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 168    Accepted Submission(s): 52 Special Judge Problem Description In this problem you have to solve a very simple maze consisti

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

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

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(深搜)

题意:给一个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

不要62 hdu 2089 dfs记忆化搜索

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中的 dfs 记忆化搜索方法解. 模板: int dfs(int i, int s, bool e) { if (i==-1) return s==target_s; if (!e && f[i][s] != -1) return f[i][s]; int res = 0; int u = e?

hdu 1501 Zipper (dfs+记忆化搜索)

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6491    Accepted Submission(s): 2341 Problem Description Given three strings, you are to determine whether the third string can be formed