线段树分治
考虑删掉所有的边权为 (i) 的边,如果连通那么就是答案,线段树分治就是保证了优先遍历小的部分
// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
#define Tp template
using pii = pair<int, int>;
#define fir first
#define sec second
Tp<class T> void cmax(T& x, const T& y) {if (x < y) x = y;} Tp<class T> void cmin(T& x, const T& y) {if (x > y) x = y;}
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
Tp<class T> void sort(vector<T>& v) { sort(all(v)); } Tp<class T> void reverse(vector<T>& v) { reverse(all(v)); }
Tp<class T> void unique(vector<T>& v) { sort(all(v)), v.erase(unique(all(v)), v.end()); }
const int SZ = 1 << 23 | 233;
struct FILEIN { char qwq[SZ], *S = qwq, *T = qwq, ch;
#ifdef __WIN64
#define GETC getchar
#else
char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
#endif
FILEIN& operator>>(char& c) {while (isspace(c = GETC()));return *this;}
FILEIN& operator>>(string& s) {while (isspace(ch = GETC())); s = ch;while (!isspace(ch = GETC())) s += ch;return *this;}
Tp<class T> void read(T& x) { bool sign = 0;while ((ch = GETC()) < 48) sign ^= (ch == 45); x = (ch ^ 48);
while ((ch = GETC()) > 47) x = (x << 1) + (x << 3) + (ch ^ 48); x = sign ? -x : x;
}FILEIN& operator>>(int& x) { return read(x), *this; } FILEIN& operator>>(ll& x) { return read(x), *this; }
} in;
struct FILEOUT {const static int LIMIT = 1 << 22 ;char quq[SZ], ST[233];int sz, O;
~FILEOUT() { flush() ; }void flush() {fwrite(quq, 1, O, stdout); fflush(stdout);O = 0;}
FILEOUT& operator<<(char c) {return quq[O++] = c, *this;}
FILEOUT& operator<<(string str) {if (O > LIMIT) flush();for (char c : str) quq[O++] = c;return *this;}
Tp<class T> void write(T x) {if (O > LIMIT) flush();if (x < 0) {quq[O++] = 45;x = -x;}
do {ST[++sz] = x % 10 ^ 48;x /= 10;} while (x);while (sz) quq[O++] = ST[sz--];
}FILEOUT& operator<<(int x) { return write(x), *this; } FILEOUT& operator<<(ll x) { return write(x), *this; }
} out;
#define int long long
int n , m ;
struct edge {
int u , v , w ;
};
const int maxn = 2e6 + 10 ;
edge e[maxn] ;
struct DSU {
int fa[maxn] , sz[maxn] ;
DSU() {
for(int i = 1 ; i < maxn ; i ++)
sz[fa[i] = i] = 1 ;
}
int find(int x) {
return x == fa[x] ? x : find(fa[x]) ;
}
int merge(int x , int y) {
x = find(x) , y = find(y) ;
if(x == y) return 0 ;
if(sz[x] > sz[y]) {
fa[y] = x , sz[x] += sz[y] ;
return y ;
}
else {
fa[x] = y , sz[y] += sz[x] ;
return x ;
}
}
void del(int x) {
sz[fa[x]] -= sz[x] ;
fa[x] = x ;
}
int operator [](int x) {
return sz[find(x)] ;
}
} dsu ;
void solve(int l , int r , int now) {
if(l == r) {
if(dsu[1] == n) {
out << l << '
' ;
exit(0) ;
}
return ;
}
int mid = l + r >> 1 ;
vector < int > d ;
int p = now , qwq = 0 ;
while(e[p].w <= r && p <= m) {
if(e[p].w > mid && (qwq = dsu.merge(e[p].u , e[p].v)))
d.pb(qwq) ;
++ p ;
}
solve(l , mid , now) ;
while(d.size()) dsu.del(d.back()) , d.pop_back() ;
p = now , qwq = 0 ;
while(e[p].w <= mid && p <= m) {
if((qwq = dsu.merge(e[p].u , e[p].v)))
d.pb(qwq) ;
++ p ;
}
solve(mid + 1 , r , p) ;
while(d.size()) dsu.del(d.back()) , d.pop_back() ;
}
signed main() {
// code begin.
in >> n >> m ;
rep(i , 1 , m)
in >> e[i].u >> e[i].v >> e[i].w ;
sort(e + 1 , e + m + 1 , [](const edge & a , const edge & b) {
return a.w < b.w ;
}) ;
solve(0 , e[m].w + 1 , 1) ;
return 0;
// code end.
}