Advertisement
erek1e

Tea - BIO 2022 Round 2

Mar 28th, 2023
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     freopen("input.txt", "r", stdin);
  7.     freopen("output.txt", "w", stdout);
  8.     int h, t; cin >> h >> t;
  9.     if (h == 2) { // just cover the case where all nodes are leaves separately
  10.         int x; cin >> x >> x; // input the edge between 1 and 2
  11.         long long sum[2] {};
  12.         while (t--) {
  13.             int a, b;
  14.             long long tea;
  15.             cin >> a >> b >> tea;
  16.             if (a != b) sum[0] += tea, sum[1] += tea;
  17.             else if (a == 1) sum[0] += tea;
  18.             else sum[1] += tea;
  19.         }
  20.         cout << sum[0] << endl << sum[1] << endl;
  21.         return 0;
  22.     }
  23.     vector<vector<int>> g(h);
  24.     for (int i = 0; i < h-1; ++i) {
  25.         int u, v; cin >> u >> v;
  26.         --u, --v;
  27.         g[u].push_back(v);
  28.         g[v].push_back(u);
  29.     }
  30.  
  31.     vector<int> pos(h); // pos -1 means leaf, otherwise this is the position of the node in the non-leaf path
  32.     for (int i = 0; i < h; ++i) {
  33.         if (g[i].size() == 1) pos[i] = -1;
  34.     }
  35.  
  36.     // Now order the non-leaf nodes
  37.     int startNode = -1;
  38.     for (int i = 0; i < h; ++i) {
  39.         if (pos[i] == -1) continue;
  40.         int nonLeafDegree = 0;
  41.         for (int j : g[i]) {
  42.             if (pos[j] != -1) ++nonLeafDegree;
  43.         }
  44.         if (nonLeafDegree < 2) {
  45.             startNode = i;
  46.             break;
  47.         }
  48.     }
  49.     assert(startNode != -1);
  50.  
  51.     vector<int> order;
  52.     int cur = startNode, parent = -1;
  53.     while (cur != -1) {
  54.         pos[cur] = order.size();
  55.         order.push_back(cur);
  56.         int nxt = -1;
  57.         for (int j : g[cur]) {
  58.             if (pos[j] != -1 && j != parent) {
  59.                 nxt = j;
  60.                 break;
  61.             }
  62.         }
  63.         parent = cur;
  64.         cur = nxt;
  65.     }
  66.  
  67.     // Now use prefix sums to store values over this path
  68.     int n = order.size();
  69.     vector<long long> prefixPath(n);
  70.     vector<long long> leafTea(h); // Pun not intended - total tea in each leaf
  71.  
  72.     while (t--) {
  73.         int a, b;
  74.         long long tea;
  75.         cin >> a >> b >> tea;
  76.         --a, --b;
  77.  
  78.         if (pos[a] == -1) leafTea[a] += tea, a = g[a][0];
  79.         if (pos[b] == -1) leafTea[b] += tea, b = g[b][0];
  80.  
  81.         if (pos[a] > pos[b]) swap(a, b);
  82.         prefixPath[pos[a]] += tea;
  83.         if (pos[b]+1 < n) prefixPath[pos[b]+1] -= tea;
  84.     }
  85.  
  86.     for (int i = 1; i < n; ++i) prefixPath[i] += prefixPath[i-1];
  87.     for (int i = 0; i < h; ++i) {
  88.         if (pos[i] == -1) cout << leafTea[i] << endl;
  89.         else cout << prefixPath[pos[i]] << endl;
  90.     }
  91.     return 0;
  92. }
  93.  
Tags: Bio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement