Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

怎么这样改就对,这样改就错?大神快进来!

Posted by jakeaaa at 2014-10-05 18:02:54 on Problem 2342
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator