多校 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 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

Author

ZSTU

Source

2015 Multi-University Training Contest 3

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cmath>
#include<stdlib.h>
#include<map>
#include<set>
#include<time.h>
#include<vector>
#include<queue>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define INF 0x3f3f3f3f
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))

#define maxn 500000 + 10

int w[maxn], a[maxn];

struct edge
{
    int v, next;
}e[maxn * 2];

int n;
int vis[maxn];
int head[maxn], tot;

void init()
{
    tot = 0;
    memset(vis, 0, sizeof(vis));
    memset(a, 0, sizeof(a));
    memset(head, -1, sizeof(head));
}

void adde(int u, int v)
{
    e[tot].v = v;
    e[tot].next = head[u];
    head[u] = tot++;
}

void dfs(int u)
{
    vis[u] = 1;
    a[u] = 1;
    for(int i=head[u]; i!=-1; i=e[i].next)
    {
        int v = e[i].v;
        //printf("%d--%d\n", u, v);
        if(!vis[v])
            dfs(v);
        a[u] += a[v];
    }
}

int main()
{
    while(~scanf("%d", &n))
    {
        init();
        for(int i=1; i<=n; i++)
            scanf("%d", w + i);
        for(int i=1; i<n; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            if(w[u] < w[v])
                adde(u, v);
            else if(w[u] > w[v])
                adde(v, u);
        }
        for(int i=1; i<=n; i++)
            if(!vis[i])
            dfs(i);

        int ans = -1;
        for(int i=1; i<=n; i++)
            ans = max(ans, a[i]);

        printf("%d\n", ans);
    }
    return 0;
}

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

时间: 2024-10-06 17:34:08

多校 hdu 5325的相关文章

hdu 5325 Crazy Bobo dfs

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

多校-HDU 5351 MZL&#39;s Border 数学

f[1] = 'b', f[2] = 'a', f[i] = f[i - 1] + f[i - 2] 斐波那契数列的字符串,给你n和m,前m位中,最长的前缀等于后缀的长度是多少.1≤n≤1000, 1≤m≤length(f[n]) 规律题,虽然我不知道为什么. 1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4 public class Main{ 5 //Scanner cin = Scanner(System.i

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

XYZ and Drops Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 430    Accepted Submission(s): 105 Problem Description XYZ is playing an interesting game called "drops". It is played on a r?

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 5305

Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1429    Accepted Submission(s): 725 Problem Description There are n people and m pairs of friends. For every pair of friends, they can ch

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,且他们之间的所有点的 大小都小于该两点,满足条件   同理取该集合内的