zoukankan      html  css  js  c++  java
  • codeforces #530 D(Sum in the tree) (树上贪心)

    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表示,让你求最终所有点权之和(要求最小)。

    思路:奇数层的信息都是已知的,我们只需要求出偶数层的就行了,只需要使得偶数层节点的s[i]等于其所有子节点s[i]的最小值就行了

    #include <cstdio>
    #include <map>
    #include <iostream>
    #include<cstring>
    #include<bits/stdc++.h>
    #define ll long long int
    #define M 6
    using namespace std;
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
    int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
    const int inf=0x3f3f3f3f;
    const ll mod=1e9+7;
    struct node{
        int next;
        int to;
    };
    node edge[200007];
    int head[200007];
    ll v[200007];
    int cnt;
    ll ans;
    bool ff;
    void add(int u,int v){
        edge[++cnt].to=v;
        edge[cnt].next=head[u];
        head[u]=cnt;
    }
    void init(){
        cnt=0;
        ff=1;
        ans=0;
        memset(head,0,sizeof(head));
    }
    void dfs(int u,int f){
        if(v[u]==-1){     //如果是偶数层就统计最小子节点 
            ll minn=inf;
            for(int i=head[u];i!=0;i=edge[i].next){
                int to=edge[i].to;
                if(to==f) continue;
                minn=min(minn,v[to]);
            }
        //    cout<<minn<<endl;
            v[u]=minn;
        }
        for(int i=head[u];i!=0;i=edge[i].next){
            int to=edge[i].to;
            if(to==f) continue;
            dfs(to,u);
            if(v[to]==inf)  //如果叶子节点是偶层就等于上面一层 
             v[to]=v[u];
        }
    }
    void dfs_ans(int u,int f){
        if(!ff) return ;
        for(int i=head[u];i!=0;i=edge[i].next){
            int to=edge[i].to;
            if(to==f) continue;
            ll temp=v[to]-v[u];
            if(temp<0){   //如果有前缀和有矛盾就出现了错误 
                ff=0;
                return ;
            }
            ans+=temp;
            dfs_ans(to,u);
        }
    } 
    int main(){
        ios::sync_with_stdio(false);
        int n;
        while(cin>>n){
            init();
            for(int i=2;i<=n;i++){
                int t; cin>>t;
                add(t,i);
                add(i,t);
            }
            for(int i=1;i<=n;i++)
                cin>>v[i];
            dfs(1,0);
            ans+=v[1]; //先加上根结点 
            dfs_ans(1,0);
            if(!ff) cout<<"-1"<<endl;
            else cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Django组件——forms组件
    Django组件——分页器(paginator)
    Django和Ajax
    多表操作
    输入DStream和Receiver详解
    spark中streamingContext的使用详解
    spark与storm的对比
    spark1.5引进内置函数
    spark 分析sql内容再插入到sql表中
    spark之数据源之自动分区推断
  • 原文地址:https://www.cnblogs.com/wmj6/p/10414119.html
Copyright © 2011-2022 走看看