Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define int long long
  4. #define pb push_back
  5. #define F first
  6. #define S second
  7.  
  8. using namespace std;
  9.  
  10. int32_t main ()
  11. {
  12. ios_base::sync_with_stdio(0);
  13. cin.tie(0);
  14. cout.tie(0);
  15. freopen("input.txt", "r", stdin);
  16. freopen("output.txt", "w", stdout);
  17. int n, m;
  18. cin >> n >> m;
  19. vector <pair<int, pair<int, int>>> g;
  20. for (int i=0; i<m; i++)
  21. {
  22. int x, y, z;
  23. cin >> x >> y >> z;
  24. x--, y--;
  25. g.pb({z, {x, y}});
  26. }
  27. sort (g.begin(), g.end());
  28. int root[n];
  29. for (int i=0; i<n; i++)
  30. {
  31. root[i] = i;
  32. }
  33. int ans = 0;
  34. int i = 0;
  35. for (int j=0; j<n-1; j++)
  36. {
  37. while (root[g[i].S.F] == root[g[i].S.S]) i++;
  38. ans += g[i].F;
  39. int mx = max (root[g[i].S.F], root[g[i].S.S]), mn = min (root[g[i].S.F], root[g[i].S.S]);
  40. for (int v=0; v<n; v++)
  41. {
  42. if (root[v] == mx) root[v] = mn;
  43. }
  44. }
  45. cout << ans;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement