結果

問題 No.3463 Beltway
コンテスト
ユーザー Nzt3
提出日時 2026-02-28 14:53:43
言語 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  
実行時間 460 ms / 2,000 ms
コード長 3,573 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,363 ms
コンパイル使用メモリ 361,664 KB
実行使用メモリ 83,316 KB
最終ジャッジ日時 2026-02-28 15:51:46
合計ジャッジ時間 7,774 ms
ジャッジサーバーID
(参考情報)
judge7 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
#define rep3(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define rep2(i, a, b) rep3(i, a, b, 1)
#define rep1(i, n) rep2(i, 0, n)
#define rep0(n) rep1(aaaaa, n)
#define ov4(a, b, c, d, name, ...) name
#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define per(i, a, b) for (ll i = (a) - 1; i >= (b); i--)
#define fore(e, v) for (auto &&e : v)
#define all(a) begin(a), end(a)
#define sz(a) (int)(size(a))
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define eb emplace_back

template <typename T, typename S> bool chmin(T &a, const S &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename S> bool chmax(T &a, const S &b) {
  return a < b ? a = b, 1 : 0;
}
const int INF = 1e9 + 100;
const ll INFL = 3e18 + 100;
#define i128 __int128_t
struct _ {
  _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }
} __;

template <typename G> struct LL {
  int n;
  const G g;
  vi ord, low, arti;
  vector<pii> bridge;
  LL(G g) : n(sz(g)), g(g), ord(sz(g), -1), low(sz(g), -1) {
    int k = 0;
    rep(i, n) {
      if (ord[i] == -1)
        k = dfs(i, k, -1);
    }
  } // fe9c0fbc
  int dfs(int x, int k, int p) {
    low[x] = (ord[x] = k++);
    int cnt = 0;
    bool is_arti = false, second = false;
    fore(e, g[x]) {
      if (ord[e] == -1) {
        cnt++;
        k = dfs(e, k, x);
        chmin(low[x], low[e]);
        is_arti |= (p != -1) && (low[e] >= ord[x]);
        if (ord[x] < low[e])
          bridge.eb(minmax(x, e));
      } else if (e != p or second) {
        chmin(low[x], ord[e]);
      } else {
        second = true;
      }
    }
    is_arti |= p == -1 && cnt > 1;
    if (is_arti)
      arti.eb(x);
    return k;
  } // e256f899
};

template <typename G> struct BCC : LL<G> {
  vi used;
  vector<vector<pii>> bc;
  vector<pii> tmp;
  using L = LL<G>;
  using L::g;
  using L::low;
  using L::ord;
  BCC(G g) : L(g) { build(); }
  void build() {
    used.assign(sz(g), 0);
    rep(i, sz(used)) if (!used[i]) dfs(i, -1);
  } // daab9fc0
  void dfs(int x, int p) {
    used[x] = true;
    fore(e, g[x]) {
      if (e == p)
        continue;
      if (!used[e] || ord[e] < ord[x])
        tmp.eb(minmax(x, e));
      if (!used[e]) {
        dfs(e, x);
        if (low[e] >= ord[x]) {
          bc.eb();
          while (true) {
            auto p = tmp.back();
            bc.back().eb(p);
            tmp.pop_back();
            if (p.first == min(x, e) and p.second == max(x, e))
              break;
          }
        }
      }
    }
  } // 8b01843c
};

int main() {
  int N, M, s, g;
  cin >> N >> M >> s >> g;
  --s, --g;
  vector G(N, vi());
  rep(i, M) {
    int U, V;
    cin >> U >> V;
    --U, --V;
    G[V].push_back(U);
    G[U].push_back(V);
  }
  BCC Gbcc(G);
  set<pii> cycleEdge;
  fore(i, Gbcc.bc) {
    if (sz(i) == 1)
      continue;
    for (auto [u, v] : i) {
      cycleEdge.insert(pii{u, v});
      cycleEdge.insert(pii{v, u});
    }
  }
  vi dist(N, INF), ans(N, -1);
  queue<int> bfs;
  dist[s] = 0;
  ans[s] = 0;
  bfs.push(s);
  while (bfs.size()) {
    int v = bfs.front();
    bfs.pop();
    for (auto u : G[v]) {
      if (dist[u] > dist[v] + 1) {
        dist[u] = dist[v] + 1;
        ans[u] = ans[v] + cycleEdge.contains(pii{v, u});
        bfs.push(u);
      } else if (dist[u] == dist[v] + 1) {
        chmax(ans[u], ans[v] + cycleEdge.contains(pii{v, u}));
      }
    }
  }
  cout << ans[g] << '\n';
}
0