Codeforces 1099 D. Sum in the tree-构造最小点权和有根树 贪心+DFS(Codeforces Round #530 (Div. 2))
2 seconds
256 megabytes
standard input
standard output
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
题意就是给你一个有根树,然后给你每个节点的父亲,然后给你每个节点到根节点的点权之和,然后就是偶数深度的节点的到根节点的点权之和被抹去了,是-1,让你将这个树还原,然后求出所有节点的最小点权之和。
因为是隔一层就断了,所以直接直接给偶数深度节点爸爸赋值他儿子的值中最小的那个,然后,儿子-爸爸的值算出每个点的点权,最后加起来就可以了。如果节点的值是负数,就是不满足条件的,直接-1输出就可以了。我写的时候,wa了好久,搞不清楚为什么,不用开long long吧,我不开会wa,然后就是如果一个节点是直接连接根节点的,他没有儿子,那么我们可以给他赋值为0,我这里写早了,写到dfs出来时每个节点的点权之前了,这样算出来的肯定是不对的,就是直接0-根节点的权值,肯定是错的,当时想的是算完之后再处理这些特殊的点,但是手抖写的时候写挫了,改了就过了。
代码:
//D
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=2e5+; struct node{
int to,val,next;
}edge[maxn<<]; int head[maxn<<],dep[maxn],fa[maxn][],cnt;
int dis[maxn]; void add(int x,int y,int v){edge[++cnt].to=y,edge[cnt].val=v,edge[cnt].next=head[x],head[x]=cnt;}//存图 void dfs(int u,int fath)
{
dep[u]=dep[fath]+;
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].to,w=edge[i].val;
if(v!=fath){
dis[v]=w;
if(dep[u]%==){
dis[u]=min(dis[u],dis[v]);//找子节点的值的最小值
}
}
}
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].to,w=edge[i].val;
if(v!=fath){
dfs(v,u);
if(dis[v]!=inf)//如果不是特殊节点
dis[v]=dis[v]-dis[u];//当前点的点权就是自己-爸爸的
}
}
} int a[maxn],b[maxn]; int main()
{
int n;
cin>>n;
for(int i=;i<=n;i++)
cin>>a[i];
for(int i=;i<=n;i++){
cin>>b[i];
if(b[i]==-) b[i]=inf;
}
for(int i=;i<=n;i++){
add(a[i],i,b[i]);
add(i,a[i],b[i]);
}
dis[]=b[];
dfs(,);
for(int i=;i<=n;i++){
if(dis[i]==inf) dis[i]=;//如果是特殊节点,处理完还是没确定值的,就赋值为0
}
// for(int i=1;i<=n;i++)
// cout<<dis[i]<<endl;
ll ans=;
int flag=;
for(int i=;i<=n;i++){
if(dis[i]<){
flag=;break;
}
ans+=dis[i];
}
if(flag==) cout<<ans<<endl;
else cout<<-<<endl;
} /*
10
1 1 1 1 2 3 4 5 1
3 -1 -1 -1 -1 3 3 3 3 -1 3
*/
Codeforces 1099 D. Sum in the tree-构造最小点权和有根树 贪心+DFS(Codeforces Round #530 (Div. 2))的更多相关文章
- Codeforces Round #530 (Div. 2):D. Sum in the tree (题解)
D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结 ...
- Codeforces Round #530 (Div. 2) D. Sum in the tree 树上贪心
D. Sum in the tree 题意 给出一颗树,奇数层数的点有值,值代表从1到该点的简单路的权值的和,偶数层数的点权值被擦去了 问所有节点的和的最小可能是多少 思路 对于每一个-1(也就是值未 ...
- Codeforces Round #530 (Div. 1) 1098A Sum in the tree
A. Sum in the tree Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root ha ...
- Codeforces Round #530 (Div. 2) A,B,C,D
A. Snowball 链接:http://codeforces.com/contest/1099/problem/A 思路:模拟 代码: #include<bits/stdc++.h> ...
- Codeforces Round #530 (Div. 1)
A - Sum in the tree 就是贪心选尽量让上面的点权尽量大,那么对于偶数层的点,其到根节点的和即为所有儿子中的最大值. #include<bits/stdc++.h> usi ...
- Codeforces Round #530 (Div. 2) Solution
A. Snowball 签. #include <bits/stdc++.h> using namespace std; ], d[]; int main() { while (scanf ...
- Codeforces Round #530 (Div. 2)
RANK :2252 题数 :3 补题: D - Sum in the tree 思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可. 最后会有一些叶子节点依旧为 ...
- Codeforces 1099 C. Postcard-字符串处理(Codeforces Round #530 (Div. 2))
C. Postcard time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Codeforces 1099 B. Squares and Segments-思维(Codeforces Round #530 (Div. 2))
B. Squares and Segments time limit per test 1 second memory limit per test 256 megabytes input stand ...
随机推荐
- maven插件理解
maven插件的主要功能是对用到的jar包进行管理,jar包先从本地仓库中获取,如果没有找到,则从远处中央仓库下载(需要联外网).本地仓库中的jar包可供所有maven工程使用,属于公共模块. mav ...
- [洛谷P2852] [USACO06DEC]牛奶模式Milk Patterns
洛谷题目链接:[USACO06DEC]牛奶模式Milk Patterns 题目描述 Farmer John has noticed that the quality of milk given by ...
- [洛谷P4774] [NOI2018]屠龙勇士
洛谷题目链接:[NOI2018]屠龙勇士 因为markdown复制过来有点炸格式,所以看题目请戳上面. 题解: 因为杀死一条龙的条件是在攻击\(x\)次,龙恢复\(y\)次血量\((y\in N^{* ...
- python3 mysql 时间参数错误
一.环境 mac OS + python3.6 + pymysql 二.执行 1.语句 select count(user_id) from chezhubangapppp.yfq_user wher ...
- [LA3523/uva10195]圆桌骑士 tarjan点双连通分量+奇环定理+二分图判定
1.一个环上的各点必定在同一个点双连通分量内: 2.如果一个点双连通分量是二分图,就不可能有奇环: 最基本的二分图中的一个环: #include<cstdio> #include<c ...
- bzoj 1093 缩点+DP
首先比较明显的是如果存在一个半连通子图,我们将其中的环缩成点,那么该图仍为半连通子图,这样我们就可以先将整张图缩点,重新构图,新图为拓扑图,记录每个新的点表示的强连通分量中点的个数num[i],那么我 ...
- Java多线程学习(四)等待/通知(wait/notify)机制
转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79690279 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...
- Python模块学习 - psutil
psutil模块介绍 psutil是一个开源切跨平台的库,其提供了便利的函数用来获取才做系统的信息,比如CPU,内存,磁盘,网络等.此外,psutil还可以用来进行进程管理,包括判断进程是否存在.获取 ...
- java===java基础学习(1)---数据类型,运算,变量,常量
今天起开始了java的学习之路,主要学习了数据类型和运算,变量,常量.基本和python有很多相通的地方,所以看起来很容易上手.下面是学习笔记! package testbotoo; public c ...
- 【Educational Codeforces Round 22】
又打了一场EDU,感觉这场比23难多了啊…… 艹还是我太弱了. A. 随便贪心一下. #include<bits/stdc++.h> using namespace std; ,ans=- ...