当前位置: 首页 > 工具软件 > Bobo > 使用案例 >

HDU 5325 CRAZY BOBO 排序

松国兴
2023-12-01

链接

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


题意:
给定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;
}


 类似资料: