Codeforces 1099 D. Sum in the tree-构造最小点权和有根树 贪心+DFS(Codeforces Round #530 (Div. 2))

D. Sum in the tree

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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

Output one integer — the minimum total sum of all values avav in the original tree, or −1−1 if such tree does not exist.

Examples

input

Copy

5
1 1 1 1
1 -1 -1 -1 -1

output

Copy

1

input

Copy

5
1 2 3 1
1 -1 2 -1 -1

output

Copy

2

input

Copy

3
1 2
2 -1 1

output

Copy

-1

题意就是给你一个有根树,然后给你每个节点的父亲,然后给你每个节点到根节点的点权之和,然后就是偶数深度的节点的到根节点的点权之和被抹去了,是-1,让你将这个树还原,然后求出所有节点的最小点权之和。

因为是隔一层就断了,所以直接直接给偶数深度节点爸爸赋值他儿子的值中最小的那个,然后,儿子-爸爸的值算出每个点的点权,最后加起来就可以了。如果节点的值是负数,就是不满足条件的,直接-1输出就可以了。我写的时候,wa了好久,搞不清楚为什么,不用开long long吧,我不开会wa,然后就是如果一个节点是直接连接根节点的,他没有儿子,那么我们可以给他赋值为0,我这里写早了,写到dfs出来时每个节点的点权之前了,这样算出来的肯定是不对的,就是直接0-根节点的权值,肯定是错的,当时想的是算完之后再处理这些特殊的点,但是手抖写的时候写挫了,改了就过了。

代码:

 1 //D
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<cstdlib>
 8 using namespace std;
 9 typedef long long ll;
10 const int inf=0x3f3f3f3f;
11 const int maxn=2e5+10;
12
13 struct node{
14     int to,val,next;
15 }edge[maxn<<1];
16
17 int head[maxn<<1],dep[maxn],fa[maxn][30],cnt;
18 int dis[maxn];
19
20 void add(int x,int y,int v){edge[++cnt].to=y,edge[cnt].val=v,edge[cnt].next=head[x],head[x]=cnt;}//存图
21
22 void dfs(int u,int fath)
23 {
24     dep[u]=dep[fath]+1;
25     for(int i=head[u];i;i=edge[i].next){
26         int v=edge[i].to,w=edge[i].val;
27         if(v!=fath){
28             dis[v]=w;
29             if(dep[u]%2==0){
30                 dis[u]=min(dis[u],dis[v]);//找子节点的值的最小值
31             }
32         }
33     }
34     for(int i=head[u];i;i=edge[i].next){
35         int v=edge[i].to,w=edge[i].val;
36         if(v!=fath){
37             dfs(v,u);
38             if(dis[v]!=inf)//如果不是特殊节点
39                 dis[v]=dis[v]-dis[u];//当前点的点权就是自己-爸爸的
40         }
41     }
42 }
43
44 int a[maxn],b[maxn];
45
46 int main()
47 {
48     int n;
49     cin>>n;
50     for(int i=2;i<=n;i++)
51         cin>>a[i];
52     for(int i=1;i<=n;i++){
53         cin>>b[i];
54         if(b[i]==-1) b[i]=inf;
55     }
56     for(int i=2;i<=n;i++){
57         add(a[i],i,b[i]);
58         add(i,a[i],b[i]);
59     }
60     dis[1]=b[1];
61     dfs(1,0);
62     for(int i=1;i<=n;i++){
63         if(dis[i]==inf) dis[i]=0;//如果是特殊节点,处理完还是没确定值的,就赋值为0
64     }
65 //    for(int i=1;i<=n;i++)
66 //        cout<<dis[i]<<endl;
67     ll ans=0;
68     int flag=0;
69     for(int i=1;i<=n;i++){
70         if(dis[i]<0){
71             flag=1;break;
72         }
73         ans+=dis[i];
74     }
75     if(flag==0) cout<<ans<<endl;
76     else cout<<-1<<endl;
77 }
78
79 /*
80 10
81 1 1 1 1 2 3 4 5 1
82 3 -1 -1 -1 -1 3 3 3 3 -1
83
84 3
85 */

原文地址:https://www.cnblogs.com/ZERO-/p/10263767.html

时间: 2024-07-28 14:54:34

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号结点到根节点的权值和.但是有些si=-1,这就相当于丢失了当前结点的数据. 假设原本每个点的权值为ai,那么现在求sum{ai}的最小为多少,ai为非负数. 题解: 这题可以单独看每一条链上的s值,假设当前结点为u,儿子结点v,那么就有几种情况: 1.su==-1&&sv==-1,这种不用管,继

Codeforces Round #530 (Div. 1)

A - Sum in the tree 就是贪心选尽量让上面的点权尽量大,那么对于偶数层的点,其到根节点的和即为所有儿子中的最大值. #include<bits/stdc++.h> using namespace std; char gc() { // static char buf[100000],*p1,*p2; // return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin))?EOF:*p1++; return get

Codeforces Round #530 (Div. 2)

RANK :2252 题数 :3 补题: D - Sum in the tree 思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可. 最后会有一些叶子节点依旧为 INF 权值按0算即可,然后其他的权值计算为 它 - 它父亲的. 注意判断时候会出现父亲比儿子大的这种非法情况. #include<bits/stdc++.h> using namespace std; #define inf 0x7f7f7f7f #define ll long long #de

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 standard input output standard output Little Sofia is in fourth grade. Today in the geometry lesson she learned about segments and squares. On the way home

Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的时间.然后有先手和后手俩个人. ?先手可以这么操作:在规定总时间T到达某个节点然后一定要返回根节点1,期间可以选择吃掉某些节点上的某些饼干(前提是保证剩下的时间能够回到根节点): ?后手可以这么操作:在先手到达的位置和这个位置的孩子之间的连边选择一条让先手吃得更多的边摧毁掉,也可以跳过这个过程: 问

Codeforces Round #530 (Div. 2) E (树形dp+线段树)

链接: 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干需要花费bi时间,有两个玩家,玩家一可以移动到当前点的子节点也可以申请游戏结束返回根节点并吃沿途的饼干,玩家二可以删除当前点到儿子节点的一条边,走路和吃饼干都消耗时间,会给出一个总时间,在总时间内尽可能的多吃饼干,问最多能吃多少个? 思路: 由于是玩家一先手,那么最开始的最大边则不会被删除,但之后路途的最大边都会被玩家二删除,所以我们对于当前点我们需要求: 1.如果现在回头那么最多可以吃到多少饼干 2.向下

Codeforces Round #530 (Div. 2) (前三题题解)

总评 今天是个上分的好日子,可惜12:30修仙场并没有打... A. Snowball(小模拟) 我上来还以为直接能O(1)算出来没想到还能小于等于0的时候变成0,那么只能小模拟了.从最高的地方进行高度的模拟,如果遇到石头就去判断一下会不会小于0其他没有什么好说的了 代码 #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int

Codeforces Round #575 (Div. 3) B. Odd Sum Segments (构造,数学)

B. Odd Sum Segments time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are given an array a consisting of n integers a1,a2,-,an. You want to split it into exactly k non-empty non-intersecting

Codeforces Round #423 (Div. 2) C 思维,并查集 或 线段树 D 树构造,水

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction   思维,并查集 或 线段树 题意:一个字符串被删除了,但给出 n条信息,要还原出可能的字典序最小的字符串.信息有:字符串ti,ki个位置xi,表明原本的字符串在xi位置是以字符串ti开头的. tags:惨遭 fst,一开始把所有字符串都存下来,排序做的,结果爆内存了.. 方法1: 考虑并查集,对于字符串 ti,在位置xi,