tien_noob

PTREE - VNOJ - Traceback of Knapsack on Tree

Feb 5th, 2022 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. //Make CSP great again
  2. //You're as beautiful as the day I lost you
  3. //New year, best wishes
  4. #include <bits/stdc++.h>
  5. #define TASK "TESTCODE"
  6. #define Log2(x) 31 - __builtin_clz(x)
  7. using namespace std;
  8. const int N = 2e2;
  9. vector<int> Adj[N + 1];
  10. int c[N + 1], f[N + 1], Trace[N + 1][N + 1], n, m;
  11. vector<int> dp[N + 1];
  12. void read()
  13. {
  14.     cin >> n >> m;
  15.     for (int i = 1; i <= n; ++ i)
  16.     {
  17.         cin >> c[i];
  18.     }
  19.     for (int i = 1; i < n; ++ i)
  20.     {
  21.         int u, v;
  22.         cin >> u >> v;
  23.         Adj[u].push_back(v);
  24.         Adj[v].push_back(u);
  25.     }
  26. }
  27. void DFS(int u, int p)
  28. {
  29.     f[u] = p;
  30.     dp[u] = {0, c[u]};
  31.     for (int v : Adj[u])
  32.     {
  33.         if (v == p)
  34.         {
  35.             continue;
  36.         }
  37.         DFS(v, u);
  38.         vector<int> t(dp[u].size() + dp[v].size() - 1, -1e9);
  39.         for (int i = 1; i < dp[u].size(); ++ i)
  40.         {
  41.             for (int j = 0; j < dp[v].size(); ++ j)
  42.             {
  43.                 if (t[i + j] < dp[u][i] + dp[v][j])
  44.                 {
  45.                     t[i + j] = dp[u][i] + dp[v][j];
  46.                     Trace[v][i + j] = j;
  47.                 }
  48.             }
  49.         }
  50.         t[0] = 0;
  51.         swap(t, dp[u]);
  52.     }
  53. }
  54. void Traceback(int u, int p, int a)
  55. {
  56.     if (a == 0)
  57.     {
  58.         return ;
  59.     }
  60.     reverse(Adj[u].begin(), Adj[u].end());
  61.     for (int v : Adj[u])
  62.     {
  63.         if (v == p)
  64.         {
  65.             continue;
  66.         }
  67.         Traceback(v, u, Trace[v][a]);
  68.         a -= Trace[v][a];
  69.     }
  70.     cout << u << ' ';
  71. }
  72. void solve()
  73. {
  74.     DFS(1, 0);
  75.     int root = -1;
  76.     for (int i = 1; i <= n; ++ i)
  77.     {
  78.         if (dp[i].size() > m)
  79.         {
  80.             if (root == -1 || dp[i][m] > dp[root][m])
  81.             {
  82.                 root = i;
  83.             }
  84.         }
  85.     }
  86.     Traceback(root, f[root], m);
  87. }
  88. int main()
  89. {
  90.     ios_base::sync_with_stdio(false);
  91.     cin.tie(nullptr);
  92.     if (fopen(TASK".INP", "r"))
  93.     {
  94.         freopen(TASK".INP", "r", stdin);
  95.         //freopen(TASK".out", "w", stdout);
  96.     }
  97.     int t = 1;
  98.     bool typetest = false;
  99.     if (typetest)
  100.     {
  101.         cin >> t;
  102.     }
  103.     for (int __ = 1; __ <= t; ++ __)
  104.     {
  105.         //cout << "Case " << __ << ": ";
  106.         read();
  107.         solve();
  108.     }
  109. }
  110.  
Add Comment
Please, Sign In to add comment