# computer

0
12

#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;}

vector<node>tree[10001];
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){
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找不到标识符？为什么，凭什么

<

0