// Author: Christoph Schwirzer // Algorithm: Kruskal // Complexity: O(n log n) #include #include #include #include #include using namespace std; class union_find { private: vector p, r; public: void init(int n) { p.resize(n); r.assign(n, 0); for (int i=0; i r[b]) p[b] = a; else { p[a] = b; if (r[a] == r[b]) r[b]++; } return 1; } }; struct Edge { int x, y, w; void read() { scanf("%d%d%d", &x, &y, &w); } bool operator<(const Edge &e) const { return w < e.w; } }; int main() { freopen("dark.in", "r", stdin); while (1) { int n, m; scanf("%d%d", &n, &m); if (!n && !m) break; assert(n > 0); assert(m >= n-1); assert(m <= 200000); int res = 0; vector e(m); for (int i=0; i= 0); assert(e[i].y >= 0); assert(e[i].x < n); assert(e[i].y < n); assert(e[i].w >= 0); assert(INT_MAX - e[i].w >= res); res += e[i].w; } sort(e.begin(), e.end()); union_find uf; uf.init(n); int cnt = 0, mst = 0; for (int i=0; cnt