結果
| 問題 |
No.1565 Union
|
| コンテスト | |
| ユーザー |
hayaten
|
| 提出日時 | 2021-06-26 13:19:27 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 227 ms / 2,000 ms |
| コード長 | 1,140 bytes |
| コンパイル時間 | 2,220 ms |
| コンパイル使用メモリ | 203,392 KB |
| 最終ジャッジ日時 | 2025-01-22 13:12:02 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
#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;
}
hayaten