結果

問題 No.1565 Union
ユーザー hayatenhayaten
提出日時 2021-06-26 13:19:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 1,140 bytes
コンパイル時間 2,176 ms
コンパイル使用メモリ 206,652 KB
実行使用メモリ 15,720 KB
最終ジャッジ日時 2023-09-07 16:26:24
合計ジャッジ時間 6,357 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,500 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 59 ms
5,128 KB
testcase_11 AC 104 ms
12,164 KB
testcase_12 AC 113 ms
6,740 KB
testcase_13 AC 34 ms
5,348 KB
testcase_14 AC 137 ms
10,204 KB
testcase_15 AC 192 ms
14,796 KB
testcase_16 AC 176 ms
14,816 KB
testcase_17 AC 197 ms
14,852 KB
testcase_18 AC 193 ms
14,812 KB
testcase_19 AC 195 ms
15,136 KB
testcase_20 AC 134 ms
15,680 KB
testcase_21 AC 134 ms
15,636 KB
testcase_22 AC 135 ms
15,536 KB
testcase_23 AC 134 ms
15,588 KB
testcase_24 AC 133 ms
15,720 KB
testcase_25 AC 139 ms
15,644 KB
testcase_26 AC 136 ms
15,712 KB
testcase_27 AC 138 ms
15,644 KB
testcase_28 AC 140 ms
15,652 KB
testcase_29 AC 137 ms
15,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define ALL(v) v.begin(), v.end()
using namespace std;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = (1 << 30);
Graph G;

int bfs(int s, int n){
  vector<int> dist(n, INF);
  vector<int> pre(n, -1);
  dist[s] = 0;
  queue<int> que;
  que.push(s);
  while (!que.empty()){
    int v = que.front();
    que.pop();
    for(int u: G[v]){
      if(dist[u] != INF) continue;
      dist[u] = dist[v] + 1;
      pre[u] = v;
      que.push(u);
    }
  }
  return dist[n - 1];
}

int main() {
  int n, m;
  cin >> n >> m;
  G.resize(n);
  rep(i,m){
    int a, b;
    cin >> a >> b;
    a--; b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int ans = bfs(0, n);
  if(ans == INF)ans = -1;
  cout << ans << endl;
}
0