codeforces 533B
每个人有一个直接的领导,1是总裁,现在要找一个最大的集合,每个领导领导的人的数量都是偶数,问最大的值是多少。
#include
#include
#include
#include
#include
#define MAX 200007
#define INF (1<<29)
using namespace std;
typedef long long LL;
int n;
int a[MAX];
LL dp[MAX][2];
vector<int> e[MAX];
void add ( int u , int v )
{
e[u].push_back ( v );
}
void dfs ( int u )
{
if ( !e[u].size())
{
dp[u][1] = a[u];
dp[u][0] = 0;
return;
}
dp[u][1] = -INF;
dp[u][0] = 0;
LL t1,t0;
for ( int i = 0 ; i < e[u].size() ; i++ )
{
int v = e[u][i];
dfs ( v );
t1 = dp[u][1];
t0 = dp[u][0];
dp[u][1] = max ( t0 + dp[v][1] , t1 + dp[v][0] );
dp[u][0] = max ( t0 + dp[v][0] , t1 + dp[v][1] );
}
dp[u][1] = max ( dp[u][1] , dp[u][0] + a[u] );
}
int main ( )
{
int x,y;
while ( ~scanf ( "%d" , &n ))
{
for ( int i = 0 ; i < MAX ; i++ )
e[i].clear();
for ( int i = 1 ; i <= n ; i++ )
{
scanf ( "%d%d" , &x , &y );
if ( x != -1 )
add ( x , i );
a[i] = y;
}
dfs ( 1 );
printf ( "%I64d\n" , max ( dp[1][0] , dp[1][1]) );
}
}