Kaseil

Kruskul

May 17th, 2023 (edited)
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <numeric>
  5. #define DUMMY 0
  6. #define INF 1e9 // use this to represent the infinity
  7.  
  8. using namespace std;
  9.  
  10. // put your Disjoint Set here
  11. class DisjointSet {
  12. public:
  13.     std::vector<int> ppt;
  14.  
  15.     DisjointSet(int size) {
  16.         size++;
  17.         ppt.resize(size);
  18.  
  19.         for (int i = 0; i < size; i++) {
  20.             makeSet(i);
  21.         }
  22.     }
  23.  
  24.     void makeSet(int i) {
  25.         ppt[i] = i;
  26.     }
  27.  
  28.     int findSet(int i) {//path compression
  29.         if (ppt[i]!= i) {
  30.             ppt[i]= findSet(ppt[i]);
  31.         }
  32.         return ppt[i];
  33.     }
  34.  
  35.     void unionSet(int i, int j) {
  36.         int iParentIndex = findSet(i);
  37.         int jParentIndex = findSet(j);
  38.  
  39.         ppt[jParentIndex] = iParentIndex;
  40.     }
  41.  
  42.     bool isCycle(int i, int j) {
  43.         if (findSet(i) == findSet(j)) return true;
  44.         return false;
  45.     }
  46.  
  47. };
  48.  
  49. class Graph {
  50. private:
  51.     struct Edge {
  52.         int u; int v; int w;
  53.         Edge(int u, int v, int w) : u(u), v(v), w(w) { }
  54.         bool operator < (const Edge& edge) const { return this->w < edge.w; }
  55.     };
  56.  
  57.     vector<Edge> edges;            //edge list used for Kruskal's algorithm
  58.     int n;                         //number of edges
  59. public:
  60.     Graph(int n) : n(n) {
  61.         edges = vector<Edge>();
  62.     }
  63.  
  64.     void insert_edge(int u, int v, int w) {
  65.         edges.push_back(Edge(u, v, w));
  66.     }
  67.  
  68.     vector<int> kruskal() {
  69.         DisjointSet set(n);
  70.         vector<int> kruskal_sequence(1, DUMMY); // store the weight of an edge
  71.  
  72.         // fill in here
  73.         std::sort(edges.begin(), edges.end());
  74.         //  - you can sequentially iterate each edge as follows:
  75.         //     - for(Edge edge : edges)
  76.         for (Edge edge : edges) {
  77.             if (set.isCycle(edge.u, edge.v)) continue;
  78.             else {
  79.                 set.unionSet(edge.u, edge.v);
  80.                 kruskal_sequence.push_back(edge.w);
  81.             }
  82.         }
  83.         //  - push back the weight of an edge selected by Kruskal into kruskal_sequence
  84.         //    (use kruskal_sequence.push_back(int weight))
  85.  
  86.         return kruskal_sequence;
  87.     }
  88. };
  89.  
  90. int main() {
  91.     ios::sync_with_stdio(false);
  92.     cin.tie(NULL);
  93.     cout.tie(NULL);
  94.  
  95.     int n; // # of nodes
  96.     int m; // # of edges
  97.     int i; // index of a sequence to be printed
  98.     cin >> n >> m >> i;
  99.  
  100.     Graph graph(n);
  101.  
  102.     for (int j = 0; j < m; j++) {
  103.         int s, t, w;
  104.         cin >> s >> t >> w;
  105.         if (s > t) swap(s, t);
  106.         graph.insert_edge(s, t, w);
  107.     }
  108.  
  109.     vector<int> kruskal_sequence = graph.kruskal();
  110.  
  111.     // sum all entries of kruskal_sequence
  112.     long long cost = std::accumulate(kruskal_sequence.begin(), kruskal_sequence.end(), 0);
  113.  
  114.     cout << cost << endl;
  115.     cout << kruskal_sequence[i] << endl;
  116.  
  117.     return 0;
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment