zoukankan      html  css  js  c++  java
  • [十二省联考2019]春节十二响 贪心,启发式合并

    [十二省联考2019]春节十二响

    链接

    loj
    luogu

    思路

    考试的75分的(O(n^2))谢瓜了,菜的一批。
    直接堆启发式合并。反正就是随便写就过的那种。
    好菜啊

    代码

    // luogu-judger-enable-o2
    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int N=2e5+7;
    int read() {
        int x=0,f=1;char s=getchar();
        for(;s>'9'||s<'0';s=getchar()) if(s=='-') f=-1;
        for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
        return x*f;
    }
    int n,w[N];
    vector<int> G[N];
    priority_queue<int> q[N];
    void dfs(int u) {
        for(auto v:G[u]) {
            dfs(v);
            vector<int> a;
            if(q[v].size()>q[u].size()) swap(q[u],q[v]);
            while(!q[v].empty()) {
                a.push_back(max(q[v].top(),q[u].top()));
                q[v].pop(),q[u].pop();
            }
            for(auto val:a) q[u].push(val);
        }
        q[u].push(w[u]);
    }
    int main() {
        scanf("%d",&n);
        for(int i=1;i<=n;++i) scanf("%d",&w[i]);
        for(int i=2,x;i<=n;++i) scanf("%d",&x),G[x].push_back(i);
        dfs(1);
        ll tot=0;
        while(!q[1].empty()) tot+=q[1].top(),q[1].pop();
        cout<<tot<<"
    ";
        return 0;
    }
    
  • 相关阅读:
    Codeforces Round #380(div 2)
    Codeforces Round #378(div 2)
    Codeforces Round #379(div 2)
    CCPC2016合肥现场赛
    CCPC2016沈阳站
    HDU2222 Keywords Search__AC自动机
    poj2185Milking Grid
    POJ2961_kmp
    POJ 2406
    poj 2752Seek the Name, Seek the Fame
  • 原文地址:https://www.cnblogs.com/dsrdsr/p/10696371.html
Copyright © 2011-2022 走看看