Description
Input
Output
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
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int MAXN = 5e6+10; struct Edge { int to, nxt; }; Edge edge[MAXN]; int head[MAXN]; int w[MAXN]; int num[MAXN]; int cnt, ans; void addedge( int from, int to ) { edge[cnt].to = to; edge[cnt].nxt = head[from]; head[from] = cnt++; } int dfs( int u ) { if(num[u]) return num[u]; num[u] = 1; for(int i = head[u]; i != -1; i = edge[i].nxt) { int v = edge[i].to; num[u]+=dfs( v ); } return num[u]; } int main() { int n; while(scanf( "%d", &n ) == 1) { cnt = 0; memset( num, 0, sizeof num ); memset( head, -1, sizeof head ); for(int i = 1; i <= n; i++) scanf( "%d", &w[i] ); for(int i = 0; i < n - 1; i++) { int u, v; scanf( "%d%d", &u, &v ); if(w[u] < w[v]) addedge( u, v ); else addedge( v, u ); } ans = 0; for(int i = 1; i <= n; i++) ans = max( ans, dfs( i ) ); cout << ans << endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/maxichu/article/details/48026011