Advertisement
erek1e

Storm in a Teacup - BIO 2024 Round 2

Apr 28th, 2024
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. /**
  2.  * @file teacup.cpp
  3.  * @version 1.0
  4.  * @date 2024-03-30
  5.  *
  6.  * usage:
  7.  *      Read from / write to default input.txt and output.txt
  8.  *          teacup.exe
  9.  *      Read from / write to custom files:
  10.  *          teacup.exe in.txt out.txt
  11.  */
  12. // This is a clean solution to the problem, but does not pass due to the very tight time limit. See my other solution with constant optimisations that passes all tests.
  13. #include <iostream>
  14. #include <vector>
  15. #include <algorithm>
  16. #include <functional>
  17.  
  18. using namespace std;
  19.  
  20. void fileIO(int argc, char *argv[]);
  21.  
  22. int main(int argc, char *argv[]) {
  23.     fileIO(argc, argv); // remove for standard input/output
  24.    
  25.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  26.     int h, f; cin >> h >> f;
  27.     vector<int> value(h);
  28.     for (int &x : value) cin >> x;
  29.  
  30.     vector<vector<int>> g(h);
  31.     for (int i = 0; i < h-1; ++i) {
  32.         int u, v; cin >> u >> v;
  33.         --u, --v;
  34.         g[u].push_back(v);
  35.         g[v].push_back(u);
  36.     }
  37.  
  38.     // Binary Search on the answer
  39.     long long left = 0, right = (long long)h * *max_element(value.begin(), value.end()) + 1;
  40.     while (left+1 < right) {
  41.         long long minComponentSum = (left + right) / 2;
  42.         /* check if the graph can be split into f components,
  43.         each with sum >= minComponentSum */
  44.  
  45.         // dp on subtrees
  46.         function<pair<int, long long>(int, int)> dfs = [&](int node, int parent = -1) {
  47.             // greedy, proved to be optimal
  48.             int components = 0;
  49.             long long leftoverSum = value[node];
  50.             for (int child : g[node]) {
  51.                 if (child != parent) {
  52.                     auto [c, s] = dfs(child, node);
  53.                     components += c;
  54.                     leftoverSum += s;
  55.                 }
  56.             }
  57.             if (leftoverSum >= minComponentSum) {
  58.                 ++components;
  59.                 leftoverSum = 0;
  60.             }
  61.             return pair{components, leftoverSum};
  62.         };
  63.  
  64.         // if dfs from arbitrary root yields at least f components
  65.         if (dfs(0, -1).first >= f) {
  66.             left = minComponentSum;
  67.         } else {
  68.             right = minComponentSum;
  69.         }
  70.     }
  71.     cout << left << '\n';
  72.     return 0;
  73. }
  74.  
  75. void fileIO(int argc, char *argv[]) {
  76.     const char * in = "input.txt", * out = "output.txt";
  77.     if (argc > 1) in = argv[1];
  78.     if (argc > 2) out = argv[2];
  79.     freopen(in, "r", stdin);
  80.     freopen(out, "w", stdout);
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement