zoukankan      html  css  js  c++  java
  • 【bzoj3252】攻略(长链剖分+贪心)

    传送门

    显然每次只会取当前最大的长链。
    那么每次直接将所有长链的权值扔到一个堆里面,最后取出(k)次即是最终答案。
    写法上类似于树链剖分:

    /*
     * Author:  heyuhhh
     * Created Time:  2020/6/11 9:49:25
     */
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <set>
    #include <map>
    #include <queue>
    #include <iomanip>
    #include <assert.h>
    #include <functional>
    #include <numeric>
    #define MP make_pair
    #define fi first
    #define se second
    #define pb push_back
    #define sz(x) (int)(x).size()
    #define all(x) (x).begin(), (x).end()
    #define INF 0x3f3f3f3f
    #define Local
    #ifdef Local
      #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
      void err() { std::cout << std::endl; }
      template<typename T, typename...Args>
      void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
      template <template<typename...> class T, typename t, typename... A> 
      void err(const T <t> &arg, const A&... args) {
      for (auto &v : arg) std::cout << v << ' '; err(args...); }
    #else
      #define dbg(...)
    #endif
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> pii;
    //head
    const int N = 2e5 + 5;
    
    int n, k;
    vector <int> G[N];
    int a[N];
    ll len[N];
    int bson[N];
    
    void dfs(int u, int fa) {
        ll Max = 0;
        for (auto v : G[u]) if (v != fa) {
            dfs(v, u);
            if (len[v] > Max) {
                Max = len[v];
                bson[u] = v;
            }
        }
        len[u] = len[bson[u]] + a[u];
    }
    
    int topf[N];
    
    void dfs2(int u, int top, int fa) {
        topf[u] = top;
        if (bson[u]) {
            dfs2(bson[u], top, u);
        }
        for (auto v : G[u]) {
            if (v == fa || v == bson[u]) {
                continue;
            }
            dfs2(v, v, u);
        }
    }
    
    void run() {
        cin >> n >> k;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        for (int i = 1; i < n; i++) {
            int u, v; cin >> u >> v;
            G[u].push_back(v);
            G[v].push_back(u);
        }
        dfs(1, 0);
        dfs2(1, 1, 0);
        priority_queue <ll> q;
        for (int i = 1; i <= n; i++) {
            if (topf[i] == i) {
                q.push(len[i]);
            }
        }
        ll ans = 0;
        while (k--) {
            ans += q.top(); q.pop();
        }
        cout << ans << '
    ';
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0); cout.tie(0);
        cout << fixed << setprecision(20);
        run();
        return 0;
    }
    
  • 相关阅读:
    APP内跳转链接用INTENT,但是用系统浏览器,在内部还是要webview
    MNIST练习
    Trigger_word_detection_v1a
    Neural_machine_translation_with_attention_v4a
    Operations_on_word_vectors_v2a
    Improvise_a_Jazz_Solo_with_an_LSTM_Network_v3a-2
    Dinosaurus_Island_Character_level_language_model_final_v3b
    Building_a_Recurrent_Neural_Network_Step_by_Step_v3b
    Sequence model
    Face_Recognition_v3a
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/13112319.html
Copyright © 2011-2022 走看看