[wikioi]没有上司的舞会

树形DP。用F[k][0]和F[k][1]表示某节点不选和选了之后子树的最大值。那么:
f[i][0]=sigma(max(f[k][0],f[k][1]))
f[i][1]=sigma(f[k][0])+v[i]
解题中用了备忘录。一开始要先找树根。

#include <cstdio>

#include <iostream>

#include <algorithm>

#include <memory.h>

#include <vector>

#define MAX(a, b) a>b?a:b

#define LEN 6005

using namespace std;



int F[LEN][2]; // dp state, F[i][0] for i not selected, F[i][1] for i selected

int R[LEN];

bool isRoot[LEN];

int opt(int x, int y, vector<vector<int> > &tree) {

	if (F[x][y] != -1) {

		return F[x][y];

	}

	if (y == 0) {

		int sum = 0;

		for (int i = 0; i < tree[x].size(); i++) {

			sum += MAX(opt(tree[x][i], 0, tree), opt(tree[x][i], 1, tree));

		}

		F[x][y] = sum;

		return sum;

	}

	else if (y == 1) {

		int sum = R[x];

		for (int i = 0; i < tree[x].size(); i++) {

			sum += opt(tree[x][i], 0, tree);

		}

		F[x][y] = sum;

		return sum;

	}

	else return 0;

}



int main()

{

    memset(F, 0, sizeof(F));

	memset(R, 0, sizeof(R));

	memset(isRoot, true, sizeof(isRoot));

	int n;

    cin >> n;

	vector<vector<int> > tree(n+1);

	for (int i = 1; i <= n; i++) {

		cin >> R[i];

	}

	int x, y;

	for (int i = 1; i <= n-1; i++) {

		cin >> x >> y;

		tree[y].push_back(x);

		isRoot[x] = false;

	}

	cin >> x >> y; // skip 0, 0

	

	for (int i = 1; i <= n; i++) {

		for (int j = 0; j <= 1; j++) {

			F[i][j] = -1;

		}

	}

	int root = 0;

	for (int i = 1; i <= n; i++) {

		if (isRoot[i]) {

			root = i;

			break;

		}

	}

	int ans = MAX(opt(root, 0, tree), opt(root, 1, tree));

	cout << ans << endl;

    return 0;

}

  

你可能感兴趣的:(IO)