标签:
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.
/*
大意:找到最长的上升序列(要求连在一起)
DP思想 dp[u] += dp[v]
从最长的开始找上升
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
int n;
int b[500010];
vector<int> G[500010];
int dp[500010];
struct edge{
int num, id;
}a[500010];
bool cmp(edge i, edge j)
{
return i.num < j.num;
}
int main()
{
int x, y;
while(~scanf("%d", &n)){
for(int i = 1; i < n ; i++)
G[i].clear();
for(int i = 1; i <= n ; i++){
scanf("%d", &a[i].num);
a[i].id = i;
}
for(int i = 1; i <= n; i++)
b[i] = a[i].num;
sort(a + 1, a + n + 1,cmp);
for(int i = 1; i < n ; i++){
scanf("%d%d", &x, &y);
G[y].push_back(x);
G[x].push_back(y);
}
int max1 = 1;
memset(dp, 0, sizeof(dp));
for(int i = n ; i >= 1; i--){
int u = a[i].id;
dp[u] = 1;
for(int j = 0 ; j < G[u].size(); j++){
int v = G[u][j];
if(b[v] > b[u]) {
dp[u] += dp[v];
// printf("%d\n", dp[u]);
}
}
max1 = max(dp[u], max1);
}
printf("%d\n", max1);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4685750.html