mr_dot_convict

TREEVERS-Codechef-mr.convict

Oct 2nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | None | 0 0
  1. // Hack it and have it ;) //
  2. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  3.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  4.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  5.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  6.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  7. When I wrote this, only God and I understood what I was doing
  8.  ** * * * * * * * Now, only God knows!* * * * * * */
  9. #include      <bits/stdc++.h>
  10. #include      <ext/pb_ds/assoc_container.hpp>
  11. #include      <ext/pb_ds/tree_policy.hpp>
  12. using namespace std;
  13. using namespace __gnu_pbds;
  14.  
  15. #ifndef CONVICTION
  16. #pragma GCC   optimize ("Ofast")
  17. #pragma GCC   optimize ("unroll-loops")
  18. #pragma GCC   target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  19. #endif
  20.  
  21. #define IOS   ios_base::sync_with_stdio(false); cin.tie (nullptr)
  22. #define PREC  cout.precision (10); cout << fixed
  23. #define x     first
  24. #define y     second
  25. #define bg(x) " [ " << #x << " : " << (x) << " ] "
  26. #define un(x) sort(x.begin(), x.end()), \
  27.    x.erase(unique(x.begin(), x.end()), x.end())
  28. using   ll  = long long;
  29. using   ull = unsigned long long;
  30. using   ff  = long double;
  31. using   pii = pair<int,int>;
  32. using   pil = pair<int,ll>;
  33. typedef tree
  34. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  35. ordered_set;
  36.  
  37. struct chash {
  38.    int operator () (pii x) const { return x.x*31 + x.y; }
  39. };
  40. gp_hash_table <pii, int, chash> mp;
  41.  
  42. #if __cplusplus > 201103L
  43. seed_seq seq{
  44.    (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  45.       (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  46.       (uint64_t) __builtin_ia32_rdtsc(),
  47.       (uint64_t) (uintptr_t) make_unique<char>().get()
  48. };
  49. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  50. #else
  51. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  52. mt19937 rng(seed);
  53. #endif
  54.  
  55. #define debug(args...) { \
  56.    /* WARNING : do NOT compile this debug func calls with following flags: // \
  57.     * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  58.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  59.    stringstream _ss(_s); \
  60.    istream_iterator<string> _it(_ss); err(_it, args); \
  61. }
  62. void err(istream_iterator<string> it) {
  63.    it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  64. }
  65. template<typename T, typename... Args>
  66. void err(istream_iterator<string> it, T a, Args... args) {
  67.    cerr << fixed << setprecision(15)
  68.       << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  69.    err(++it, args...);
  70. }
  71. /*****************************************************************************/
  72. //Don’t practice until you get it right. Practice until you can’t get it wrong
  73.  
  74. signed main() {
  75.    IOS; PREC;
  76.  
  77.    int tc;
  78.    cin >> tc;
  79.    while (tc--) {
  80.       int n;
  81.       cin >> n;
  82.       vector <int> w(n), dp(n), sz(n);
  83.       vector <vector <int>> T(n, vector <int>());
  84.       for (int i = 0; i < n; ++i) {
  85.          cin >> w[i];
  86.       }
  87.       for (int e = 0; e < n - 1; ++e) {
  88.          int u, v;
  89.          cin >> u >> v;
  90.          --u, --v;
  91.          T[u].push_back(v);
  92.          T[v].push_back(u);
  93.       }
  94.  
  95.       function <void(int, int)> dfs
  96.          = [&] (int u, int pr) -> void {
  97.             for (int i = 0; i < (int)T[u].size(); ++i) if (T[u][i] == pr) {
  98.                T[u].erase(T[u].begin() + i);
  99.                break;
  100.             }
  101.  
  102.             dp[u] = w[u];
  103.             sz[u] = 1;
  104.             for (int v : T[u]) {
  105.                dfs(v, u);
  106.                dp[u] += dp[v];
  107.                sz[u] += sz[v];
  108.             }
  109.  
  110.             sort(T[u].begin(), T[u].end(), [&] (int p, int q) {
  111.                   return 1ll * dp[p] * (sz[q] - dp[q]) < 1ll * dp[q] * (sz[p] - dp[p]);
  112.                   });
  113.             // p before q if inversion keeping p ahead are less
  114.          };
  115.  
  116.       vector <int> w_;
  117.       function <void(int)> dfs2
  118.          = [&] (int u) -> void {
  119.             w_.push_back(w[u]);
  120.             for (int v : T[u]) dfs2(v);
  121.          };
  122.  
  123.       dfs(0, -1);
  124.       dfs2(0);
  125.  
  126.       vector <int> suff(n);
  127.       for (int i = n - 1; i >= 0; --i) {
  128.          if (i == n - 1) suff[i] = (w_[i] == 0);
  129.          else suff[i] = suff[i + 1] + (w_[i] == 0);
  130.       }
  131.  
  132.       ll ans = 0;
  133.       for (int i = 0; i < n; ++i) {
  134.          if (w_[i]) ans += suff[i];
  135.       }
  136.       cout << ans << '\n';
  137.    }
  138.  
  139.    return EXIT_SUCCESS;
  140. }
Add Comment
Please, Sign In to add comment