結果

問題 No.1565 Union
ユーザー CleyLCleyL
提出日時 2022-01-07 09:10:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 639 bytes
コンパイル時間 891 ms
コンパイル使用メモリ 78,408 KB
実行使用メモリ 15,180 KB
最終ジャッジ日時 2024-04-26 05:23:44
合計ジャッジ時間 6,194 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 66 ms
6,940 KB
testcase_11 AC 114 ms
11,940 KB
testcase_12 AC 125 ms
7,040 KB
testcase_13 AC 35 ms
6,940 KB
testcase_14 AC 148 ms
10,112 KB
testcase_15 AC 193 ms
14,316 KB
testcase_16 AC 170 ms
14,220 KB
testcase_17 AC 174 ms
14,688 KB
testcase_18 AC 180 ms
14,552 KB
testcase_19 AC 192 ms
14,524 KB
testcase_20 AC 144 ms
15,180 KB
testcase_21 AC 148 ms
14,908 KB
testcase_22 AC 148 ms
14,932 KB
testcase_23 AC 145 ms
14,980 KB
testcase_24 AC 144 ms
14,976 KB
testcase_25 AC 147 ms
14,972 KB
testcase_26 AC 144 ms
14,980 KB
testcase_27 AC 147 ms
15,024 KB
testcase_28 AC 150 ms
14,976 KB
testcase_29 AC 145 ms
14,872 KB
権限があれば一括ダウンロードができます

ソースコード

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