Codeforce 1098-A
Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root has index 11. Each vertex vv initially had an integer number av≥0av≥0 written on it. For every vertex vv Mitya has computed svsv: the sum of all values written on the vertices on the path from vertex vv to the root, as well as hvhv — the depth of vertex vv, which denotes the number of vertices on the path from vertex vv to the root. Clearly, s1=a1s1=a1 and h1=1h1=1.
Then Mitya erased all numbers avav, and by accident he also erased all values svsv for vertices with even depth (vertices with even hvhv). Your task is to restore the values avav for every vertex, or determine that Mitya made a mistake. In case there are multiple ways to restore the values, you're required to find one which minimizes the total sum of values avav for all vertices in the tree.
The first line contains one integer nn — the number of vertices in the tree (2≤n≤1052≤n≤105). The following line contains integers p2p2, p3p3, ... pnpn, where pipi stands for the parent of vertex with index ii in the tree (1≤pi<i1≤pi<i). The last line contains integer values s1s1, s2s2, ..., snsn (−1≤sv≤109−1≤sv≤109), where erased values are replaced by −1−1.
Output one integer — the minimum total sum of all values avav in the original tree, or −1−1 if such tree does not exist.
- 5
- 1 1 1 1
- 1 -1 -1 -1 -1
- 1
- 5
- 1 2 3 1
- 1 -1 2 -1 -1
- 2
- 3
- 1 2
- 2 -1 1
- -1
- 这道题时说有一颗树,树有N个节点,每个节点有一个值Vi,沿着根节点到I节点求和得到SUMi,前缀和,现在删去vi,且删去了偶数深度节点的sumi,vi>=0,sumi<=1e9.现是否存在一棵
这样的树使得各点vi的和相加最小,不存在就是,存在子节点sumi小于父节点sumi使得子节点vi为负值。当为偶数节点时,他要被做差分,他也要与父节点做差分,如果他有子节点时,
他们几个构成贪心的机制,代价=∑(sum[子节点]-sum[当前节点])+sum[当前节点]-sum[父节点] 可知当子节点大于1的时候sum[当前]越大越好,最大的符合条件的sum[当前]
是 sum【子节点】的最小值,当没有子节点时,显而易见等于父节点的sum【父节点】时代价最小,当只有一个子节点时,代价去sum【子】与sum【父】带价相同,归到第一类,可以写代码了。
- #include<bits/stdc++.h>
- #define Swap(a,b) a^=b^=a^=b
- #define cini(n) scanf("%d",&n)
- #define cinl(n) scanf("%lld",&n)
- #define cinc(n) scanf("%c",&n)
- #define speed ios_base::sync_with_stdio(0); // 切不可用scnaf;
- #define Max(a,b) a>b?a:b
- #define Min(a,b) a<b?a:b
- using namespace std;
- typedef long long ll;
- const int INF=0x3f3f3f3f;
- const int maxn=1e5+;
- int fa[maxn];
- ll sum[maxn];
- int main()
- {
- speed
- int n;
- ll ans=;
- cin>>n;
- for(int i=;i<=n;i++)
- {
- cin>>fa[i];
- }
- for(int i=;i<=n;i++)
- {
- cin>>sum[i];
- if(sum[i]==-) sum[i]=1e9+;
- // sum[i]=sum[i]==-1?1e9+1:sum[i];
- }
- // for(int i=1;i<=n;i++) cout<<sum[i]<<' '<<endl;
- // cout<<endl;
- for(int i=;i<=n;i++)
- {
- sum[fa[i]]=Min(sum[fa[i]],sum[i]);
- }
- // for(int i=1;i<=n;i++) cout<<sum[i]<<' '<<endl;
- for(int i=;i<=n;i++)
- {
- int temp=sum[i]-sum[fa[i]];
- if(temp<) return cout<<-<<endl,;
- if(sum[i]<=1e9) ans+=temp;
- }
- cout<<sum[]+ans<<endl;
- }
Codeforce 1098-A的更多相关文章
- code vs 1098 均分纸牌(贪心)
1098 均分纸牌 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有 N 堆纸牌 ...
- Codeforce - Street Lamps
Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...
- 【BZOJ】1098: [POI2007]办公楼biu(补图+bfs+链表)
http://www.lydsy.com/JudgeOnline/problem.php?id=1098 显然答案是补图连通块..... 想到用并查集...可是连补图的边都已经...n^2了...怎么 ...
- LightOj 1098 - A New Function(求1-n所有数的因子和)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1098 题意:给你一个数n (0 ≤ n ≤ 2 * 109),求n以内所有数的因子和, ...
- Codeforce Round #216 Div2
e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...
- HDU 1098 Ignatius's puzzle(数学归纳)
以下引用自http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=8466&messageid=2&deep=1 题意以 ...
- HDU 1098 Ignatius's puzzle
http://acm.hdu.edu.cn/showproblem.php?pid=1098 题意 :输入一个K,让你找一个a,使得f(x)=5*x^13+13*x^5+k*a*x这个f(x)%65等 ...
- [BZOJ 1098] [POI2007] 办公楼biu 【链表优化BFS】
题目链接:BZOJ - 1098 题目分析 只有两个点之间有边的时候它们才能在不同的楼内,那么就是说如果两个点之间没有边它们就一定在同一座楼内. 那么要求的就是求原图的补图的连通块. 然而原图的补图的 ...
- Codeforce 水题报告(2)
又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...
- codeforce 375_2_b_c
codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...
随机推荐
- 【Web】阿里icon图标webpack插件(webpack-qc-iconfont-plugin)详解
webpack-qc-iconfont-plugin webpack-qc-iconfont-plugin是一个webpack插件,可以轻松地帮你将阿里icon的图标项目下载至本地 开发初衷 之前已经 ...
- web系统安全运营之基础- 基于DFA算法的高性能的敏感词,脏词的检测过滤算法类(c#).
[概述]做好一个web系统的安全运维,除了常规的防注入,防入侵等,还有一个检测并过滤敏感词,脏词.. 这件事做得不好,轻则导致一场投诉或纠纷,重则导致产品被勒令关闭停运. 废话少说,先看下代码,可以 ...
- 在MAC上如何使用SQL Server
由于小编在这学期要学习数据库原理这门课程,需要用到SQL Server,然而大家都知道SQL Server目前是只能在Windows上使用,我们在mac电脑上如何使用呢?我们可以借助目前比较火的Doc ...
- 合理使用CSS框架,加速UI设计进程
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://dzone.com/articles/how-to-speed-up-your-d ...
- AJ学IOS(25)UI之触摸事件
AJ分享,必须精品 iOS中的事件 在用户使用app过程中,会产生各种各样的事件;iOS中的事件可以分为3大类型: 响应者对象–UIResponder 在iOS中不是任何对象都能处理事件,只有继承了U ...
- (转) 关于Windows CE和Windows Mobile
转发自http://www.cnblogs.com/chump/articles/1281955.aspx 一.Windows CE Windows CE是微软的嵌入式操作系统主要的一种,面世于199 ...
- day01,了解gcc
今天主要是学一下gcc 功能选项: 一. 1. gcc -E:表示预处理,把指令处理掉 2.gcc -o:改变目标文件名称 3. gcc -c: 表示只编译不链接(也就是不生成a.out) 4. g ...
- linux基础知识点扫描
1.tty:查看自己的虚拟终端 2.echo "你的服务器已经被我控制,请立刻打钱给我,账号12312312312314123421,否则后果自负!!!" > /dev/ ...
- vue2.x学习笔记(十二)
接着前面的内容:https://www.cnblogs.com/yanggb/p/12592256.html. 组件基础 组件化是vue的一个重要特性,也是vue学习中非常重要的一个知识点. 基础示例 ...
- Jmeter 使用正则表达式提取响应结果中的值
正则表达式提取的界面如下图: apply to: Main sample and sub-samples:作用于父节点取样器及对应子节点取样器Main sample only:仅作用于父节点取样器Su ...