結果

問題 No.1565 Union
ユーザー aaaaaaaa
提出日時 2022-10-27 17:20:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 603 bytes
コンパイル時間 1,661 ms
コンパイル使用メモリ 175,644 KB
実行使用メモリ 16,512 KB
最終ジャッジ日時 2024-05-02 22:21:39
合計ジャッジ時間 6,596 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 61 ms
6,144 KB
testcase_11 AC 97 ms
12,584 KB
testcase_12 AC 121 ms
8,192 KB
testcase_13 AC 48 ms
5,760 KB
testcase_14 AC 120 ms
11,392 KB
testcase_15 AC 181 ms
16,000 KB
testcase_16 AC 157 ms
15,796 KB
testcase_17 AC 156 ms
15,980 KB
testcase_18 AC 172 ms
15,900 KB
testcase_19 AC 157 ms
15,908 KB
testcase_20 AC 131 ms
16,384 KB
testcase_21 AC 136 ms
16,456 KB
testcase_22 AC 130 ms
16,484 KB
testcase_23 AC 131 ms
16,512 KB
testcase_24 AC 132 ms
16,384 KB
testcase_25 AC 134 ms
16,492 KB
testcase_26 AC 132 ms
16,512 KB
testcase_27 AC 135 ms
16,512 KB
testcase_28 AC 135 ms
16,480 KB
testcase_29 AC 133 ms
16,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,m;
  cin>>n>>m;
  int a[m],b[m];
  vector<vector<int>> g(n);
  for(int i = 0;i < m;i++){
    cin>>a[i]>>b[i];
    a[i]--;
    b[i]--;
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }
  queue<int> q;
  q.push(0);
  vector<int> cost(n,1000000);
  cost[0] = 0;
  while(!q.empty()){
    int nn = q.front();
    q.pop();
    for(int x:g[nn]){
      if(cost[x] == 1000000){
        q.push(x);
        cost[x] = cost[nn]+1;
      }
    }
  }
  if(cost[n-1] == 1000000){
    cout<<-1<<endl;
  }
  else{
  cout<<cost[n-1]<<endl;
  }
}
0