Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. struct muchie
  9. {
  10.     int x, y, cost;
  11.     bool operator<(const muchie &that) const
  12.     {
  13.         return cost < that.cost;
  14.     }
  15. };
  16.  
  17. const int nMax = 200005;
  18. const int mMax = 400005;
  19.  
  20. int n, m;
  21. muchie v[mMax];
  22. int father[nMax];
  23. vector<int> ans;
  24. int ansCost;
  25.  
  26. void citire()
  27. {
  28.     ifstream in("apm.in");
  29.     in >> n >> m;
  30.     for(int i = 1; i <= m; ++i)
  31.         in >> v[i].x >> v[i].y >> v[i].cost;
  32.     in.close();
  33. }
  34.  
  35. int GetFather(int x)
  36. {
  37.     if(father[x] != x)
  38.         father[x] = GetFather(father[x]);
  39.     return father[x];
  40. }
  41.  
  42. void rezolvare()
  43. {
  44.     for(int i = 1; i <= n; ++i)
  45.         father[i] = i;
  46.     sort(v + 1, v + m + 1);
  47.     ans.reserve(n - 1);
  48.     for(int i = 1; i <= m; ++i)
  49.     {
  50.         if(GetFather(v[i].x) != GetFather(v[i].y))
  51.         {
  52.             ans.push_back(i);
  53.             ansCost += v[i].cost;
  54.             father[father[v[i].y]] = v[i].x;
  55.         }
  56.     }
  57. }
  58.  
  59. void afisare()
  60. {
  61.     ofstream out("apm.out");
  62.     out << ansCost << "\n" << n-1 << "\n";
  63.     for(auto i:ans)
  64.         out << v[i].x << " " << v[i].y << "\n";
  65. }
  66.  
  67. int main()
  68. {
  69.     citire();
  70.     rezolvare();
  71.     afisare();
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement