| ||||||||||
| 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 | |||||||||
树形dp,拓扑排序遍历树#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
int n;
while (cin >> n)
{
vector<int> rate(n + 1);
vector<int> sup(n + 1);
vector<int> in(n + 1);
vector<vector<int>> dp(2, vector<int>(n + 1));
for (int i = 1; i <= n; ++i)
scanf("%d", &rate[i]);
int a, b;
while (scanf("%d %d", &a, &b) && a && b)
{
sup[a] = b;
in[b] += 1;
}
queue<int> q;
for (int i = 1; i <= n; ++i)
if (in[i] == 0)
q.push(i);
int x;
while (!q.empty())
{
x = q.front();
q.pop();
dp[1][x] += rate[x];
int sx = sup[x];
dp[1][sx] += max(dp[0][x], 0);
dp[0][sx] += max(0, max(dp[1][x], dp[0][x]));
in[sx] -= 1;
if (in[sx] == 0 && sx > 0)
q.push(sx);
}
cout << max(dp[0][x], dp[1][x]) << endl;
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator