VK Cup 2015 - Round 2 (unofficial online mirror, Div. 1 only) B. 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.

题目要求,每个根结点的下属(包含子结点的子结点)的个数为偶数个所能形成的最大集合的最大权值!

分析可以发现,对于每个满足条件的结点可 以分成两数奇结点(共有奇数个点),偶结点(共偶数个点)如图

VK Cup 2015 - Round 2 (unofficial online mirror, Div. 1 only) B. Work Group 树形dp_第1张图片

则得到三个递推式:

dp[root][0] = max(t0 + dp[child][0],t1 + dp[child][1]);偶结点加上个子偶结点仍是偶结点,奇结点加个子奇结点就是偶结点
dp[root][1] = max(t1 + dp[child][0],t0 + dp[child][1]);奇结点加上偶结点是奇结点,偶结点加上奇结点是奇结点
dp[root][1] = max(dp[root][1],dp[root][0]+val[root]);偶结点,加上自身结点形成奇结点

利用这三个递推公式,再用树形dp的方法,推出来就可以 了!

#define INF			100000000000000000
#define EPS			(double)1e-9
#define mod			1000000007
#define PI			3.14159265358979
//*******************************************************************************/
#endif
#define N 200005
#define MOD 1000000007
int n,val[N],pp;
vector<int> p[N];
long long dp[N][2];
long long Dfs(int root){
    dp[root][0] = 0;dp[root][1] = -INF;
    FI(p[root].size()){
        int child = p[root][i];
        Dfs(child);
        long long t0 = dp[root][0],t1 = dp[root][1];
        dp[root][0] = max(t0 + dp[child][0],t1 + dp[child][1]);
        dp[root][1] = max(t1 + dp[child][0],t0 + dp[child][1]);
    }
    dp[root][1] = max(dp[root][1],dp[root][0]+val[root]);
    return max(dp[root][0],dp[root][1]);
}
int main()
{
    while (S(n) != EOF)
    {
        FI(n){
            p[i+1].clear();
        }
        FI(n){
            S2(pp,val[i+1]);
            if(pp != -1)
                p[pp].push_back(i+1);
        }

        cout<<Dfs(1)<<endl;
    }
    return 0;
}


你可能感兴趣的:(VK Cup 2015 - Round 2 (unofficial online mirror, Div. 1 only) B. Work Group 树形dp)