Advertisement
Sander447

kruskal.cpp

Jun 19th, 2021
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. // Author: Sander de Beet
  2.  
  3. #include <bits/stdc++.h>  // This will work only for g++ compiler.
  4.  
  5. #define f0r(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
  6. #define f1r(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
  7. #define f0rr(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
  8. #define f1rr(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
  9.  
  10. // short hand for usual tokens
  11. #define pb push_back
  12. #define fi first
  13. #define se second
  14.  
  15. // to be used with algorithms that processes a container Eg: find(all(c),42)
  16. #define all(x) (x).begin(), (x).end() //Forward traversal
  17. #define rall(x) (x).rbegin, (x).rend() //reverse traversal
  18.  
  19. // find if a given value is present in a container. Container version. Runs in log(n) for set and map
  20. #define present(c,x) ((c).find(x) != (c).end())
  21.  
  22. // find version works for all containers. This is present in std namespace.
  23. #define cpresent(c,x) (find(all(c),x) != (c).end())
  24.  
  25. #define uni(x) (x).erase(unique(all(x)), (x).end())
  26. #define debug(x) cout<<#x<<" -> "<<x<<'\n'
  27.  
  28.  
  29. using namespace std;
  30.  
  31. // Shorthand for commonly used types
  32. typedef vector<int> vi;
  33. typedef vector<vi> vvi;
  34. typedef vector<string> vs;
  35. typedef pair<int, int> ii;
  36. typedef vector<ii> vii;
  37. typedef long long ll;
  38. typedef vector<ll> vll;
  39. typedef vector<vll> vvll;
  40. typedef long double ld;
  41.  
  42. void fast_IO() {
  43.     ios::sync_with_stdio(false);
  44.     cin.tie(NULL); cout.tie(NULL);
  45. }
  46.  
  47. class DisjSet {
  48.     int *rank, *parent, n;
  49. public:
  50.     // Constructor to create and
  51.     // initialize sets of n items.
  52.     DisjSet(int n) {
  53.         rank = new int[n];
  54.         parent = new int[n];
  55.         this->n = n;
  56.         makeSet();
  57.     }
  58.  
  59.     // Creates n single item sets.
  60.     void makeSet() {
  61.         for (int i = 0; i < n; i++) {
  62.             parent[i] = i;
  63.             rank[i] = 1;
  64.         }
  65.     }
  66.  
  67.     // Finds set of given item x.
  68.     int find(int x) {
  69.         // Finds the representative of the set
  70.         // that x is an element of
  71.         if (parent[x] != x) {
  72.  
  73.             // if x is not the parent of itself
  74.             // Then x is not the representative of
  75.             // his set,
  76.             parent[x] = find(parent[x]);
  77.  
  78.             // so we recursively call Find on its parent
  79.             // and move i's node directly under the
  80.             // representative of this set
  81.         }
  82.  
  83.         return parent[x];
  84.     }
  85.  
  86.     // Do union of two sets represented
  87.     // by x and y.
  88.     void Union(int x, int y) {
  89.         // Find current sets of x and y
  90.         int x_root = find(x);
  91.         int y_root = find(y);
  92.  
  93.         // If they are already in same set
  94.         if (x_root == y_root)
  95.             return;
  96.  
  97.         // Put smaller ranked item under
  98.         // bigger ranked item if ranks are
  99.         // different
  100.         if (rank[x_root] <= rank[y_root]) {
  101.             int temp = x_root;
  102.             x_root = y_root;
  103.             y_root = temp;
  104.         }
  105.  
  106.         parent[y_root] = x_root;
  107.  
  108.         int new_rank = rank[x_root] + rank[y_root];
  109.         rank[x_root] = new_rank;
  110.         rank[y_root] = new_rank;
  111.     }
  112.  
  113.     int get_size(int x) {
  114.         int x_root = find(x);
  115.         return rank[x_root];
  116.     }
  117.  
  118.     bool same_set(int x, int y) {
  119.         return find(x) == find(y);
  120.     }
  121. };
  122.  
  123.  
  124. void solve() {
  125.     int n, m; cin >> n >> m;
  126.     int mst_cost = 0;
  127.  
  128.     DisjSet dset(n);
  129.     vector<pair<pair<int, int>, int>> edges;
  130.     vector<pair<int, int>> ans;
  131.  
  132.     for (int i = 0; i < m; i++) {
  133.         int a, b, c;
  134.         cin >> a >> b >> c;
  135.         edges.pb({{a, b}, c});
  136.     }
  137.  
  138.     sort(all(edges), [](pair<pair<int, int>, int> p1, pair<pair<int, int>, int> p2){
  139.         return p1.se < p2.se;
  140.     });
  141.  
  142.     for (auto e : edges) {
  143.         if (!dset.same_set(e.fi.fi, e.fi.se)) {
  144.             dset.Union(e.fi.fi, e.fi.se);
  145.             mst_cost += e.se;
  146.             ans.pb({e.fi.fi, e.fi.se});
  147.         }
  148.     }
  149.  
  150.     for (auto a : ans) {
  151.         cout << a.fi << " " << a.se << "\n";
  152.     }
  153.  
  154.     cout << mst_cost << "\n";
  155. }
  156.  
  157.  
  158. int main(int argc, char* argv[]) {
  159.     fast_IO();
  160.     auto a = freopen(argv[1], "r", stdin);
  161.  
  162.     solve();
  163.  
  164.     return 0;
  165. }
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement