結果

問題 No.1565 Union
ユーザー keisuke6
提出日時 2022-08-15 23:55:59
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 219 ms / 2,000 ms
コード長 585 bytes
コンパイル時間 2,150 ms
コンパイル使用メモリ 201,300 KB
最終ジャッジ日時 2025-01-30 23:02:16
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    int N,M;
    cin>>N>>M;
    vector<vector<int>> G(N);
    for(int i=0;i<M;i++){
        int a,b;
        cin>>a>>b;
        a--;
        b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    vector<int> dist(N,-1);
    dist[0] = 0;
    queue<int> q;
    q.push(0);
    while(!q.empty()){
        int pos = q.front();
        q.pop();
        for(int x:G[pos]){
            if(dist[x] != -1) continue;
            q.push(x);
            dist[x] = dist[pos] + 1;
        }
    }
    cout<<dist[N-1]<<endl;
}
0