fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4.  
  5. const int MAXN = 1115;
  6.  
  7. struct Edge {
  8. int u, v, w;
  9. };
  10.  
  11. struct cmp {
  12. bool operator() (Edge x, Edge y) {
  13. return x.w < y.w;
  14. }
  15. };
  16.  
  17. int N, M;
  18. vector<Edge> g;
  19.  
  20. struct Dosu {
  21. int par[MAXN], sz[MAXN];
  22. void INIT() {
  23. for (int i = 1; i <= MAXN - 1; i++) par[i] = i, sz[i] = 1;
  24. }
  25.  
  26. int find_set(int u) {
  27. return (par[u] == u ? u : par[u] = find_set(par[u]));
  28. }
  29.  
  30. void union_set(int u, int v) {
  31. u = find_set(u), v = find_set(v);
  32. if (sz[u] < sz[v]) swap(u, v);
  33. par[v] = par[u];
  34. sz[u] += sz[v];
  35. }
  36. } DSU;
  37.  
  38.  
  39. main() {
  40. ios_base::sync_with_stdio(0);
  41. cin.tie(0);
  42. cout.tie(0);
  43.  
  44. cin >> N >> M;
  45. DSU.INIT();
  46.  
  47. for (int i = 1; i <= M; i++) {
  48. int u, v, w;
  49. cin >> u >> v >> w;
  50. g.push_back({u, v, w});
  51. }
  52.  
  53. sort(g.begin(), g.end(), cmp());
  54.  
  55. int ans = 0;
  56.  
  57. for (auto E : g) {
  58. if (DSU.find_set(E.u) != DSU.find_set(E.v)) {
  59. DSU.union_set(E.u, E.v);
  60. ans = max(ans, E.w);
  61. }
  62. //cout << E.u << ' ' << E.v << ' ' << E.w << '\n';
  63. }
  64.  
  65. cout << ans;
  66. }
  67.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty