Advertisement
vladm98

Untitled

Oct 15th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector <int> graf[10001];
  6. map <pair<int, int>, int> Costuri;
  7. int tata[10001], int h[10001];
  8.  
  9. int stramos (int node)
  10. {
  11. int rege = node;
  12. while (rege!=tata[rege])
  13. rege = tata[rege];
  14. while (node != tata[node])
  15. {
  16. int copie = node;
  17. node = tata[node];
  18. tata[copie] = rege;
  19. }
  20. return rege;
  21. }
  22. void Union (int x, int y)
  23. {
  24. int s1 = stramos(x), s2 = stramos(y);
  25. if (s1 == s2) return;
  26. if (h[s1] < h[s2])
  27. swap(s1, s2);
  28. tata[s2]=s1;
  29. if (h[s1] == h[s2])
  30. ++h[s1];
  31. }
  32. vector <int, pair<int, int>> Edeges;
  33. int main(int argc, char const *argv[])
  34. {
  35. int n, m, s;
  36. cin >> n >> m >> s;
  37. for (int i = 1; i<=n; ++i)
  38. {
  39. tata[i] = i;
  40. h[i] = 1;
  41. }
  42. for (int i = 1; i<=m; ++i)
  43. {
  44. int x, y, w;
  45. cin >> x >> y >> w;
  46. Edges.push_back({w, {x, y}});
  47. }
  48. sort(Edges.begin(), Edges.end());
  49. for (auto x:Edges)
  50. {
  51. if (stramos(x.second.first) == stramos(x.second.second)) continue;
  52. Union(x.second.first, x.second.second);
  53. graf[x.second.first].push_back(x.second.second);
  54. graf[x.second.second].push_back(x.second.first);
  55. Costuri[{x, y}] = Costuri[{y, x}] = x.first;
  56. }
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement