URAL 1039 Anniversary Party (树形DP)

#include <stdio.h>
#define MAX_EMPLOYEES 6000
#define MAX(x, y) ( (x) > (y) ? (x) : (y) )

int numOfEmployees;
int convivialityRate[MAX_EMPLOYEES + 1];
typedef struct Subordinate{
	int subordinate;
	int next;
}Subordinate;
Subordinate SubordinateArray[MAX_EMPLOYEES + 1];
int SubordinateNum;
int head[MAX_EMPLOYEES + 1];
int visited[MAX_EMPLOYEES + 1];
int isRoot[MAX_EMPLOYEES + 1];
int convivialityArray[MAX_EMPLOYEES + 1][2];

void attendOrNot(int supervisor){
	convivialityArray[supervisor][1] = convivialityRate[supervisor];
	int i, subordinate;
	for (i = head[supervisor]; i != 0; i = SubordinateArray[i].next){
		subordinate = SubordinateArray[i].subordinate;
		if (visited[subordinate] == 0){
			visited[subordinate] == 1;
			attendOrNot(subordinate);
			convivialityArray[supervisor][1] += convivialityArray[subordinate][0];
			convivialityArray[supervisor][0] += MAX(convivialityArray[subordinate][0], convivialityArray[subordinate][1]);
		}
	}
}

int main(){
	//freopen("input.txt", "r", stdin);
    scanf("%d", &numOfEmployees);
	int i;
	for (i = 1; i <= numOfEmployees; i++){
		scanf("%d", &convivialityRate[i]);
		isRoot[i] = 1;
	}

	int subordinate, supervisor;
	while (scanf("%d%d", &subordinate, &supervisor) != EOF){
		isRoot[subordinate] = 0;
		if (subordinate == 0 && supervisor == 0)
			break;
		SubordinateNum++;
		SubordinateArray[SubordinateNum].subordinate = subordinate;
		SubordinateArray[SubordinateNum].next = head[supervisor];
		head[supervisor] = SubordinateNum;
	}

	for (i = 1; i <= numOfEmployees; i++)
		if (isRoot[i] == 1){
			visited[i] = 1;
			attendOrNot(i);
			printf("%d\n", MAX(convivialityArray[i][0], convivialityArray[i][1]));
			break;
		}

	return 0;
}

你可能感兴趣的:(dp,party,anniversary,树形,1039,ural)