struct Edge
{
ll u, v, cost;
Edge(ll u, ll v, ll cost) : u(u), v(v), cost(cost) {}
bool operator<(const Edge &e) const { return cost < e.cost; }
};
struct DSU
{
vector<ll> parent, size; // Representative
// The leader should be the parent of itself
DSU(int n)
{
parent.resize(n + 1);
size.resize(n + 1, 1); // Each component if of size 1 initially
iota(parent.begin(), parent.end(), 0);
}
ll representative(ll node)
{
// Small to large technique
if (parent[node] == node)
return node;
return parent[node] = representative(parent[node]); // Path compression
}
void Union(ll u, ll v)
{
ll rep1 = representative(u);
ll rep2 = representative(v);
if (rep1 == rep2)
return;
if (size[rep1] > size[rep2])
swap(rep1, rep2); // representative of smaller set comes first
parent[rep1] = rep2;
size[rep2] += size[rep1];
}
bool isSameComponent(ll u, ll v)
{
return (representative(u) == representative(v));
}
map<ll, ll> findConnectedComponents(int N)
{
map<ll, ll> components;
for (int i = 1; i <= N; i++)
{
ll root = representative(i);
components[root]++;
}
return components;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
ll N, M, A;
// cin >> t;
while (t--)
{
cin >> N >> M >> A;
vector<Edge> graph;
DSU disjointSet(N + 1);
for (int i{}; i < M; i++)
{
ll u, v, c;
cin >> u >> v >> c;
graph.push_back({u, v, c});
}
sort(graph.begin(), graph.end()); // Sort edges by cost
// Kruskal's MST logic
ll mstCost = 0;
for (const auto &[u, v, cost] : graph)
{
if (!disjointSet.isSameComponent(u, v))
{
disjointSet.Union(u, v);
mstCost += cost;
}
}
}
return 0;
}
c3RydWN0IEVkZ2UKewogICAgbGwgdSwgdiwgY29zdDsKICAgIEVkZ2UobGwgdSwgbGwgdiwgbGwgY29zdCkgOiB1KHUpLCB2KHYpLCBjb3N0KGNvc3QpIHt9CiAgICBib29sIG9wZXJhdG9yPChjb25zdCBFZGdlICZlKSBjb25zdCB7IHJldHVybiBjb3N0IDwgZS5jb3N0OyB9Cn07CgpzdHJ1Y3QgRFNVCnsKICAgIHZlY3RvcjxsbD4gcGFyZW50LCBzaXplOyAvLyBSZXByZXNlbnRhdGl2ZQogICAgLy8gVGhlIGxlYWRlciBzaG91bGQgYmUgdGhlIHBhcmVudCBvZiBpdHNlbGYKICAgIERTVShpbnQgbikKICAgIHsKICAgICAgICBwYXJlbnQucmVzaXplKG4gKyAxKTsKICAgICAgICBzaXplLnJlc2l6ZShuICsgMSwgMSk7IC8vIEVhY2ggY29tcG9uZW50IGlmIG9mIHNpemUgMSBpbml0aWFsbHkKICAgICAgICBpb3RhKHBhcmVudC5iZWdpbigpLCBwYXJlbnQuZW5kKCksIDApOwogICAgfQogICAgbGwgcmVwcmVzZW50YXRpdmUobGwgbm9kZSkKICAgIHsKICAgICAgICAvLyBTbWFsbCB0byBsYXJnZSB0ZWNobmlxdWUKICAgICAgICBpZiAocGFyZW50W25vZGVdID09IG5vZGUpCiAgICAgICAgICAgIHJldHVybiBub2RlOwoKICAgICAgICByZXR1cm4gcGFyZW50W25vZGVdID0gcmVwcmVzZW50YXRpdmUocGFyZW50W25vZGVdKTsgLy8gUGF0aCBjb21wcmVzc2lvbgogICAgfQogICAgdm9pZCBVbmlvbihsbCB1LCBsbCB2KQogICAgewogICAgICAgIGxsIHJlcDEgPSByZXByZXNlbnRhdGl2ZSh1KTsKICAgICAgICBsbCByZXAyID0gcmVwcmVzZW50YXRpdmUodik7CiAgICAgICAgaWYgKHJlcDEgPT0gcmVwMikKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIGlmIChzaXplW3JlcDFdID4gc2l6ZVtyZXAyXSkKICAgICAgICAgICAgc3dhcChyZXAxLCByZXAyKTsgLy8gcmVwcmVzZW50YXRpdmUgb2Ygc21hbGxlciBzZXQgY29tZXMgZmlyc3QKICAgICAgICBwYXJlbnRbcmVwMV0gPSByZXAyOwogICAgICAgIHNpemVbcmVwMl0gKz0gc2l6ZVtyZXAxXTsKICAgIH0KICAgIGJvb2wgaXNTYW1lQ29tcG9uZW50KGxsIHUsIGxsIHYpCiAgICB7CiAgICAgICAgcmV0dXJuIChyZXByZXNlbnRhdGl2ZSh1KSA9PSByZXByZXNlbnRhdGl2ZSh2KSk7CiAgICB9CiAgICBtYXA8bGwsIGxsPiBmaW5kQ29ubmVjdGVkQ29tcG9uZW50cyhpbnQgTikKICAgIHsKICAgICAgICBtYXA8bGwsIGxsPiBjb21wb25lbnRzOwogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IE47IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIGxsIHJvb3QgPSByZXByZXNlbnRhdGl2ZShpKTsKICAgICAgICAgICAgY29tcG9uZW50c1tyb290XSsrOwogICAgICAgIH0KICAgICAgICByZXR1cm4gY29tcG9uZW50czsKICAgIH0KfTsKCmludCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKG51bGxwdHIpOwogICAgaW50IHQgPSAxOwogICAgbGwgTiwgTSwgQTsKICAgIC8vIGNpbiA+PiB0OwogICAgd2hpbGUgKHQtLSkKICAgIHsKICAgICAgICBjaW4gPj4gTiA+PiBNID4+IEE7CiAgICAgICAgdmVjdG9yPEVkZ2U+IGdyYXBoOwogICAgICAgIERTVSBkaXNqb2ludFNldChOICsgMSk7CiAgICAgICAgZm9yIChpbnQgaXt9OyBpIDwgTTsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgbGwgdSwgdiwgYzsKICAgICAgICAgICAgY2luID4+IHUgPj4gdiA+PiBjOwogICAgICAgICAgICBncmFwaC5wdXNoX2JhY2soe3UsIHYsIGN9KTsKICAgICAgICB9CiAgICAgICAgc29ydChncmFwaC5iZWdpbigpLCBncmFwaC5lbmQoKSk7IC8vIFNvcnQgZWRnZXMgYnkgY29zdAoKICAgICAgICAvLyBLcnVza2FsJ3MgTVNUIGxvZ2ljCiAgICAgICAgbGwgbXN0Q29zdCA9IDA7CgogICAgICAgIGZvciAoY29uc3QgYXV0byAmW3UsIHYsIGNvc3RdIDogZ3JhcGgpCiAgICAgICAgewogICAgICAgICAgICBpZiAoIWRpc2pvaW50U2V0LmlzU2FtZUNvbXBvbmVudCh1LCB2KSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZGlzam9pbnRTZXQuVW5pb24odSwgdik7CiAgICAgICAgICAgICAgICBtc3RDb3N0ICs9IGNvc3Q7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQ==