Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int long long
- #define pb push_back
- #define F first
- #define S second
- using namespace std;
- int32_t main ()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- int n, m;
- cin >> n >> m;
- vector <pair<int, pair<int, int>>> g;
- for (int i=0; i<m; i++)
- {
- int x, y, z;
- cin >> x >> y >> z;
- x--, y--;
- g.pb({z, {x, y}});
- }
- sort (g.begin(), g.end());
- int root[n];
- for (int i=0; i<n; i++)
- {
- root[i] = i;
- }
- int ans = 0;
- int i = 0;
- for (int j=0; j<n-1; j++)
- {
- while (root[g[i].S.F] == root[g[i].S.S]) i++;
- ans += g[i].F;
- 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]);
- for (int v=0; v<n; v++)
- {
- if (root[v] == mx) root[v] = mn;
- }
- }
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement