hdu5325记忆化深搜

来源:互联网 发布:在vb中cstr是什么意思 编辑:程序博客网 时间:2024/05/16 12:34

Crazy Bobo

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2068    Accepted Submission(s): 737


Problem Description
Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each node has a weightwi. 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(excludingui 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
73 30 350 100 200 300 4001 22 33 44 55 66 7
 

Sample Output
5
 

Author
ZSTU
 

Source
2015 Multi-University Training Contest 3
#include<cstdio>#include<iostream>#include<vector>#include<string>#include<string.h>#include<algorithm> #pragma comment(linker,"/STACK:1024000000,1024000000") using namespace std;const int MAXN=500010;int v[MAXN];int n;vector<int> e[MAXN];int num[MAXN];int ans;int h;int dfs(int fa){if(num[fa]) return num[fa];num[fa]=1;for(int i=0;i<e[fa].size();i++){int son=e[fa][i];num[fa]+=dfs(son);ans=max(ans,num[fa]);}return num[fa];}int main(){while(scanf("%d",&n)!=EOF){for(int i=1;i<=n;i++)scanf("%d",&v[i]);int a,b;for(int i=0;i<n-1;i++){scanf("%d%d",&a,&b);if(v[a]<v[b]){e[a].push_back(b);} else{e[b].push_back(a);}}ans=1;memset(num,0,sizeof(num));for(h=1;h<=n;h++)dfs(h);printf("%d\n",ans);for(int i=1;i<=n;i++)e[i].clear();}}


 
原创粉丝点击