533B - Work Group (dp-树节点奇偶)

B. Work Group
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One Big Software Company has n employees numbered from 1 to n. The director is assigned number 1. Every employee of the company except the director has exactly one immediate superior. The director, of course, doesn't have a superior.

We will call person a a subordinates of another person b, if either b is an immediate supervisor of a, or the immediate supervisor of a is a subordinate to person b. In particular, subordinates of the head are all other employees of the company.

To solve achieve an Important Goal we need to form a workgroup. Every person has some efficiency, expressed by a positive integer ai, where i is the person's number. The efficiency of the workgroup is defined as the total efficiency of all the people included in it.

The employees of the big software company are obsessed with modern ways of work process organization. Today pair programming is at the peak of popularity, so the workgroup should be formed with the following condition. Each person entering the workgroup should be able to sort all of his subordinates who are also in the workgroup into pairs. In other words, for each of the members of the workgroup the number of his subordinates within the workgroup should be even.

Your task is to determine the maximum possible efficiency of the workgroup formed at observing the given condition. Any person including the director of company can enter the workgroup.

Input

The first line contains integer n (1 ≤ n ≤ 2·105) — the number of workers of the Big Software Company.

Then n lines follow, describing the company employees. The i-th line contains two integers pi, ai (1 ≤ ai ≤ 105) — the number of the person who is the i-th employee's immediate superior and i-th employee's efficiency. For the director p1 =  - 1, for all other people the condition 1 ≤ pi < i is fulfilled.

Output

Print a single integer — the maximum possible efficiency of the workgroup.

Sample test(s)
Input
7
-1 3
1 2
1 1
1 4
4 5
4 3
5 2
Output
17
Note

In the sample test the most effective way is to make a workgroup from employees number 1, 2, 4, 5, 6.

 

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

题意:给你n个节点,每个节点已知父亲和权值,1节点为根必选,你从中挑选节点,以每个节点为父亲的子节点个数和必须为偶数,问最大的权值和

思路:在代码里了

代码:

 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define INF 1000000  //设置的比权值小就行了
typedef long long LL;
const int N = 200005;
int fa[N], a[N];
LL dp[N][2];//dp[i][0]代表以i为根的单层树个数和为偶数,另一个为奇数
int main()
{
    int n;
    //freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d%d",&fa[i],&a[i]);
        dp[i][0] = 0;
        dp[i][1] = -INF;
    }
    for(int i=n; i>0; i--)
    {
        dp[i][1] = max(dp[i][1],dp[i][0]+a[i]);
        if(i==1)break;
        LL n0,n1;
        n0 = max(dp[fa[i]][0]+dp[i][0],dp[fa[i]][1]+dp[i][1]);
        n1 = max(dp[fa[i]][1]+dp[i][0],dp[fa[i]][0]+dp[i][1]);
        dp[fa[i]][0] = n0;
        dp[fa[i]][1] = n1;
    }
    printf("%I64d\n",dp[1][1]);
}
View Code

 

posted @ 2015-05-28 16:33  Doli  阅读(304)  评论(0)    收藏  举报