5 条题解

  • -2
    @ 2023-8-19 17:57:43

    #include <bits/stdc++.h> using namespace std;

    struct node { long long u, v, w; }a[500020]; long long fa[500020], tot, ans;

    bool cmp(node x, node y) { return x.w < y.w; } long long get(int x) { if (fa[x] == x) return x; return fa[x] = get(fa[x]); }

    int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, cnt = 0; cin >> n >> m; for (int i = 1; i <= n; i++) { fa[i] = i; } for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; a[++cnt].u= u; a[cnt].v = v; a[cnt].w = w; } sort(a + 1, a + cnt + 1, cmp); for (int i = 1; i <= m; i++) { long long u = a[i].u; long long v = a[i].v; long long w = a[i].w; if (get(u) != get(v)) { if(ans<w)ans=w; tot++; fa[get(u)] = get(v); if (tot == n - 1) { break; }

    }
    }
    cout << ans << endl;
    return 0;
    

    }

    信息

    ID
    474
    时间
    1000ms
    内存
    128MiB
    难度
    3
    标签
    递交数
    148
    已通过
    75
    上传者