1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i) #ifdef zerol #define dbg(args...) do { cout << "\033[32;1m" << #args<< " -> "; err(args); } while (false) #else #define dbg(...) #endif void err() { cout << "\033[39;0m" << endl; } template<typename T, typename... Args> void err(T a, Args... args) { cout << a << ' '; err(args...); } // ----------------------------------------------------------------------------- const int maxn = 2E5 + 100; int n, m; struct E { int u, v, d; }; struct EE { int to, d; }; vector<E> edges; int fa[maxn]; vector<EE> G[maxn]; LL ans[maxn]; int findset(int x) { return fa[x] == -1 ? x : fa[x] = findset(fa[x]); }
int dfs(int u, int fa) { int sz = 1; for (EE& e: G[u]) { int v = e.to; if (v == fa) continue; int t = dfs(v, u); ans[e.d] += 1LL * t * (n - t); sz += t; } return sz; }
int main() { memset(fa, -1, sizeof fa); cin >> n >> m; FOR (_, 0, m) { int u, v, d; scanf("%d%d%d", &u, &v, &d); edges.push_back({u, v, d}); } sort(edges.begin(), edges.end(), [](const E& a, const E& b)->bool{ return a.d < b.d; }); int C = n - 1; for (E& e: edges) { if (!C) break; int fu = findset(e.u), fv = findset(e.v); if (fu != fv) { --C; fa[fu] = fv; G[e.u].push_back({e.v, e.d}); G[e.v].push_back({e.u, e.d}); } } dfs(1, -1); FOR (i, 0, maxn) if (ans[i] > 1) { ans[i + 1] += ans[i] >> 1; ans[i] &= 1; } int last = maxn - 1; while (!ans[last]) --last; FORD (i, last, -1) printf("%lld", ans[i]); puts(""); }
|