Codeforces 533B Work Group

时间:2021-06-02 14:34:38

http://codeforces.com/problemset/problem/533/B

题目大意:

每个人有一个直接的领导,1是总裁,现在要找一个最大的集合,每个领导领导的人的数量都是偶数,问最大的值是多少

思路:

dp:f[i][0]代表以i为根的子树,选出偶数个人的最大值,1反之。

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#define ll long long
ll f[][],v[],ans;
int tot,go[],next[],first[];
int n;
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
}
void dfs(int x){
f[x][]=-0x3f3f3f3f;
f[x][]=;
int pd=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
dfs(pur);pd=;
ll t0=f[x][],t1=f[x][];
f[x][]=std::max(f[pur][]+t0,f[pur][]+t1);
f[x][]=std::max(f[pur][]+t1,f[pur][]+t0);
}
if (!pd){
f[x][]=v[x];
f[x][]=;
return;
}
f[x][]=std::max(f[x][],f[x][]+v[x]);
}
int main(){
n=read();
for (int i=;i<=n;i++){
int x=read(),y=read();
if (x!=-)
insert(x,i);
v[i]=y;
}
dfs();
printf("%I64d\n",f[][]);
return ;
}