Advertisement
sidjha57

Treeversion

Sep 14th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. //template
  2.  
  3. #include<bits/stdc++.h>
  4. //#include<ext/pb_ds/assoc_container.hpp>
  5. //#include<ext/pb_ds/tree_policy.hpp>
  6. //#include <ext/pb_ds/trie_policy.hpp>
  7. //using namespace __gnu_pbds;
  8. using namespace std;
  9. //typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
  10. //typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pbtrie;
  11.  
  12. #define ll                       long long int
  13. #define ld                       long double
  14. #define mod                      1000000007
  15. #define inf                      1e18
  16. #define endl                     "\n"
  17. #define pb                       push_back
  18. #define vi                       vector<ll>
  19. #define vs                       vector<string>
  20. #define pii                      pair<ll,ll>
  21. #define ump                      unordered_map
  22. #define mp                       make_pair
  23. #define pq_max                   priority_queue<ll>
  24. #define pq_min                   priority_queue<ll,vi,greater<ll> >
  25. #define all(n)                   n.begin(),n.end()
  26. #define ff                       first
  27. #define ss                       second
  28. #define mid(l,r)                 (l+(r-l)/2)
  29. #define bitc(n)                  __builtin_popcount(n)
  30. #define SET(a)                   memset( a, -1, sizeof a )
  31. #define CLR(a)                   memset( a,  0, sizeof a )
  32. #define Pi                       3.141592653589793
  33. #define loop(i,a,b)              for(int i=(a);i<=(b);i++)
  34. #define looprev(i,a,b)           for(int i=(a);i>=(b);i--)
  35. #define _fast                    ios_base::sync_with_stdio(0);  cin.tie(0);
  36. #define iter(container,it)       for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
  37. #define log(args...)             {string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
  38. #define logarr(arr,a,b)          for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl;
  39. template <typename T> T          gcd(T a, T b){if(a%b) return gcd(b,a%b);return b;}
  40. template <typename T> T          lcm(T a, T b){return (a*(b/gcd(a,b)));}
  41. vs tokenizer(string str,char ch) {std::istringstream var((str)); vs v; string t; while(getline((var), t, (ch))) {v.pb(t);} return v;}
  42.  
  43. void err(istream_iterator<string> it) {}
  44. template<typename T, typename... Args>
  45. void err(istream_iterator<string> it, T a, Args... args) {
  46. cout << *it << " = " << a << endl;
  47. err(++it, args...);
  48. }
  49.  
  50. void fileIO() {
  51.     #ifndef ONLINE_JUDGE
  52.         freopen("input.txt", "r", stdin);
  53.         freopen("output.txt", "w", stdout);
  54.     #endif
  55. }
  56.  
  57. vector<vi> adj;
  58. vi w;
  59. struct trio {
  60.     ll inv, zeros, ones;
  61.     trio() {
  62.         inv = 0,zeros=0,ones=0;
  63.     }
  64. };
  65. bool cmp (const trio &a, const trio &b) {
  66.     return a.ones*b.zeros < a.zeros*b.ones;
  67. }
  68.  
  69. trio dfs (ll nd=1, ll par=0) {
  70.     trio cur;
  71.     vector<trio> subtr;
  72.     for (ll child : adj[nd]) {
  73.         if (child != par)
  74.             subtr.pb(dfs(child,nd));
  75.     }
  76.  
  77.     sort(subtr.begin(),subtr.end(),cmp);
  78.  
  79.     if (w[nd] == 1) cur.ones++;
  80.     else cur.zeros++;
  81.  
  82.     for (trio &t : subtr) {
  83.         cur.inv += t.inv; // Intra count of inversions
  84.         cur.inv += cur.ones * t.zeros; // Inter count
  85.  
  86.         cur.zeros += t.zeros;
  87.         cur.ones += t.ones;
  88.     }
  89.     return cur;
  90. }
  91. void solve() {
  92.     ll n; cin >> n;
  93.     adj.clear();
  94.     adj.resize(n+1);
  95.     w.resize(n+1);
  96.    
  97.     loop (i,1,n) cin >> w[i];
  98.  
  99.     loop (i,0,n-1) {
  100.         ll u,v; cin >> u >> v;
  101.         adj[u].pb(v);
  102.         adj[v].pb(u);
  103.     }
  104.  
  105.     trio ans = dfs();
  106.     cout << ans.inv << "\n";
  107.  
  108. }
  109.  
  110. int main(int argc, char const *argv[]){
  111.     _fast
  112.     //fileIO();
  113.     ll t; cin>>t;
  114.     while(t--){
  115.      solve();
  116.     }
  117.   return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement