Advertisement
pb_jiang

CF915F TLE

May 5th, 2023
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. // Problem: F. Imbalance Value of a Tree
  2. // Contest: Codeforces - Educational Codeforces Round 36 (Rated for Div. 2)
  3. // URL: https://codeforces.com/problemset/problem/915/F
  4. // Memory Limit: 256 MB
  5. // Time Limit: 4000 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.  
  13. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  14.  
  15. using ll = long long;
  16. using pii = pair<int, int>;
  17. using pll = pair<ll, ll>;
  18. using vl = vector<ll>;
  19. using vi = vector<int>;
  20.  
  21. int n;
  22. vector<pii> a;
  23.  
  24. ll maxv, minv;
  25.  
  26. int fa[1000003], cnt[1000003];
  27. void init_dsu()
  28. {
  29.     memset(fa, -1, sizeof(fa));
  30.     fill_n(cnt, sizeof(cnt) / sizeof(int), 1);
  31. }
  32. int find(int x)
  33. {
  34.     if (fa[x] == -1)
  35.         return x;
  36.     return fa[x] = find(fa[x]);
  37. }
  38. void join(int x, int y)
  39. {
  40.     int px = find(x), py = find(y);
  41.     if (px != py) {
  42.         fa[px] = py;
  43.         cnt[py] += cnt[px];
  44.         cnt[px] = 0;
  45.     }
  46. }
  47.  
  48. int main(int argc, char **argv)
  49. {
  50.     cin >> n;
  51.     a = vector<pii>(n);
  52.     for (int i = 0; i < n; ++i)
  53.         cin >> a[i].first, a[i].second = i + 1;
  54.  
  55.     vector<pii> edges(n - 1);
  56.     using a3i = array<int, 3>;
  57.     vector<a3i> es(n - 1);
  58.     for (int i = 0; i < n - 1; ++i)
  59.         cin >> edges[i].first >> edges[i].second;
  60.  
  61.     init_dsu();
  62.     // sort(a.begin(), a.end());
  63.     for (int i = 0; i < n - 1; ++i)
  64.         es[i][1] = edges[i].first, es[i][2] = edges[i].second,
  65.         es[i][0] = max(a[es[i][1] - 1].first, a[es[i][2] - 1].first);
  66.     sort(es.begin(), es.end());
  67.     for (int i = 0; i < n - 1; ++i) {
  68.         int u = es[i][1], v = es[i][2];
  69.         ll w = es[i][0];
  70.         int up = find(u), vp = find(v);
  71.         int usize = cnt[up], vsize = cnt[vp];
  72.         join(up, vp);
  73.         maxv += w * usize * vsize;
  74.     }
  75.  
  76.     init_dsu();
  77.     for (int i = 0; i < n - 1; ++i)
  78.         es[i][1] = edges[i].first, es[i][2] = edges[i].second,
  79.         es[i][0] = min(a[es[i][1] - 1].first, a[es[i][2] - 1].first);
  80.     sort(es.rbegin(), es.rend());
  81.     for (int i = 0; i < n - 1; ++i) {
  82.         int u = es[i][1], v = es[i][2];
  83.         ll w = es[i][0];
  84.  
  85.         int up = find(u), vp = find(v);
  86.         int usize = cnt[up], vsize = cnt[vp];
  87.         join(up, vp);
  88.         minv += w * usize * vsize;
  89.     }
  90.  
  91.     cout << maxv - minv << endl;
  92.  
  93.     return 0;
  94. };
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement