题目链接:

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

B. Work Group

time limit per test2 seconds
memory limit per test256 megabytes

问题描述

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.

输入

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.

输出

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

样例输入

7

-1 3

1 2

1 1

1 4

4 5

4 3

5 2

样例输出

17

Note

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

题意

给你一颗树和点权,问你找出满足当x在集合里面时,它的后代的个数恰好为偶数个的内权值和最大的集合。

题解

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=2e5+10; /// dp[u][0]表示以u为根的子树节点数为偶数的合法最大值,
///同理dp[u][1]表示节点数为奇数的情况。
LL dp[maxn][2];
int n;
int arr[maxn]; VI G[maxn]; void dfs(int u){
if(G[u].sz()==0){
dp[u][0]=0;
dp[u][1]=arr[u];
return;
}
dp[u][0]=0;
///这个初始化不要写错!!!!!!
dp[u][1]=-INF;
rep(i,0,G[u].sz()){
int v=G[u][i];
dfs(v);
LL t0,t1;
t0=max(dp[u][0]+dp[v][0],dp[u][1]+dp[v][1]);
t1=max(dp[u][0]+dp[v][1],dp[u][1]+dp[v][0]);
dp[u][0]=t0;
dp[u][1]=t1;
}
dp[u][1]=max(dp[u][1],dp[u][0]+arr[u]);
} int main() {
scf("%d",&n);
int rt;
for(int i=1;i<=n;i++){
int p;
scf("%d%d",&p,&arr[i]);
if(p==-1) rt=i;
else G[p].pb(i);
} dfs(rt); prf("%I64d\n",max(dp[rt][0],dp[rt][1])); return 0;
} //end-----------------------------------------------------------------------

Notes

这题一开始就想到考虑每个点在和不在的情况,结果发现好难转移。。

其实这题有个特点就是状态之和子孙个数有关,而且只考虑奇偶性,所以应该要往这方面想的!。。

以后觉得状态很难转的时候可以考虑下问题的性质,对照自己的状态是否合适,是否有其他更好的状态表示。

VK Cup 2015 - Round 2 (unofficial online mirror, Div. 1 only) B. Work Group 树形dp的更多相关文章

  1. Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!

    VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM Time Lim ...

  2. VK Cup 2015 - Round 2 (unofficial online mirror, Div. 1 only) E. Correcting Mistakes 水题

    E. Correcting Mistakes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  3. VK Cup 2012 Round 3 (Unofficial Div. 2 Edition)

    VK Cup 2012 Round 3 (Unofficial Div. 2 Edition) 代码 VK Cup 2012 Round 3 (Unofficial Div. 2 Edition) A ...

  4. VK Cup 2015 - Round 1 -E. Rooks and Rectangles 线段树最值+扫描线

    题意: n * m的棋盘, k个位置有"rook"(车),q次询问,问是否询问的方块内是否每一行都有一个车或者每一列都有一个车? 满足一个即可 先考虑第一种情况, 第二种类似,sw ...

  5. VK Cup 2015 - Round 1 E. Rooks and Rectangles 线段树 定点修改,区间最小值

    E. Rooks and Rectangles Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemse ...

  6. VK Cup 2015 - Round 2 E. Correcting Mistakes —— 字符串

    题目链接:http://codeforces.com/contest/533/problem/E E. Correcting Mistakes time limit per test 2 second ...

  7. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!

    Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...

  8. VK Cup 2017 - Round 1

    和FallDream组队瞎打一通--B两个人写的都挂了233,最后只剩下FallDream写的A和我写的C,最后我yy了个E靠谱做法结果打挂了,结束之后改了改就A了,难受. AC:AC Rank:18 ...

  9. VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集

    D. Restructuring Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

随机推荐

  1. 大数据学习--day12(内部类)

    内部类学习     定义在类的内部的类  叫做内部类     包含了内部类的类 叫做外部类 内部类的作用      内部类是为了 实现 java中 多继承而存在的      内部类 可以继承其他类   ...

  2. ACM1013:Digital Roots

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  3. # 20155308 2016-2017-2《Java程序设计》课堂实践项目 5月17日

    20155308 2016-2017-2<Java程序设计>课堂实践项目 5/17 本次因为git出现了问题,所以没有按时提交我的代码 问题一 在IDEA中对P145 MathTool.j ...

  4. 20155336 2016-2017-2《JAVA程序设计》第一周学习总结

    # 20155336  2016-2017-2<JAVA程序设计>第1周学习总结 ## 教材学习内容总结 开学的第一周,带着些许的欣喜和好奇,听完了老师的第一堂课.说心里话学习JAVA仿佛 ...

  5. BZOJ1096_仓库建设_KEY

    题目传送门 一道斜率优化的题目,加深了印象. 设sum[i]=∑p[i],S[i]=∑p[i]*x[i]. 暴力方程加前缀和优化: f[i]=min(f[j]+c[i]+(sum[i]-sum[j]) ...

  6. High Water Mark 图示

    +---- high water mark of newly created table | V +-------------------------------------------------- ...

  7. 打豪车应用:uber详细攻略(附100元优步uber优惠码、uber优惠券、优步优惠码、优步优惠券)

    在嘀嘀打车和快的打车交战热闹的时候,美国的打车应用uber进入中国.与在美国以个人司机注册做 Uber 司机为主的模式不同,Uber 在中国采用与租车公司合作.由租车公司提供车辆和司机的模式,同时中文 ...

  8. 成都优步uber司机客户端下载-支持安卓、IOS系统、优步司机端Uberpartner

    国外打车软件优步乘客端大家在手机应用商店里都可以下载到,但是优步司机的App却不好找下载地址:这就跟滴滴打车一样,滴滴的乘客端是滴滴打车,而司机端是滴滴专车,司机版本在应用商店里都找不到,原因不清楚. ...

  9. day2 HTML - body

    <body>内常用标签 1.基本标签 所有标签分为: #  块级标签: div(白板),H系列(加大加粗),p标签(段落和段落之间有间距) # 行内标签: span(白板) 1. 图标,  ...

  10. LVS入门篇(五)之LVS+Keepalived实战

    一.实验架构和环境说明 (1)本次基于VMware Workstation搭建一个四台Linux(CentOS 7.4)系统所构成的一个服务器集群,其中两台负载均衡服务器(一台为主机,另一台为备机), ...