結果
問題 | No.1473 おでぶなおばけさん |
ユーザー |
|
提出日時 | 2022-05-01 21:13:29 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 60 ms / 2,000 ms |
コード長 | 2,355 bytes |
コンパイル時間 | 1,894 ms |
コンパイル使用メモリ | 151,452 KB |
最終ジャッジ日時 | 2025-01-29 01:23:52 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 47 |
ソースコード
#pragma GCC optimize("Ofast") #include <iostream> #include <vector> #include <algorithm> #include <map> #include <queue> #include <cstdio> #include <ctime> #include <assert.h> #include <chrono> #include <random> #include <numeric> #include <set> #include <deque> #include <stack> #include <sstream> #include <utility> #include <cstring> #include <unordered_map> #include <unordered_set> #include <tuple> #include <array> #include <bitset> 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<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9; } struct UnionFind{ vector<int> par,num; UnionFind(int n):par(n),num(n,1){ iota(par.begin(),par.end(),0); //include<numeric> } 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]<num[v])swap(u,v); num[u]+=num[v]; par[v]=u; } bool same(int u,int v){ return find(u) == find(v); } int size(int v){ return num[find(v)]; } }; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n,m; cin >> n >> m; vector<pair<int,pair<int,int>>> v(m); for(int i=0;i<m;i++){ cin >> 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<vector<int>> 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<int> dp(n,-1); dp[0] = 0; queue<int> 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; }