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
#define maxn 123456
ll fa[maxn],n,s[maxn],ans;
int main()
{
    scanf("%lld",&n);
    for(int i=2; i<=n; i++)
        scanf("%lld",&fa[i]);
    for(int i=1; i<=n; i++)
    {
        scanf("%lld",&s[i]);
        if(s[i]==-1)s[i]=inf;
    }
    ans=s[1];
    for(int i=2; i<=n; i++)
        s[fa[i]]=min(s[fa[i]],s[i]);
    for(int i=2; i<=n; i++)
    {
        if(s[i]==inf)s[i]=0;
        else
        {
            ans+=s[i]-s[fa[i]];
            if(s[i]-s[fa[i]]<0)
            {
                printf("-1\n");
                return 0;
            }
        }
    }
    printf("%lld\n",ans);
    return 0;
}

  

原文地址:https://www.cnblogs.com/SDUTNING/p/10234588.html

时间: 2024-08-03 10:57:19

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. 2)F Cookies (树形dp+线段树)

题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的时间.然后有先手和后手俩个人. ?先手可以这么操作:在规定总时间T到达某个节点然后一定要返回根节点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) (前三题题解)

总评 今天是个上分的好日子,可惜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 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 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

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

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

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿