Advertisement
pb_jiang

CF600E divide and conquer

Nov 2nd, 2023
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. // Problem: E. Lomsat gelral
  2. // Contest: Codeforces - Educational Codeforces Round 2
  3. // URL: https://codeforces.com/problemset/problem/600/E
  4. // Memory Limit: 256 MB
  5. // Time Limit: 2000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #ifndef __DEBUG__
  13. #define dbg(...) 42
  14. #endif
  15. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  16.  
  17. using ll = long long;
  18. using pii = pair<int, int>;
  19. using pll = pair<ll, ll>;
  20. using vl = vector<ll>;
  21. using vi = vector<int>;
  22. using a2l = array<ll, 2>;
  23.  
  24. int main(int argc, char **argv)
  25. {
  26.     int n, x, y;
  27.     cin >> n;
  28.     vi vc(n + 1);
  29.     for (int i = 1; i <= n; ++i)
  30.         cin >> vc[i];
  31.     vector<vi> g(n + 1);
  32.     for (int i = 1; i < n; ++i)
  33.         cin >> x >> y, g[x].push_back(y), g[y].push_back(x);
  34.  
  35.     int dcnt = 0;
  36.     vi dfn(n + 1), idx(n + 1), dsize(n + 1);
  37.     function<int(int, int)> dfs = [&](int u, int fa) {
  38.         dsize[u] = 1, dfn[u] = ++dcnt, idx[dcnt] = u;
  39.         for (auto v : g[u])
  40.             if (v != fa)
  41.                 dsize[u] += dfs(v, u);
  42.         return dsize[u];
  43.     };
  44.     dfs(1, -1);
  45.  
  46.     vl ans(n + 1);
  47.     vi ccnt(n + 1), opts;
  48.     ll max_freq = 0, acc = 0;
  49.     auto inc_color_cnt = [&](int c) -> ll {
  50.         opts.push_back(c);
  51.         ccnt[c] += 1;
  52.         if (ccnt[c] == max_freq) {
  53.             acc += c;
  54.         } else if (ccnt[c] > max_freq) {
  55.             max_freq = ccnt[c];
  56.             acc = c;
  57.         }
  58.         return acc;
  59.     };
  60.     auto clear_color = [&]() {
  61.         max_freq = acc = 0;
  62.         while (opts.size()) {
  63.             ccnt[opts.back()]--;
  64.             opts.pop_back();
  65.         }
  66.     };
  67.     function<void(int, int)> bisearch = [&](int l, int r) {
  68.         if (l == r) {
  69.             ans[idx[l]] = vc[idx[l]];
  70.             return;
  71.         }
  72.  
  73.         int mid = l + r >> 1;
  74.         bisearch(l, mid), bisearch(mid + 1, r);
  75.         clear_color();
  76.  
  77.         int p = mid;
  78.         for (int i = mid; i >= l; --i) {
  79.             int j = i + dsize[idx[i]] - 1;
  80.             if (j > r)
  81.                 break;
  82.             ll last = inc_color_cnt(vc[idx[i]]);
  83.             if (j <= mid)
  84.                 continue;
  85.             while (p < j)
  86.                 last = inc_color_cnt(vc[idx[++p]]);
  87.             ans[idx[i]] = last;
  88.         }
  89.     };
  90.     bisearch(1, n);
  91.  
  92.     for (int i = 1; i <= n; ++i)
  93.         cout << ans[i] << ' ';
  94.     cout << endl;
  95. };
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement