#pragma GCC optimize("Ofast") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long int ll; typedef unsigned long long ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } inline double time() { return static_cast(chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9; } struct UnionFind{ vector par,num; UnionFind(int n):par(n),num(n,1){ iota(par.begin(),par.end(),0); //include } int find(int v){ return (par[v]==v)?v:(par[v]=find(par[v])); } void unite(int u,int v){ u=find(u),v=find(v); if(u==v)return; if(num[u]> n >> m; vector>> v(m); for(int i=0;i> v[i].second.first; cin >> v[i].second.second; cin >> v[i].first; v[i].second.first--; v[i].second.second--; } sort(v.rbegin(), v.rend()); UnionFind uf(n); vector> g(n); int u = -1; for(auto p:v){ if(p.first < u)break; int x = p.second.first, y = p.second.second; uf.unite(x, y); g[x].push_back(y); g[y].push_back(x); if(uf.same(0,n-1) and u == -1){ cout << p.first << " "; u = p.first; } } vector dp(n,-1); dp[0] = 0; queue q; q.push(0); while(q.size()){ int s = q.front(); q.pop(); for(int t:g[s]){ if(dp[t] == -1){ dp[t] = dp[s]+1; q.push(t); } } } cout << dp[n-1] << endl; }