fork download
  1. #include <bits/stdc++.h>
  2. #define reu(i,a,b) for (int i=a;i<=b;++i)
  3. #define pb push_back
  4. using namespace std;
  5. struct Edge {
  6. int x, y, c;
  7. Edge(int _x, int _y, int _c): x(_x), y(_y), c(_c) {};
  8. };
  9. const int N=1005;
  10.  
  11. struct Dsu
  12. {
  13. vector<int> par;
  14. void init(int n)
  15. {
  16. par.resize(n+5,0);
  17. reu(i,1,n+5) par[i]=i;
  18. }
  19. int findi(int x)
  20.  
  21. {
  22. if (x!=par[x]) return findi(par[x]);
  23. return par[x];
  24. }
  25. bool unionn(int x,int y)
  26. {
  27. x=findi(x); y=findi(y);
  28. if (x==y) return 0;
  29. par[y]=x;
  30. return 1;
  31. }
  32. }dsu;
  33. vector<Edge> v;
  34. int n,m,res=0;
  35. void solve()
  36. {
  37. cin>>n>>m;
  38. reu(i,1,m)
  39. {
  40. int x,y,c;
  41. cin>>x>>y>>c;
  42. v.pb({x,y,c});
  43. }
  44. dsu.init(n);
  45. sort(v.begin(),v.end(),[](Edge &a,Edge &b){return a.c<b.c;});
  46. for(auto e:v)
  47. {
  48. if (!dsu.unionn(e.x,e.y)) continue;
  49. res=max(res,e.c);
  50. }
  51. cout<<res;
  52. return;
  53. }
  54.  
  55. int main()
  56. {
  57. ios::sync_with_stdio(0); cin.tie(0);
  58. int t=1;
  59. while (t--)
  60. {
  61. solve();
  62. }
  63. return 0;
  64. }
Success #stdin #stdout 0s 5588KB
stdin
Standard input is empty
stdout
Standard output is empty