Codeforces 815C. Karen and supermarket "tree DP"

Source: Internet
Author: User

LINK

Ideas

First, we find that the dependency is a tree-shaped structure.
And then it's not very good to have a direct count of how much it cost to contribute.
Because the array cannot be opened
Then you can count the minimum cost of a subtree.

Pay attention to the statistical contribution of the time with the coupon answer can only be counted at number 1th.

Author:dream_maker#include<bits/stdc++.h>using namespace std;//-----------------------------------------  -----//typenametypedef Long Long ll;//convenient for#define fu (A, B, c) for (int a = B; a <= C; ++a) #define FD (A, B, c) for (Int. a = B; a >= C;--a) #define FV (A, b) for (int a = 0; a < (signed) b.size (); ++a)//inf of different Typenamec  onst int inf_of_int = 1e9;const ll inf_of_ll = 1e18;//fast Read and writetemplate <typename t>void read (T &x) {  bool W = 1;x = 0;  char C = GetChar ();  while (!isdigit (c) && c! = '-') c = GetChar ();  if (c = = '-') w = 0, c = getchar ();    while (IsDigit (c)) {x = (x<<1) + (x<<3) + C-' 0 ';  c = GetChar (); } if (!w) x =-X;}    Template <typename t>void Write (T x) {if (x < 0) {Putchar ('-');   x =-X;  } if (x > 9) Write (X/10); Putchar (x 10 + ' 0 ');} ----------------------------------------------const int N = 5010;struct edge{int v, NXT;} E[n << 1];int head[n], tot = 0;ll c[N], D[n], siz[n], n;ll dp[n][n][2], ans = 0, b;void add (int u, int v) {E[++tot] = (Edge) {V, head[u]}; Head[u] = tot;}  void Dfs (int u, int fa) {Siz[u] = 1;  DP[U][1][1] = C[u]-d[u];  Dp[u][1][0] = C[u];    for (int i = head[u]; i; i = e[i].nxt) {int v = E[I].V;    if (v = = FA) continue;    DFS (v, u); FD (J, Siz[u], 0) fd (k, siz[v], 0) {dp[u][j + k][1] = min (dp[u][j + k][1], dp[u][j][1] + min (dp[v][k][0), dp[v        ][K][1]));        Dp[u][j + k][0] = min (dp[u][j + k][0], dp[u][j][0] + dp[v][k][0]);  } Siz[u] + = Siz[v];    } Fu (i, ans + 1, siz[u]) {if (dp[u][i][0] <= B) ans = i;  else break;  }}int Main () {memset (DP, 0x3f, sizeof (DP)); Read (n);  Read (B); Fu (i, 1, n) {Read (c[i]);    Read (D[i]);    Dp[i][0][0] = 0; if (i > 1) {int u;      Read (U);      Add (i, u);    Add (U, i);  }} DFS (1, 0);  Fu (i, ans + 1, N) if (dp[1][i][1] <= B) ans = i;  Write (ANS); return 0;}

Codeforces 815C. Karen and supermarket "tree DP"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.