Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ll p[N], s[N];
- ll root(ll x) {
- if (x == p[x]) return x;
- return p[x] = root(p[x]);
- }
- void merge(ll x, ll y) {
- x = root(x), y = root(y);
- if (x != y) {
- if (s[x] > s[y]) swap(x, y);
- p[x] = y;
- s[y] += s[x];
- }
- }
- struct edge {
- ll x, y, dist;
- bool operator< (const edge& other) const {
- return dist < other.dist;
- }
- };
- vector<pll> G[N];
- vector<edge> edges;
- void mst() {
- sort(edges.begin(), edges.end());
- for (int i = 0; i <= n; ++i) p[i] = i, s[i] = 1;
- for (auto& e : edges) {
- if (root(e.x) != root(e.y)) {
- merge(e.x, e.y);
- G[e.x].emplace_back(e.y, e.dist);
- G[e.y].emplace_back(e.x, e.dist);
- }
- }
- }
- void clear() {
- for (int i = 0; i < n; ++i) G[i].clear();
- edges.clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement