poj 2342树形dp板子题1

时间:2023-03-09 20:31:27
poj 2342树形dp板子题1

http://poj.org/problem?id=2342

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
inline int read(){
int sum=,x=;
char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')
x=;
ch=getchar();
}
while(ch>=''&&ch<=''){
sum=(sum<<)+(sum<<)+(ch^),ch=getchar();
}
return x?sum:-sum;
}
inline void write(int x){
if(x<)
putchar('-'),x=-x;
if(x>)
write(x/);
putchar(x%+'');
}
int mi(int x,int y){
return x<y?x:y;
}
int ma(int x,int y){
return x>y?x:y;
}
const int M=6e3+;
vector<int>g[M];
int f[M],dp[M][]; void dfs(int u){
for(int i=;i<g[u].size();i++){
int v=g[u][i];
dfs(v);
dp[u][]+=ma(dp[v][],dp[v][]);
dp[u][]+=dp[v][];
}
}
int main(){
int n=read(); for(int i=;i<=n;i++){
dp[i][]=read();
}
while(true){
int x=read(),y=read();
if(!x&&!y)
break;
g[y].push_back(x);
f[x]=y;
}
int root=;
while(f[root]!=)
root=f[root];
dfs(root);
write(ma(dp[root][],dp[root][]));
return ;
}