Crazy Bobo
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 879 Accepted Submission(s): 264
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.
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.
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
Recommend
1 #include <stdio.h> 2 #include <string.h> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 7 vector<int> edg[500005]; 8 struct Node 9 { 10 int w; 11 int num; 12 }a[500050]; 13 14 int n; 15 16 bool cmp(Node i,Node o) 17 { 18 return i.w>o.w; 19 } 20 21 int dp[500005]; 22 23 int main() 24 { 25 int i,j,k; 26 int x,y; 27 while(scanf("%d",&n)!=EOF) 28 { 29 for(i=1;i<=n;i++) 30 edg[i].clear(); 31 for(i=1;i<=n;i++) 32 { 33 scanf("%d",&a[i].w); 34 a[i].num=i; 35 } 36 for(i=1;i<n;i++) 37 { 38 scanf("%d %d",&x,&y); 39 if(a[x].w<a[y].w) 40 edg[x].push_back(y); 41 else 42 edg[y].push_back(x); 43 } 44 sort(a+1,a+n+1,cmp); 45 int ma=1; 46 memset(dp,0,sizeof(dp)); 47 for(i=1;i<=n;i++) 48 { 49 int u=a[i].num; 50 dp[u]=1; 51 for(j=0;j<edg[u].size();j++) 52 { 53 int v=edg[u][j]; 54 dp[u]=dp[u]+dp[v]; 55 } 56 if(dp[u]>ma) 57 ma=dp[u]; 58 } 59 printf("%d ",ma); 60 61 /*printf("*** "); 62 for(i=1;i<=n;i++) 63 printf("%d ",a[i].num); 64 printf(" "); 65 for(i=1;i<=n;i++) 66 printf("%d ",dp[a[i].num]); 67 printf(" "); 68 for(j=0;j<edg[6].size();j++) 69 printf("%d ",edg[6][j]); 70 printf(" ");*/ 71 } 72 return 0; 73 }