Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector <int> graf[10001];
- map <pair<int, int>, int> Costuri;
- int tata[10001], int h[10001];
- int stramos (int node)
- {
- int rege = node;
- while (rege!=tata[rege])
- rege = tata[rege];
- while (node != tata[node])
- {
- int copie = node;
- node = tata[node];
- tata[copie] = rege;
- }
- return rege;
- }
- void Union (int x, int y)
- {
- int s1 = stramos(x), s2 = stramos(y);
- if (s1 == s2) return;
- if (h[s1] < h[s2])
- swap(s1, s2);
- tata[s2]=s1;
- if (h[s1] == h[s2])
- ++h[s1];
- }
- vector <int, pair<int, int>> Edeges;
- int main(int argc, char const *argv[])
- {
- int n, m, s;
- cin >> n >> m >> s;
- for (int i = 1; i<=n; ++i)
- {
- tata[i] = i;
- h[i] = 1;
- }
- for (int i = 1; i<=m; ++i)
- {
- int x, y, w;
- cin >> x >> y >> w;
- Edges.push_back({w, {x, y}});
- }
- sort(Edges.begin(), Edges.end());
- for (auto x:Edges)
- {
- if (stramos(x.second.first) == stramos(x.second.second)) continue;
- Union(x.second.first, x.second.second);
- graf[x.second.first].push_back(x.second.second);
- graf[x.second.second].push_back(x.second.first);
- Costuri[{x, y}] = Costuri[{y, x}] = x.first;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement