結果

問題 No.1473 おでぶなおばけさん
ユーザー KKT89KKT89
提出日時 2022-05-01 21:13:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 2,355 bytes
コンパイル時間 1,989 ms
コンパイル使用メモリ 148,916 KB
実行使用メモリ 11,024 KB
最終ジャッジ日時 2023-09-13 14:02:59
合計ジャッジ時間 5,886 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 58 ms
10,448 KB
testcase_03 AC 49 ms
8,848 KB
testcase_04 AC 32 ms
7,260 KB
testcase_05 AC 13 ms
4,376 KB
testcase_06 AC 50 ms
8,620 KB
testcase_07 AC 62 ms
11,024 KB
testcase_08 AC 65 ms
10,960 KB
testcase_09 AC 62 ms
11,012 KB
testcase_10 AC 36 ms
4,380 KB
testcase_11 AC 36 ms
4,408 KB
testcase_12 AC 36 ms
4,404 KB
testcase_13 AC 24 ms
4,376 KB
testcase_14 AC 17 ms
4,376 KB
testcase_15 AC 32 ms
4,376 KB
testcase_16 AC 35 ms
4,376 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 30 ms
4,936 KB
testcase_20 AC 40 ms
8,060 KB
testcase_21 AC 43 ms
6,132 KB
testcase_22 AC 51 ms
9,208 KB
testcase_23 AC 43 ms
9,036 KB
testcase_24 AC 44 ms
7,988 KB
testcase_25 AC 56 ms
8,896 KB
testcase_26 AC 59 ms
8,644 KB
testcase_27 AC 23 ms
4,624 KB
testcase_28 AC 61 ms
10,752 KB
testcase_29 AC 41 ms
6,520 KB
testcase_30 AC 47 ms
7,008 KB
testcase_31 AC 51 ms
10,644 KB
testcase_32 AC 40 ms
8,944 KB
testcase_33 AC 35 ms
7,728 KB
testcase_34 AC 21 ms
5,716 KB
testcase_35 AC 20 ms
5,316 KB
testcase_36 AC 43 ms
6,564 KB
testcase_37 AC 44 ms
8,324 KB
testcase_38 AC 10 ms
4,376 KB
testcase_39 AC 35 ms
4,380 KB
testcase_40 AC 35 ms
4,392 KB
testcase_41 AC 32 ms
4,380 KB
testcase_42 AC 32 ms
4,396 KB
testcase_43 AC 37 ms
7,892 KB
testcase_44 AC 37 ms
7,948 KB
testcase_45 AC 37 ms
7,904 KB
testcase_46 AC 44 ms
7,716 KB
testcase_47 AC 53 ms
8,884 KB
testcase_48 AC 47 ms
8,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}

0