标签:des style blog http color java os io
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
5
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 6010; 18 int dp[maxn][2],w[maxn],n,in[maxn]; 19 vector<int>g[maxn]; 20 void dfs(int u,int fa){ 21 dp[u][1] = w[u]; 22 for(int i = 0; i < g[u].size(); i++){ 23 if(g[u][i] == fa) continue; 24 dfs(g[u][i],u); 25 dp[u][1] += dp[g[u][i]][0];//u来,g[u][i]不来 26 dp[u][0] += max(dp[g[u][i]][1],dp[g[u][i]][0]); 27 //u不来,g[u][i]来或者不来 28 } 29 } 30 int main() { 31 int i,j,u,v,tmp; 32 while(~scanf("%d",&n)){ 33 for(i = 1; i <= n; i++){ 34 scanf("%d",w+i); 35 g[i].clear(); 36 in[i] = 0; 37 dp[i][0] = dp[i][1] = 0; 38 } 39 for(i = 1; i; i++){ 40 scanf("%d %d",&u,&v); 41 if(u+v == 0) break; 42 g[u].push_back(v); 43 g[v].push_back(u); 44 in[v]++; 45 } 46 for(i = 1; i <= n; i++){ 47 if(!in[i]){dfs(i,-1);break;} 48 } 49 printf("%d\n",max(dp[i][0],dp[i][1])); 50 } 51 return 0; 52 }
BNUOJ 2461 Anniversary party,布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/crackpotisback/p/3916527.html