結果

問題 No.1565 Union
ユーザー 👑 CleyL
提出日時 2022-01-07 09:10:46
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 639 bytes
コンパイル時間 1,009 ms
コンパイル使用メモリ 77,624 KB
実行使用メモリ 15,104 KB
最終ジャッジ日時 2024-11-08 18:07:28
合計ジャッジ時間 6,613 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
using namespace std;

struct d{
  int p,dist;
};

int main(){
  int n,m;cin>>n>>m;
  vector<int> A[n];
  for(int i = 0; m > i; i++){
    int a,b;cin>>a>>b;
    a--;b--;
    A[a].push_back(b);
    A[b].push_back(a);
  }
  queue<d> B;
  vector<int> lck(n);
  B.push({0,0});
  while(B.size()){
    auto x = B.front();B.pop();
    for(int i = 0; (int)A[x.p].size() > i; i++){
      if(!lck[A[x.p][i]]){
        lck[A[x.p][i]] = 1;
        if(A[x.p][i] == n-1){
          cout << x.dist+1 << endl;
          return 0;
        }
        B.push({A[x.p][i],x.dist+1});
      }
    }
  }
  cout << -1 << endl;
}
0