Advertisement
Ahmed_Negm

Challenge

Apr 12th, 2023
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. #define ll long long
  7. #define OO 2'000'000'000
  8. #define ull unsigned long long
  9. #define nl '\n'
  10. #define sz(x) (ll)(x.size())
  11. #define all(x) x.begin(),x.end()
  12. #define rall(s)  s.rbegin(), s.rend()
  13. #define getline(s) getline(cin>>ws,s)
  14. #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  15. #define pi  3.141592653589793
  16. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  17. #define multi_ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  18.  
  19.  
  20. void Fast_IO(){
  21. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  22. // freopen("filename.in", "r", stdin);
  23. // freopen("filename.txt", "w", stdout);
  24. #ifndef ONLINE_JUDGE
  25. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  26. #endif
  27. }
  28.  
  29.  
  30.  
  31.  
  32. int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
  33. int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  34.  
  35. const int MAXN = 1e5+5;
  36.  
  37. int parent[MAXN], siz[MAXN],n;
  38. ll ans;
  39.  
  40. struct edge{
  41.     int u, v, w;
  42. };
  43.  
  44. edge edges[MAXN];
  45.  
  46. bool cmp(edge &a, edge &b){
  47.     return a.w<b.w;
  48. }
  49.  
  50. int find(int u){
  51.     while(parent[u] != u){
  52.         parent[u] = parent[parent[u]];
  53.         u = parent[u];
  54.     }
  55.     return u;
  56. }
  57.  
  58. void unite(int u, int v){
  59.     u = find(u), v = find(v);
  60.     if(siz[u] < siz[v]) swap(u, v);
  61.     parent[v] = u;
  62.     siz[u] += siz[v];
  63. }
  64.  
  65. void DSU(){
  66.     for(int i=1;i<=n;i++){
  67.         parent[i] = i;
  68.         siz[i] = 1;
  69.     }
  70.  
  71.     sort(edges,edges+n-1,cmp);
  72.  
  73.     for(int i=0;i<n-1;i++){
  74.         int u = edges[i].u, v = edges[i].v, w = edges[i].w;
  75.         int parent_u = find(u), parent_v = find(v);
  76.         ans += 1LL * w * siz[parent_u] * siz[parent_v];
  77.         unite(parent_u, parent_v);
  78.     }
  79. }
  80.  
  81.  
  82. void solve(){
  83.     cin>>n;
  84.  
  85.     for(int i=0;i<n-1;i++){
  86.         int u, v, w;
  87.         cin>>u>>v>>w;
  88.         edges[i].u = u;
  89.         edges[i].v = v;
  90.         edges[i].w = w;
  91.     }
  92.  
  93.     DSU();
  94.  
  95.     cout<<ans<<nl;
  96.  
  97.  
  98. }
  99.  
  100. int main(){
  101.     Fast_IO();
  102. int t =1;
  103. //cin>>t;
  104. while(t--){
  105. solve();
  106. }
  107. return 0;
  108. }  
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement