Advertisement
Guest User

Kruskal

a guest
Mar 22nd, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. /**
  2. * E:\Dropbox\Profession\SUST\CSE_138_2017\Kruskal.cpp
  3. * Created on: 2017-11-22-16.37.51, Wednesday
  4. * Verdict: Not Solved
  5. * Author: Enamul Hassan
  6. **/
  7.  
  8. #include <bits/stdc++.h>
  9. #define _ ios_base::sync_with_stdio(0);cin.tie(0);
  10.  
  11. #define SZ(a) ((ll)a.size())
  12. #define sz 200005
  13. #define pb push_back
  14. #define pp pop_back()
  15. #define all(a) a.begin(),a.end()
  16. #define ll long long
  17. #define cntbit(mask) __builtin_popcount(mask)
  18. #define unify(a) stable_sort(a.begin(),a.end());a.resize(distance(a.begin(),unique(all(a))));
  19. #define fread freopen("input.txt","r",stdin)
  20. #define fwrite freopen("output.txt","w",stdout)
  21. #define inf (1e18)
  22. #define chng(a,b) a^=b^=a^=b;
  23. #define clr(abc,z) memset(abc,z,sizeof(abc))
  24. #define PI acos(-1)
  25. #define pi 3.14159265358979323846264338327950288419716939937510
  26. #define fr(i,a,b) for(i=a;i<=b;i++)
  27. #define cspf printf("Case %d:", cas++);
  28. #define csco cout<<"Case "<<cas++<<": ";
  29. #define mod 1000000007
  30.  
  31. #ifdef ENAM
  32. #define deb(args...) {dbg,args; cerr<<endl;}
  33. #else
  34. #define deb(args...)
  35. #endif
  36. using namespace std;
  37.  
  38. struct _deb{
  39.     template<typename T> _deb& operator , (const T& _temp){
  40.         cerr<<_temp<<" ";
  41.         return *this;
  42.     }
  43. }dbg;
  44.  
  45. /// debug template credit: Bidhan Roy
  46.  
  47. ll bigmod(ll sonkha,ll ghat,ll vag_const){ll vag_shesh=1;while(ghat>0){if(ghat%2==1){vag_shesh=(vag_shesh*sonkha)%vag_const;}ghat/=2;sonkha=(sonkha*sonkha)%vag_const;}return vag_shesh;}
  48. ll inverse_mod(ll bivajok, ll vag_const){return bigmod(bivajok,vag_const-2, vag_const);}
  49.  
  50. int par[sz];
  51.  
  52. priority_queue< pair<int, pair<int,int> > ,
  53. vector< pair<int, pair<int,int> > >,
  54. greater< pair<int,pair<int,int> > > >pq;
  55.  
  56.  
  57. int find_set(int node)
  58. {
  59.     if(par[node] == node)
  60.         return node;
  61.     return par[node] = find_set(par[node]);
  62. }
  63.  
  64. int kruskal()
  65. {
  66.     vector < pair<int, pair<int,int> > > ans;
  67.     int c, u, v,tot=0,x,y;
  68.     while(!pq.empty())
  69.     {
  70.         c = pq.top().first;
  71.         u = pq.top().second.first;
  72.         v = pq.top().second.second;
  73.         x = find_set(u);
  74.         y = find_set(v);
  75.         if(x!=y)
  76.         {
  77.             ans.pb({c, {u,v} });
  78.             par[x] = y;
  79.             tot+=c;
  80.         }
  81.  
  82.         pq.pop();
  83.     }
  84.  
  85.     for (int i = 0; i<ans.size(); i++)
  86.     {
  87.         cout<<i<<":"<<ans[i].first<<" -> ("
  88.         <<ans[i].second.first << "," <<
  89.         ans[i].second.second << ")\n";
  90.     }
  91.  
  92.     return tot;
  93. }
  94.  
  95.  
  96. int main()
  97. {
  98. #ifdef ENAM
  99. //      fread;
  100. //  fwrite;
  101. #endif // ENAM
  102.     _
  103.     int cas = 1,t,n,m,x,y,z;
  104. //    clock_t begin, end;
  105. //    double time_spent;
  106. //    begin = clock();
  107.  
  108.     cin>>n>>m;
  109.  
  110.     for (int i = 0; i<=n; i++)
  111.         par[i]=i;
  112.  
  113.     for (int i = 0; i<m; i++)
  114.     {
  115.         cin>>x>>y>>z;
  116.         pq.push({z,{x,y}});
  117.     }
  118.  
  119.     cout<<"Total Cost of MST = "<<kruskal()<<"\n";
  120.  
  121. //    end = clock();
  122. //    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  123. //    cerr<<"Time spent = "<<time_spent<<endl;
  124.  
  125.    return 0;
  126. }
  127. /**
  128. 6 8
  129. 1 2 1
  130. 1 3 1
  131. 1 5 1
  132. 2 4 3
  133. 2 5 2
  134. 3 6 2
  135. 4 6 3
  136. 5 6 5
  137. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement