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

题意:

给定n个点的 有点权的 树,且每个点点权各不相同

问:

选择尽可能多的点。

将这些点按点权排序

sample中,若选择了点{3,4,5,6,7}

则按点权排序:{100,200,300,350,400}

则排序后相邻两个点间的路径上的点权都要小于这两个点的点权。

即{100,200}{200,300}, {300,350}, {350,400}的路径上的点权都要小于这两个点。

如:{350,400} 的路径是3-4-5-6-7, 则4-5-6的点权都要<min(350,400)

排个序然后点权大的先插进去。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
    char c; int sgn;
    if (c = getchar(), c == EOF) return 0;
    while (c != '-' && (c<'0' || c>'9')) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}
template <class T>
inline void pt(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) pt(x / 10);
    putchar(x % 10 + '0');
}
typedef long long ll;
typedef pair<int, int> pii;
const int N = 500000 + 10;

int n;
vector<int>G[N];
pii a[N];
int w[N], r[N];
int main() {
    while (cin>>n) {
        for (int i = 1; i <= n; i++) {
            G[i].clear();
            rd(a[i].first); a[i].second = i;
            w[i] = a[i].first;
        }
        for (int i = 1, u, v; i < n; i++) {
            rd(u); rd(v); G[u].push_back(v); G[v].push_back(u);
        }
        sort(a + 1, a + 1 + n);
        int ans = 1;
        for (int i = n; i; i--) {
            int id = a[i].second;
            int tmp = 1;
            for (auto v : G[id]) {
                if (w[id] > w[v])continue;
                tmp += r[v];
            }
            r[id] = tmp;
            ans = max(ans, tmp);
        }
        pt(ans); puts("");
    }
    return 0;
}

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

时间: 2024-11-03 01:20:01

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

对原来的边(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(思路+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 2000 ASCII码排序

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2000 题目大意:按各字符的ASCII码从小到大的顺序进行排序 注意格式哦!输出时字符中间用一个空格分开 1 #include<stdio.h> 2 int main() 3 { 4 char a,b,c,t; 5 while(scanf("%c%c%c",&a,&b,&c)!=EOF) 6 { 7 getchar(); 8 if(a>b) 9 {