mr_dot_convict

Tree-pruning-Hackerrank-mr.convict

Jul 6th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. using namespace std;
  10. #ifndef CONVICTION
  11. #pragma GCC      optimize ("Ofast")
  12. #pragma GCC      optimize ("unroll-loops")
  13. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  14. #endif
  15.  
  16. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  17. #define PREC     cout.precision (10); cout << fixed
  18. #define bg(x)    " [ " << #x << " : " << (x) << " ] "
  19. #define x        first
  20. #define y        second
  21. using ll = long long;
  22. using ff = long double;
  23. using pii = pair<int,int>;
  24.  
  25. #define debug(args...) \
  26. { \
  27. /* WARNING : do NOT compile this debug func calls with following flags: // \
  28.  * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  29.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  30.    stringstream _ss(_s); \
  31.    istream_iterator<string> _it(_ss); err(_it, args); \
  32. }
  33. void err(istream_iterator<string> it)
  34. {
  35.    it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  36. }
  37. template<typename T, typename... Args>
  38. void err(istream_iterator<string> it, T a, Args... args)
  39. {
  40.     cerr << fixed << setprecision(15)
  41.       << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  42.     err(++it, args...);
  43. }
  44.  
  45. const int N = (int)1e5 + 10, K = 200;
  46. const ll inf = (ll)1e18;
  47. ll dp[N][K];
  48. int n, k;
  49. int cnt[N];
  50. ll w[N];
  51. vector <vector<int>> adj;
  52.  
  53. void root_at(int u, int pr, vector <bool> &vis)
  54. {
  55.    vis[u] = true;
  56.    for (auto vit = adj[u].begin(); vit != adj[u].end(); ++vit) {
  57.       if (*vit == pr)
  58.       {
  59.          adj[u].erase(vit);
  60.          break;
  61.       }
  62.    }
  63.    for (int v : adj[u])
  64.       if (!vis[v]) root_at(v, u, vis);
  65. }
  66.  
  67. void dfs_freq(int u, vector <bool> &vis)
  68. {
  69.    vis[u] = true;
  70.    cnt[u] = 1;
  71.    for (int v : adj[u]) {
  72.       if (!vis[v])
  73.       {
  74.          dfs_freq(v, vis);
  75.          w[u] += w[v];
  76.          cnt[u] += cnt[v];
  77.       }
  78.    }
  79. }
  80.  
  81. void dfs (int u, vector <bool> &vis)
  82. {
  83.    vis[u] = true;
  84.    int nchild = (int)adj[u].size();
  85.    vector <vector<ll>> pref(nchild, vector <ll> (k+1));
  86.    dp[u][0] = 0;
  87.  
  88.    for (int rem = 1; rem <= k; ++rem) {
  89.       if (rem > cnt[u]) dp[u][rem] = inf;
  90.       else dp[u][rem] = w[u];
  91.    }
  92.  
  93.    for (int v : adj[u]) {
  94.       if (!vis[v]) dfs(v, vis);
  95.    }
  96.  
  97.    for (int rem = 0; rem <= k; ++rem) {
  98.       if (rem > cnt[u]) break; // very important
  99.       for (int i = 0; i < (int)adj[u].size(); ++i) {
  100.          int v = adj[u][i];
  101.          if (i == 0) {
  102.             for (int x = 0; x <= rem; ++x) {
  103.                pref[i][x] = dp[v][x];
  104.             }
  105.          }
  106.          else {
  107.             for (int x = 0; x <= rem; ++x) {
  108.                if (pref[i-1][rem-x] != inf && dp[v][x] != inf)
  109.                   pref[i][rem] = min(pref[i][rem], pref[i-1][rem-x] + dp[v][x]);
  110.             }
  111.          }
  112.       }
  113.       if (adj[u].size())
  114.          dp[u][rem] = min(pref[adj[u].size()-1][rem], dp[u][rem]);
  115.    }
  116. }
  117.  
  118. void solve() {
  119.    vector <bool> vis(n, false);
  120.    root_at(0, -1, vis);
  121.    vis.assign(n, false);
  122.    dfs_freq(0, vis);
  123.  
  124.    vis.assign(n, false);
  125.    dfs(0, vis);
  126.    ll ans = 0;
  127.    for (int i = 0; i <= k; ++i) {
  128.       ans = min(ans, dp[0][i]);
  129.    }
  130.    cout << w[0] - ans << '\n';
  131. }
  132.  
  133. void read() {
  134.    cin >> n >> k;
  135.    adj.assign(n, vector <int> ());
  136.  
  137.    for (int i = 0; i < n; ++i)
  138.       cin >> w[i];
  139.  
  140.    for (int i = 0; i < n - 1; ++i)
  141.    {
  142.       int u, v;
  143.       cin >> u >> v;
  144.       --u, --v;
  145.       adj[u].push_back(v);
  146.       adj[v].push_back(u);
  147.    }
  148. }
  149.  
  150. signed main()
  151. {
  152.    IOS; PREC;
  153.    read(), solve();
  154.  
  155.    return EXIT_SUCCESS;
  156. }
Add Comment
Please, Sign In to add comment