Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
怎么这样改就对,这样改就错?大神快进来!#include<iostream> #include<cmath> #include<algorithm> #include<vector> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> using namespace std; #define maxn 6001 int father[maxn]; int visit[maxn]; int dp[maxn][2]; int n; int max(int a, int b){ return a > b ? a : b; } void dfs(int root){ int i; visit[root] = 1; for (i = 1; i <= n; i++){ if (!visit[i] && father[i] == root){ dfs(i); dp[root][0] +=max( dp[i][1],dp[i][0]); dp[root][1] += dp[i][0]; } } } int main(){ int i, j; while (~scanf("%d", &n)){ memset(visit, 0, sizeof(visit)); //wrong //right memset(father, -1, sizeof(father));//memset(father,0,sizeof(father)); memset(dp, 0, sizeof(dp)); for (i = 0; i < n; i++){ scanf("%d", &dp[i + 1][1]); } int root = 0; bool beg = 1; int c, f; while (scanf("%d %d", &c, &f),c||f){ father[c] = f; if (root == c || beg){ root = f; } } //Wrong //Right while (father[root]!=-1) //while(father[root]){ { // root = father[root];// root=father[root]; } //} dfs(root); int ans = max(dp[root][0], dp[root][1]); printf("%d\n", ans); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator