結果
| 問題 | No.1565 Union | 
| コンテスト | |
| ユーザー |  Nachia | 
| 提出日時 | 2021-06-26 13:09:20 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 261 ms / 2,000 ms | 
| コード長 | 704 bytes | 
| コンパイル時間 | 1,250 ms | 
| コンパイル使用メモリ | 75,772 KB | 
| 最終ジャッジ日時 | 2025-01-22 13:08:43 | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 27 | 
ソースコード
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)
int N,M;
vector<vector<int>> E;
int main(){
  cin >> N >> M;
  E.resize(N);
  rep(i,M){
    int a,b; cin >> a >> b; a--; b--;
    E[a].push_back(b);
    E[b].push_back(a);
  }
  vector<int> D(N,-1);
  vector<int> I;
  D[0] = 0;
  I.push_back(0);
  rep(i,I.size()){
    int p = I[i];
    for(int e : E[p]) if(D[e] == -1){
      D[e] = D[p] + 1;
      I.push_back(e);
    }
  }
  cout << D[N-1] << "\n";
  return 0;
}
struct ios_do_not_sync{
  ios_do_not_sync(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} ios_do_not_sync_inst;
            
            
            
        