結果

問題 No.1565 Union
コンテスト
ユーザー miscalc
提出日時 2026-03-07 07:25:08
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,610 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,189 ms
コンパイル使用メモリ 343,264 KB
実行使用メモリ 16,912 KB
最終ジャッジ日時 2026-03-07 07:25:15
合計ジャッジ時間 4,975 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pll = pair<ll, ll>;
#define vc vector
using vl = vc<ll>;

#define ov(a, b, c, name, ...) name
#define rep2(i, a, b) for (ll i = (a); i < ll(b); i++)
#define rep1(i, n) rep2(i, 0, n)
#define rep(...) ov(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)
#define fec(...) for (const auto &__VA_ARGS__)
#define per(i, a, b) for (ll i = ll(a) - 1; i >= ll(b); i--)

bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }
const ll INF = ll(4e18) + 100;

#define ALL(x) begin(x), end(x)
#define SZ(x) (ll)size(x)
#define LB(v, x) (lower_bound(ALL(v), x) - begin(v))
#define eb emplace_back

void printvec(const auto &v)
{
    for (auto it = begin(v); it != end(v); it++) cout << *it << " ";
    cout << endl;
}

#ifdef LOCAL
#define local(...) __VA_ARGS__
#else
#define local(...)
#endif

void main2()
{
    ll N, M;
    cin >> N >> M;
    vc<vl> G(N);
    rep(i, M)
    {
        ll a, b;
        cin >> a >> b;
        a--, b--;
        G[a].eb(b);
        G[b].eb(a);
    }

    vl D(N, INF);
    D[0] = 0;
    queue<ll> que;
    que.push(0);
    while (!que.empty())
    {
        ll v = que.front();
        que.pop();
        fec(nv : G[v])
        {
            if (chmin(D[nv], D[v] + 1)) que.push(nv);
        }
    }
    ll ans = D[N - 1];
    cout << (ans == INF ? -1 : ans) << endl;
}

int main()
{
#ifndef LOCAL
    cin.tie(0)->sync_with_stdio(0), cout.tie(0);
#endif

    local(while (true))
    {
        ll t = 1;
        // cin >> t;
        while (t--) main2();
    }
}
0