卑微的我又在用例題刷流量,嗚node
它居然說找不到max標識符??,我就寫上了……ios
這個樹形DP不太好想,首先得定義狀態,就像數學解題設x,y同樣函數
一個點遍歷的最大花費深度須要從上和下兩個方向尋找因此要找到它的子節點的最大花費和父節點中不通過它的最大花費spa
子節點好求,在代碼dfs1()函數,父節點的話判斷是否是最大花費進過這個結點,若是是還須要次大花費ci
狀態轉移方程即是max(price(下),price(上));而後price(上)=max(price(上上),price(上下(不通過此節點)));數學
順序須要好好考慮,先遍歷作出向下最大花費,而後經過dfs從上往下作出向上最大花費io
切記臨界條件(根節點)和初始化stream
接下來是代碼:遍歷
#include <iostream>
#include <vector>
using namespace std;
int dp[10001][3];
int n;
struct node{
int son,cost;
node(int a,int b):son(a),cost(b){}
};
int max(int a,int b){
return (a>b)?a:b;}while
vector<node>tree[10001];
void read(){
for(int i=1;i<=n;i++){
tree[i].clear();
dp[i][0]=dp[i][1]=dp[i][2]=0;
}
int s,price;
for(int i=2;i<=n;i++){
cin >> s >> price;
tree[s].push_back(node(i,price));
}
}
int dfs1(int father){
int one=0,two=0;
for(int i=0;i<tree[father].size();i++){
int price=dfs1(tree[father][i].son)+tree[father][i].cost;
if(price>one){
two=one;
one=price;
}
else if(price<=one&&price>two){
two=price;
}
}
dp[father][0]=one;
dp[father][1]=two;
return one;
}
void dfs2(int fa){
for(int i=0;i<tree[fa].size();i++){
node child=tree[fa][i];
if(dp[child.son][0]+child.cost==dp[fa][0]){
dp[child.son][2]=max(dp[fa][1],dp[fa][2])+child.cost;
}
else{
dp[child.son][2]=max(dp[fa][0],dp[fa][2])+child.cost;
}
dfs2(child.son);
}
}
int main()
{
while(cin >> n){
read();
dfs1(1);
dp[1][2]=0;
dfs2(1);
for(int i=1;i<=n;i++){
cout << max(dp[i][0],dp[i][2]) << endl;
}
}
return 0;
}
max找不到標識符?爲何,憑什麼