标签:
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 879 Accepted Submission(s): 264
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\n",ma); 60 61 /*printf("***\n"); 62 for(i=1;i<=n;i++) 63 printf("%d ",a[i].num); 64 printf("\n"); 65 for(i=1;i<=n;i++) 66 printf("%d ",dp[a[i].num]); 67 printf("\n"); 68 for(j=0;j<edg[6].size();j++) 69 printf("%d ",edg[6][j]); 70 printf("\n");*/ 71 } 72 return 0; 73 }
标签:
原文地址:http://www.cnblogs.com/cyd308/p/4685913.html